diff --git a/feilong-security/src/main/java/com/feilong/security/oneway/SHA1Util.java b/feilong-security/src/main/java/com/feilong/security/oneway/SHA1Util.java index 45a41065e..4df3189d2 100755 --- a/feilong-security/src/main/java/com/feilong/security/oneway/SHA1Util.java +++ b/feilong-security/src/main/java/com/feilong/security/oneway/SHA1Util.java @@ -116,7 +116,7 @@ public static String encode(String origin){ * 如果 origin 是null,抛出 {@link NullPointerException}
* @throws EncryptionException * 如果在加密解密的过程中发生了异常,会以EncryptionException形式抛出 - * @see OnewayEncryption#encode(OnewayType, String) + * @see OnewayEncryption#encodeUpperCase(OnewayType, String) * @see com.feilong.lib.codec.digest.DigestUtils#sha1Hex(String) * @since 4.0.8 */ diff --git a/feilong-security/src/main/java/com/feilong/security/oneway/SHA384Util.java b/feilong-security/src/main/java/com/feilong/security/oneway/SHA384Util.java index 82ec5dc17..243a40a98 100644 --- a/feilong-security/src/main/java/com/feilong/security/oneway/SHA384Util.java +++ b/feilong-security/src/main/java/com/feilong/security/oneway/SHA384Util.java @@ -104,7 +104,7 @@ public static String encode(String origin){ * 如果 origin 是null,抛出 {@link NullPointerException}
* @throws EncryptionException * 如果在加密解密的过程中发生了异常,会以EncryptionException形式抛出 - * @see OnewayEncryption#encode(OnewayType, String) + * @see OnewayEncryption#encodeUpperCase(OnewayType, String) * @see com.feilong.lib.codec.digest.DigestUtils#sha384Hex(String) * @since 4.0.8 */ diff --git a/feilong-security/src/main/java/com/feilong/security/oneway/SHA512Util.java b/feilong-security/src/main/java/com/feilong/security/oneway/SHA512Util.java index db8049024..64f699499 100644 --- a/feilong-security/src/main/java/com/feilong/security/oneway/SHA512Util.java +++ b/feilong-security/src/main/java/com/feilong/security/oneway/SHA512Util.java @@ -19,8 +19,18 @@ import com.feilong.security.EncryptionException; /** - * The Class SHA512Util. - * + * SHA-512是一种安全哈希算法,也称为SHA-2 512,它是SHA-2系列中的一种。 + * + *

+ * SHA-512是一种密码散列函数,可以将任意长度的数据映射为固定长度的512位(64字节)的散列值。 + * + * SHA-512由美国国家标准与技术研究院(NIST)于2001年发布,作为美国联邦信息处理标准(FIPS)。它是在SHA-2系列中继SHA-256之后发布的第二种算法。 + * + * SHA-512的主要用途是用于验证数据的完整性和来源,例如电子邮件、数字签名等。由于其产生的散列值长度较长,因此被认为比SHA-256更安全。然而,由于其计算复杂度较高,因此在某些场景下可能会比SHA-256慢一些。 + * + * 需要注意的是,SHA-512并不是一种可以解密的算法,它是一种单向散列函数,只能用于将数据加密成固定长度的字符串,无法将加密后的字符串还原为原始数据。因此,在使用SHA-512进行数据加密时,需要谨慎选择合适的密钥和参数,以确保数据的安全性和完整性。 + *

+ * * @author feilong * @see OnewayType#SHA512 * @since 1.14.2 @@ -42,15 +52,26 @@ private SHA512Util(){ //--------------------------------------------------------------- /** - * 使用sha512算法 单向加密字符串. + * 使用sha512算法 单向加密字符串(小写). * *

* 加密之后的转成小写的16进制字符串 *

+ *

示例:

+ *
+ * + *
+     * SHA512Util.encode("") = "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e"
+     * SHA512Util.encode("123456") = "ba3253876aed6bc22d4a6ff53d8406c6ad864195ed144ab5c87621b6c233b548baeae6956df346ec8c17f5ea10f35ee3cbc514797ed7ddd3145464e2a0bab413"
+     * 
+ * + *
+ * * * @param origin * 原始字符串,将使用默认的 {@link String#getBytes()} 转成字节数组
- * @return 加密之后的转成小写的16进制字符串 + * @return 加密之后的转成小写的16进制字符串
+ * 如果 origin 是null,抛出 {@link NullPointerException}
* @throws EncryptionException * 如果在加密解密的过程中发生了异常,会以EncryptionException形式抛出 * @see OnewayEncryption#encode(OnewayType, String) @@ -61,7 +82,37 @@ public static String encode(String origin){ } /** - * 使用sha512算法 单向加密字符串. + * 使用sha512算法 单向加密字符串(大写). + * + *

+ * 加密之后的转成大写的16进制字符串 + *

+ *

示例:

+ *
+ * + *
+     * SHA512Util.encodeUpperCase("") = "CF83E1357EEFB8BDF1542850D66D8007D620E4050B5715DC83F4A921D36CE9CE47D0D13C5D85F2B0FF8318D2877EEC2F63B931BD47417A81A538327AF927DA3E"
+     * SHA512Util.encodeUpperCase("123456") = "BA3253876AED6BC22D4A6FF53D8406C6AD864195ED144AB5C87621B6C233B548BAEAE6956DF346EC8C17F5EA10F35EE3CBC514797ED7DDD3145464E2A0BAB413"
+     * 
+ * + *
+ * + * @param origin + * 原始字符串,将使用默认的 {@link String#getBytes()} 转成字节数组
+ * @return 加密之后的转成大写的16进制字符串
+ * 如果 origin 是null,抛出 {@link NullPointerException}
+ * @throws EncryptionException + * 如果在加密解密的过程中发生了异常,会以EncryptionException形式抛出 + * @see OnewayEncryption#encodeUpperCase(OnewayType, String) + * @see com.feilong.lib.codec.digest.DigestUtils#sha512Hex(String) + * @since 4.0.8 + */ + public static String encodeUpperCase(String origin){ + return OnewayEncryption.encodeUpperCase(ONEWAYTYPE, origin); + } + + /** + * 使用sha512算法 单向加密字符串(小写). * *

* 加密之后的转成小写的16进制字符串 @@ -72,7 +123,8 @@ public static String encode(String origin){ * 如果需要string 转码,请自行调用value.getBytes(string chartsetname),再调用{@link #encode(String, String)} * @param charsetName * 受支持的 {@link CharsetType} 名称,比如 utf-8 - * @return 加密之后的转成小写的16进制字符串 + * @return 加密之后的转成小写的16进制字符串
+ * 如果 origin 是null,抛出 {@link NullPointerException}
* @throws EncryptionException * 如果在加密解密的过程中发生了异常,会以EncryptionException形式抛出 * @see OnewayEncryption#encode(OnewayType, String, String) diff --git a/feilong-security/src/test/java/com/feilong/security/SuiteTests.java b/feilong-security/src/test/java/com/feilong/security/SuiteTests.java index 2d7903087..fcbb7b1e0 100644 --- a/feilong-security/src/test/java/com/feilong/security/SuiteTests.java +++ b/feilong-security/src/test/java/com/feilong/security/SuiteTests.java @@ -20,7 +20,6 @@ import org.junit.runners.Suite.SuiteClasses; import com.feilong.security.oneway.OnewayEncryptionParameterizedTest; -import com.feilong.security.oneway.SHA512UtilTest; import com.feilong.security.oneway.md5.Md5EncodeFileTest; import com.feilong.security.oneway.md5.Md5EncodeParameterizedTest; import com.feilong.security.oneway.md5.Md5EncodeTest; @@ -32,6 +31,8 @@ import com.feilong.security.oneway.sha256.Sha256EncodeFileTest; import com.feilong.security.oneway.sha384.SHA384UtilTest; import com.feilong.security.oneway.sha384.Sha384EncodeFileTest; +import com.feilong.security.oneway.sha512.SHA512UtilTest; +import com.feilong.security.oneway.sha512.Sha512EncodeFileTest; import com.feilong.security.symmetric.AesUtilTest; @RunWith(Suite.class) @@ -54,6 +55,7 @@ Sha384EncodeFileTest.class, SHA512UtilTest.class, + Sha512EncodeFileTest.class, OnewayEncryptionParameterizedTest.class, diff --git a/feilong-security/src/test/java/com/feilong/security/oneway/sha512/SHA512UtilTest.java b/feilong-security/src/test/java/com/feilong/security/oneway/sha512/SHA512UtilTest.java new file mode 100644 index 000000000..80a44d9fc --- /dev/null +++ b/feilong-security/src/test/java/com/feilong/security/oneway/sha512/SHA512UtilTest.java @@ -0,0 +1,60 @@ +/* + * Copyright (C) 2008 feilong + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.feilong.security.oneway.sha512; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; + +import com.feilong.lib.codec.digest.DigestUtils; +import com.feilong.security.AbstractSecurityTest; +import com.feilong.security.oneway.SHA512Util; + +public class SHA512UtilTest extends AbstractSecurityTest{ + + @Test + public void encode121(){ + assertEquals(DigestUtils.sha512Hex("2284208963"), SHA512Util.encode("2284208963")); + } + + @Test + public void encode12(){ + LOGGER.debug(debugSecurityValue(SHA512Util.encode("2284208963"))); + } + + //--------------------------------------------------------------- + + @Test + public void encodeUpperCase(){ + assertEquals( + "BA3253876AED6BC22D4A6FF53D8406C6AD864195ED144AB5C87621B6C233B548BAEAE6956DF346EC8C17F5EA10F35EE3CBC514797ED7DDD3145464E2A0BAB413", + SHA512Util.encodeUpperCase("123456")); + assertEquals( + "CF83E1357EEFB8BDF1542850D66D8007D620E4050B5715DC83F4A921D36CE9CE47D0D13C5D85F2B0FF8318D2877EEC2F63B931BD47417A81A538327AF927DA3E", + SHA512Util.encodeUpperCase("")); + } + + @Test + public void encode(){ + assertEquals( + "ba3253876aed6bc22d4a6ff53d8406c6ad864195ed144ab5c87621b6c233b548baeae6956df346ec8c17f5ea10f35ee3cbc514797ed7ddd3145464e2a0bab413", + SHA512Util.encode("123456")); + assertEquals( + "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e", + SHA512Util.encode("")); + } + +} diff --git a/feilong-security/src/test/java/com/feilong/security/oneway/SHA512UtilTest.java b/feilong-security/src/test/java/com/feilong/security/oneway/sha512/Sha512EncodeFileTest.java similarity index 83% rename from feilong-security/src/test/java/com/feilong/security/oneway/SHA512UtilTest.java rename to feilong-security/src/test/java/com/feilong/security/oneway/sha512/Sha512EncodeFileTest.java index 3afd6bd30..c1b89f42a 100644 --- a/feilong-security/src/test/java/com/feilong/security/oneway/SHA512UtilTest.java +++ b/feilong-security/src/test/java/com/feilong/security/oneway/sha512/Sha512EncodeFileTest.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.feilong.security.oneway; +package com.feilong.security.oneway.sha512; import static org.junit.Assert.assertEquals; @@ -24,8 +24,9 @@ import com.feilong.io.InputStreamUtil; import com.feilong.lib.codec.digest.DigestUtils; import com.feilong.security.AbstractSecurityTest; +import com.feilong.security.oneway.SHA512Util; -public class SHA512UtilTest extends AbstractSecurityTest{ +public class Sha512EncodeFileTest extends AbstractSecurityTest{ @Test public void encodeFile() throws IOException{ @@ -35,17 +36,6 @@ public void encodeFile() throws IOException{ encodeFile); assertEquals(encodeFile, DigestUtils.sha512Hex(InputStreamUtil.getInputStream(LOCATION))); } - - @Test - public void encode121(){ - assertEquals(DigestUtils.sha512Hex("2284208963"), SHA512Util.encode("2284208963")); - } - - @Test - public void encode12(){ - LOGGER.debug(debugSecurityValue(SHA512Util.encode("2284208963"))); - } - //--------------------------------------------------------------- @Test(expected = NullPointerException.class)