Skip to content

Commit

Permalink
新建 com.feilong.security.oneway.SHA512Util.encodeUpperCase(String)
Browse files Browse the repository at this point in the history
返回大写的结果 fix #695

更新 com.feilong.security.oneway.SHA512Util javadoc fix #696
  • Loading branch information
venusdrogon committed Jan 26, 2024
1 parent 6a89785 commit 20d754f
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public static String encode(String origin){
* 如果 <code>origin</code> 是null,抛出 {@link NullPointerException}<br>
* @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
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public static String encode(String origin){
* 如果 <code>origin</code> 是null,抛出 {@link NullPointerException}<br>
* @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
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,18 @@
import com.feilong.security.EncryptionException;

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

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

/**
* 使用sha512算法 单向加密字符串.
* 使用sha512算法 单向加密字符串(大写).
*
* <p>
* 加密之后的转成<span style="color:green">大写的</span>16进制字符串
* </p>
* <h3>示例:</h3>
* <blockquote>
*
* <pre class="code">
* SHA512Util.encodeUpperCase("") = "CF83E1357EEFB8BDF1542850D66D8007D620E4050B5715DC83F4A921D36CE9CE47D0D13C5D85F2B0FF8318D2877EEC2F63B931BD47417A81A538327AF927DA3E"
* SHA512Util.encodeUpperCase("123456") = "BA3253876AED6BC22D4A6FF53D8406C6AD864195ED144AB5C87621B6C233B548BAEAE6956DF346EC8C17F5EA10F35EE3CBC514797ED7DDD3145464E2A0BAB413"
* </pre>
*
* </blockquote>
*
* @param origin
* 原始字符串,将使用默认的 {@link String#getBytes()} 转成字节数组<br>
* @return 加密之后的转成大写的16进制字符串<br>
* 如果 <code>origin</code> 是null,抛出 {@link NullPointerException}<br>
* @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算法 单向加密字符串(小写).
*
* <p>
* 加密之后的转成<span style="color:green">小写的</span>16进制字符串
Expand All @@ -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进制字符串<br>
* 如果 <code>origin</code> 是null,抛出 {@link NullPointerException}<br>
* @throws EncryptionException
* 如果在加密解密的过程中发生了异常,会以EncryptionException形式抛出
* @see OnewayEncryption#encode(OnewayType, String, String)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
Expand All @@ -54,6 +55,7 @@
Sha384EncodeFileTest.class,

SHA512UtilTest.class,
Sha512EncodeFileTest.class,

OnewayEncryptionParameterizedTest.class,

Expand Down
Original file line number Diff line number Diff line change
@@ -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(""));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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{
Expand All @@ -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)
Expand Down

0 comments on commit 20d754f

Please sign in to comment.