新增 SHA-256 工具,使用 ShaUtils 替代 Sha1Utils
This commit is contained in:
@@ -19,6 +19,7 @@ import java.security.*;
|
||||
public class DigestUtils {
|
||||
|
||||
public static final String SHA1 = "SHA-1";
|
||||
public static final String SHA256 = "SHA-256";
|
||||
public static final String MD5 = "MD5";
|
||||
public static final String SM3 = "SM3";
|
||||
|
||||
|
||||
@@ -4,79 +4,11 @@
|
||||
*/
|
||||
package com.jeesite.common.codec;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
/**
|
||||
* SHA-1 加密工具类,散列加密,不可逆加密
|
||||
* @author ThinkGem
|
||||
*/
|
||||
public class Sha1Utils {
|
||||
|
||||
/**
|
||||
* 生成随机的 Byte[] 作为 salt 密钥.
|
||||
* @param numBytes byte数组的大小
|
||||
*/
|
||||
public static byte[] genSalt(int numBytes) {
|
||||
return DigestUtils.genSalt(numBytes);
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成随机的 Byte[] 作为 salt 密钥,返回 HEX 值
|
||||
* @param numBytes byte 数组的大小
|
||||
*/
|
||||
public static String genSaltString(int numBytes) {
|
||||
return DigestUtils.genSaltString(numBytes);
|
||||
}
|
||||
|
||||
/**
|
||||
* 对输入字符串进行 SHA-1 散列.
|
||||
*/
|
||||
public static byte[] sha1(byte[] input) {
|
||||
return DigestUtils.digest(input, DigestUtils.SHA1, null, 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* 对输入字符串进行 SHA-1 散列.
|
||||
*/
|
||||
public static String sha1(String input) {
|
||||
return EncodeUtils.encodeHex(sha1(input.getBytes(StandardCharsets.UTF_8)));
|
||||
}
|
||||
|
||||
/**
|
||||
* 对输入字符串进行 SHA-1 散列.
|
||||
*/
|
||||
public static byte[] sha1(byte[] input, byte[] salt) {
|
||||
return DigestUtils.digest(input, DigestUtils.SHA1, salt, 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* 对输入字符串进行 SHA-1 散列.
|
||||
*/
|
||||
public static String sha1(String data, String salt) {
|
||||
return EncodeUtils.encodeHex(sha1(data.getBytes(StandardCharsets.UTF_8), EncodeUtils.decodeHex(salt)));
|
||||
}
|
||||
|
||||
/**
|
||||
* 对输入字符串进行 SHA-1 散列.
|
||||
*/
|
||||
public static byte[] sha1(byte[] input, byte[] salt, int iterations) {
|
||||
return DigestUtils.digest(input, DigestUtils.SHA1, salt, iterations);
|
||||
}
|
||||
|
||||
/**
|
||||
* 对输入字符串进行 SHA-1 散列.
|
||||
*/
|
||||
public static String sha1(String input, String salt, int iterations) {
|
||||
return EncodeUtils.encodeHex(sha1(input.getBytes(StandardCharsets.UTF_8), EncodeUtils.decodeHex(salt), iterations));
|
||||
}
|
||||
|
||||
/**
|
||||
* 对文件进行 SHA-1 散列.
|
||||
*/
|
||||
public static byte[] sha1(InputStream input) throws IOException {
|
||||
return DigestUtils.digest(input, DigestUtils.SHA1);
|
||||
}
|
||||
@Deprecated
|
||||
public class Sha1Utils extends ShaUtils {
|
||||
|
||||
}
|
||||
|
||||
131
common/src/main/java/com/jeesite/common/codec/ShaUtils.java
Normal file
131
common/src/main/java/com/jeesite/common/codec/ShaUtils.java
Normal file
@@ -0,0 +1,131 @@
|
||||
/**
|
||||
* Copyright (c) 2013-Now http://jeesite.com All rights reserved.
|
||||
* No deletion without permission, or be held responsible to law.
|
||||
*/
|
||||
package com.jeesite.common.codec;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
/**
|
||||
* SHA-1 和 SHA-256 加密工具类,散列加密,不可逆加密
|
||||
* @author ThinkGem
|
||||
*/
|
||||
public class ShaUtils {
|
||||
|
||||
/**
|
||||
* 生成随机的 Byte[] 作为 salt 密钥.
|
||||
* @param numBytes byte数组的大小
|
||||
*/
|
||||
public static byte[] genSalt(int numBytes) {
|
||||
return DigestUtils.genSalt(numBytes);
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成随机的 Byte[] 作为 salt 密钥,返回 HEX 值
|
||||
* @param numBytes byte 数组的大小
|
||||
*/
|
||||
public static String genSaltString(int numBytes) {
|
||||
return DigestUtils.genSaltString(numBytes);
|
||||
}
|
||||
|
||||
/**
|
||||
* 对输入字符串进行 SHA-1 散列.
|
||||
*/
|
||||
public static byte[] sha1(byte[] input) {
|
||||
return DigestUtils.digest(input, DigestUtils.SHA1, null, 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* 对输入字符串进行 SHA-1 散列.
|
||||
*/
|
||||
public static String sha1(String input) {
|
||||
return EncodeUtils.encodeHex(sha1(input.getBytes(StandardCharsets.UTF_8)));
|
||||
}
|
||||
|
||||
/**
|
||||
* 对输入字符串进行 SHA-1 散列.
|
||||
*/
|
||||
public static byte[] sha1(byte[] input, byte[] salt) {
|
||||
return DigestUtils.digest(input, DigestUtils.SHA1, salt, 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* 对输入字符串进行 SHA-1 散列.
|
||||
*/
|
||||
public static String sha1(String data, String salt) {
|
||||
return EncodeUtils.encodeHex(sha1(data.getBytes(StandardCharsets.UTF_8), EncodeUtils.decodeHex(salt)));
|
||||
}
|
||||
|
||||
/**
|
||||
* 对输入字符串进行 SHA-1 散列.
|
||||
*/
|
||||
public static byte[] sha1(byte[] input, byte[] salt, int iterations) {
|
||||
return DigestUtils.digest(input, DigestUtils.SHA1, salt, iterations);
|
||||
}
|
||||
|
||||
/**
|
||||
* 对输入字符串进行 SHA-1 散列.
|
||||
*/
|
||||
public static String sha1(String input, String salt, int iterations) {
|
||||
return EncodeUtils.encodeHex(sha1(input.getBytes(StandardCharsets.UTF_8), EncodeUtils.decodeHex(salt), iterations));
|
||||
}
|
||||
|
||||
/**
|
||||
* 对文件进行 SHA-1 散列.
|
||||
*/
|
||||
public static byte[] sha1(InputStream input) throws IOException {
|
||||
return DigestUtils.digest(input, DigestUtils.SHA1);
|
||||
}
|
||||
|
||||
/**
|
||||
* 对输入字符串进行 SHA-256 散列.
|
||||
*/
|
||||
public static byte[] sha256(byte[] input) {
|
||||
return DigestUtils.digest(input, DigestUtils.SHA256, null, 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* 对输入字符串进行 SHA-256 散列.
|
||||
*/
|
||||
public static String sha256(String input) {
|
||||
return EncodeUtils.encodeHex(sha256(input.getBytes(StandardCharsets.UTF_8)));
|
||||
}
|
||||
|
||||
/**
|
||||
* 对输入字符串进行 SHA-256 散列.
|
||||
*/
|
||||
public static byte[] sha256(byte[] input, byte[] salt) {
|
||||
return DigestUtils.digest(input, DigestUtils.SHA256, salt, 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* 对输入字符串进行 SHA-256 散列.
|
||||
*/
|
||||
public static String sha256(String data, String salt) {
|
||||
return EncodeUtils.encodeHex(sha256(data.getBytes(StandardCharsets.UTF_8), EncodeUtils.decodeHex(salt)));
|
||||
}
|
||||
|
||||
/**
|
||||
* 对输入字符串进行 SHA-256 散列.
|
||||
*/
|
||||
public static byte[] sha256(byte[] input, byte[] salt, int iterations) {
|
||||
return DigestUtils.digest(input, DigestUtils.SHA256, salt, iterations);
|
||||
}
|
||||
|
||||
/**
|
||||
* 对输入字符串进行 SHA-256 散列.
|
||||
*/
|
||||
public static String sha256(String input, String salt, int iterations) {
|
||||
return EncodeUtils.encodeHex(sha256(input.getBytes(StandardCharsets.UTF_8), EncodeUtils.decodeHex(salt), iterations));
|
||||
}
|
||||
|
||||
/**
|
||||
* 对文件进行 SHA-256 散列.
|
||||
*/
|
||||
public static byte[] sha256(InputStream input) throws IOException {
|
||||
return DigestUtils.digest(input, DigestUtils.SHA256);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,28 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2013-Now http://jeesite.com All rights reserved.
|
||||
* No deletion without permission, or be held responsible to law.
|
||||
*/
|
||||
package com.jeesite.test.codec;
|
||||
|
||||
import com.jeesite.common.codec.Sha1Utils;
|
||||
|
||||
/**
|
||||
* SHA-1 加密工具类,散列加密,不可逆加密
|
||||
* @author ThinkGem
|
||||
* @version 2024-07-22
|
||||
*/
|
||||
public class Sha1UtilsTest {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
String s = "Hello word! 你好,中文!";
|
||||
System.out.println(s);
|
||||
|
||||
String salt = Sha1Utils.genSaltString(8);
|
||||
System.out.println(salt);
|
||||
String data = Sha1Utils.sha1(s, salt);
|
||||
System.out.println(data);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
/**
|
||||
* Copyright (c) 2013-Now http://jeesite.com All rights reserved.
|
||||
* No deletion without permission, or be held responsible to law.
|
||||
*/
|
||||
package com.jeesite.test.codec;
|
||||
|
||||
import com.jeesite.common.codec.ShaUtils;
|
||||
|
||||
/**
|
||||
* SHA-1 加密工具类,散列加密,不可逆加密
|
||||
* @author ThinkGem
|
||||
* @version 2024-07-22
|
||||
*/
|
||||
public class ShaUtilsTest {
|
||||
|
||||
public static final int HASH_ITERATIONS = 1024;
|
||||
public static final int SALT_SIZE = 8;
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
String s = "Hello word! 你好,中文!";
|
||||
System.out.println(s);
|
||||
|
||||
String salt = ShaUtils.genSaltString(SALT_SIZE);
|
||||
System.out.println(salt);
|
||||
String data = ShaUtils.sha1(s, salt, HASH_ITERATIONS);
|
||||
System.out.println(data);
|
||||
|
||||
String salt2 = ShaUtils.genSaltString(SALT_SIZE);
|
||||
System.out.println(salt2);
|
||||
String data2 = ShaUtils.sha256(s, salt2, HASH_ITERATIONS);
|
||||
System.out.println(data2);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user