This is an automated email from the ASF dual-hosted git repository.

elecharny pushed a commit to branch 1.2.X
in repository https://gitbox.apache.org/repos/asf/mina-ftpserver.git


The following commit(s) were added to refs/heads/1.2.X by this push:
     new 42d8b181 Add the SHA-256 and SHA-512 methods
42d8b181 is described below

commit 42d8b181f4a17b85f4d2c31d850f93d3c37b890a
Author: emmanuel lecharny <elecha...@apache.org>
AuthorDate: Tue Jul 4 00:41:47 2023 +0200

    Add the SHA-256 and SHA-512 methods
---
 .../org/apache/ftpserver/util/EncryptUtils.java    | 54 ++++++++++++++--------
 1 file changed, 36 insertions(+), 18 deletions(-)

diff --git a/core/src/main/java/org/apache/ftpserver/util/EncryptUtils.java 
b/core/src/main/java/org/apache/ftpserver/util/EncryptUtils.java
index 04be9fe5..06ebcdda 100644
--- a/core/src/main/java/org/apache/ftpserver/util/EncryptUtils.java
+++ b/core/src/main/java/org/apache/ftpserver/util/EncryptUtils.java
@@ -19,6 +19,7 @@
 
 package org.apache.ftpserver.util;
 
+import java.nio.charset.StandardCharsets;
 import java.security.MessageDigest;
 import java.security.NoSuchAlgorithmException;
 
@@ -36,20 +37,24 @@ public class EncryptUtils {
     /**
      * Encrypt byte array.
      */
-    public final static byte[] encrypt(byte[] source, String algorithm)
-            throws NoSuchAlgorithmException {
+    public final static byte[] encrypt(byte[] source, String algorithm) throws 
NoSuchAlgorithmException {
         MessageDigest md = MessageDigest.getInstance(algorithm);
         md.reset();
         md.update(source);
+        
         return md.digest();
     }
 
     /**
      * Encrypt string
      */
-    public final static String encrypt(String source, String algorithm)
-            throws NoSuchAlgorithmException {
-        byte[] resByteArray = encrypt(source.getBytes(), algorithm);
+    public final static String encrypt(String source, String algorithm) throws 
NoSuchAlgorithmException {
+        if (source == null) {
+            source = "";
+        }
+
+        byte[] resByteArray = encrypt(source.getBytes( StandardCharsets.UTF_8 
), algorithm);
+        
         return StringUtils.toHexString(resByteArray);
     }
 
@@ -57,36 +62,49 @@ public class EncryptUtils {
      * Encrypt string using MD5 algorithm
      */
     public final static String encryptMD5(String source) {
-        if (source == null) {
-            source = "";
-        }
-
-        String result = "";
         try {
-            result = encrypt(source, MD5.MD5);
+            return encrypt(source, MD5.MD5);
         } catch (NoSuchAlgorithmException ex) {
             // this should never happen
             throw new RuntimeException(ex);
         }
-        return result;
     }
 
     /**
-     * Encrypt string using SHA algorithm
+     * Encrypt string using SHA-1 algorithm
      */
     public final static String encryptSHA(String source) {
-        if (source == null) {
-            source = "";
+        try {
+            return encrypt(source, "SHA");
+        } catch (NoSuchAlgorithmException ex) {
+            // this should never happen
+            throw new RuntimeException(ex);
         }
+    }
+
 
-        String result = "";
+    /**
+     * Encrypt string using SHA-256 algorithm
+     */
+    public final static String encryptSHA256(String source) {
         try {
-            result = encrypt(source, "SHA");
+            return encrypt(source, "SHA-256");
         } catch (NoSuchAlgorithmException ex) {
             // this should never happen
             throw new RuntimeException(ex);
         }
-        return result;
     }
 
+
+    /**
+     * Encrypt string using SHA-512 algorithm
+     */
+    public final static String encryptSHA512(String source) {
+        try {
+            return encrypt(source, "SHA-512");
+        } catch (NoSuchAlgorithmException ex) {
+            // this should never happen
+            throw new RuntimeException(ex);
+        }
+    }
 }

Reply via email to