mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
del methods
This commit is contained in:
parent
90a137a95b
commit
253ec671c1
@ -243,7 +243,7 @@ public class KeyUtil {
|
||||
/**
|
||||
* 生成RSA私钥,仅用于非对称加密<br>
|
||||
* 采用PKCS#8规范,此规范定义了私钥信息语法和加密私钥语法<br>
|
||||
* 算法见:https://docs.oracle.com/javase/7/docs/technotes/guides/security/StandardNames.html#KeyFactory
|
||||
* 算法见:<a href="https://docs.oracle.com/javase/7/docs/technotes/guides/security/StandardNames.html#KeyFactory">...</a>
|
||||
*
|
||||
* @param key 密钥,必须为DER编码存储
|
||||
* @return RSA私钥 {@link PrivateKey}
|
||||
@ -256,7 +256,7 @@ public class KeyUtil {
|
||||
/**
|
||||
* 生成私钥,仅用于非对称加密<br>
|
||||
* 采用PKCS#8规范,此规范定义了私钥信息语法和加密私钥语法<br>
|
||||
* 算法见:https://docs.oracle.com/javase/7/docs/technotes/guides/security/StandardNames.html#KeyFactory
|
||||
* 算法见:<a href="https://docs.oracle.com/javase/7/docs/technotes/guides/security/StandardNames.html#KeyFactory">...</a>
|
||||
*
|
||||
* @param algorithm 算法,如RSA、EC、SM2等
|
||||
* @param key 密钥,PKCS#8格式
|
||||
@ -271,7 +271,7 @@ public class KeyUtil {
|
||||
|
||||
/**
|
||||
* 生成私钥,仅用于非对称加密<br>
|
||||
* 算法见:https://docs.oracle.com/javase/7/docs/technotes/guides/security/StandardNames.html#KeyFactory
|
||||
* 算法见:<a href="https://docs.oracle.com/javase/7/docs/technotes/guides/security/StandardNames.html#KeyFactory">...</a>
|
||||
*
|
||||
* @param algorithm 算法,如RSA、EC、SM2等
|
||||
* @param keySpec {@link KeySpec}
|
||||
@ -309,7 +309,7 @@ public class KeyUtil {
|
||||
/**
|
||||
* 生成RSA公钥,仅用于非对称加密<br>
|
||||
* 采用X509证书规范<br>
|
||||
* 算法见:https://docs.oracle.com/javase/7/docs/technotes/guides/security/StandardNames.html#KeyFactory
|
||||
* 算法见:<a href="https://docs.oracle.com/javase/7/docs/technotes/guides/security/StandardNames.html#KeyFactory">...</a>
|
||||
*
|
||||
* @param key 密钥,必须为DER编码存储
|
||||
* @return 公钥 {@link PublicKey}
|
||||
@ -322,7 +322,7 @@ public class KeyUtil {
|
||||
/**
|
||||
* 生成公钥,仅用于非对称加密<br>
|
||||
* 采用X509证书规范<br>
|
||||
* 算法见:https://docs.oracle.com/javase/7/docs/technotes/guides/security/StandardNames.html#KeyFactory
|
||||
* 算法见:<a href="https://docs.oracle.com/javase/7/docs/technotes/guides/security/StandardNames.html#KeyFactory">...</a>
|
||||
*
|
||||
* @param algorithm 算法
|
||||
* @param key 密钥,必须为DER编码存储
|
||||
@ -337,7 +337,7 @@ public class KeyUtil {
|
||||
|
||||
/**
|
||||
* 生成公钥,仅用于非对称加密<br>
|
||||
* 算法见:https://docs.oracle.com/javase/7/docs/technotes/guides/security/StandardNames.html#KeyFactory
|
||||
* 算法见:<a href="https://docs.oracle.com/javase/7/docs/technotes/guides/security/StandardNames.html#KeyFactory">...</a>
|
||||
*
|
||||
* @param algorithm 算法
|
||||
* @param keySpec {@link KeySpec}
|
||||
@ -358,7 +358,7 @@ public class KeyUtil {
|
||||
|
||||
/**
|
||||
* 生成用于非对称加密的公钥和私钥,仅用于非对称加密<br>
|
||||
* 密钥对生成算法见:https://docs.oracle.com/javase/7/docs/technotes/guides/security/StandardNames.html#KeyPairGenerator
|
||||
* 密钥对生成算法见:<a href="https://docs.oracle.com/javase/7/docs/technotes/guides/security/StandardNames.html#KeyPairGenerator">...</a>
|
||||
*
|
||||
* @param algorithm 非对称加密算法
|
||||
* @return {@link KeyPair}
|
||||
@ -375,7 +375,7 @@ public class KeyUtil {
|
||||
|
||||
/**
|
||||
* 生成用于非对称加密的公钥和私钥<br>
|
||||
* 密钥对生成算法见:https://docs.oracle.com/javase/7/docs/technotes/guides/security/StandardNames.html#KeyPairGenerator
|
||||
* 密钥对生成算法见:<a href="https://docs.oracle.com/javase/7/docs/technotes/guides/security/StandardNames.html#KeyPairGenerator">...</a>
|
||||
*
|
||||
* @param algorithm 非对称加密算法
|
||||
* @param keySize 密钥模(modulus )长度
|
||||
@ -387,7 +387,7 @@ public class KeyUtil {
|
||||
|
||||
/**
|
||||
* 生成用于非对称加密的公钥和私钥<br>
|
||||
* 密钥对生成算法见:https://docs.oracle.com/javase/7/docs/technotes/guides/security/StandardNames.html#KeyPairGenerator
|
||||
* 密钥对生成算法见:<a href="https://docs.oracle.com/javase/7/docs/technotes/guides/security/StandardNames.html#KeyPairGenerator">...</a>
|
||||
*
|
||||
* @param algorithm 非对称加密算法
|
||||
* @param keySize 密钥模(modulus )长度
|
||||
@ -406,7 +406,7 @@ public class KeyUtil {
|
||||
|
||||
/**
|
||||
* 生成用于非对称加密的公钥和私钥<br>
|
||||
* 密钥对生成算法见:https://docs.oracle.com/javase/7/docs/technotes/guides/security/StandardNames.html#KeyPairGenerator
|
||||
* 密钥对生成算法见:<a href="https://docs.oracle.com/javase/7/docs/technotes/guides/security/StandardNames.html#KeyPairGenerator">...</a>
|
||||
*
|
||||
* @param algorithm 非对称加密算法
|
||||
* @param params {@link AlgorithmParameterSpec}
|
||||
@ -419,7 +419,7 @@ public class KeyUtil {
|
||||
|
||||
/**
|
||||
* 生成用于非对称加密的公钥和私钥<br>
|
||||
* 密钥对生成算法见:https://docs.oracle.com/javase/7/docs/technotes/guides/security/StandardNames.html#KeyPairGenerator
|
||||
* 密钥对生成算法见:<a href="https://docs.oracle.com/javase/7/docs/technotes/guides/security/StandardNames.html#KeyPairGenerator">...</a>
|
||||
*
|
||||
* @param algorithm 非对称加密算法
|
||||
* @param param {@link AlgorithmParameterSpec}
|
||||
@ -433,7 +433,7 @@ public class KeyUtil {
|
||||
|
||||
/**
|
||||
* 生成用于非对称加密的公钥和私钥<br>
|
||||
* 密钥对生成算法见:https://docs.oracle.com/javase/7/docs/technotes/guides/security/StandardNames.html#KeyPairGenerator
|
||||
* 密钥对生成算法见:<a href="https://docs.oracle.com/javase/7/docs/technotes/guides/security/StandardNames.html#KeyPairGenerator">...</a>
|
||||
*
|
||||
* <p>
|
||||
* 对于非对称加密算法,密钥长度有严格限制,具体如下:
|
||||
@ -467,7 +467,7 @@ public class KeyUtil {
|
||||
|
||||
/**
|
||||
* 生成用于非对称加密的公钥和私钥<br>
|
||||
* 密钥对生成算法见:https://docs.oracle.com/javase/7/docs/technotes/guides/security/StandardNames.html#KeyPairGenerator
|
||||
* 密钥对生成算法见:<a href="https://docs.oracle.com/javase/7/docs/technotes/guides/security/StandardNames.html#KeyPairGenerator">...</a>
|
||||
*
|
||||
* <p>
|
||||
* 对于非对称加密算法,密钥长度有严格限制,具体如下:
|
||||
@ -663,7 +663,7 @@ public class KeyUtil {
|
||||
/**
|
||||
* 读取密钥库(Java Key Store,JKS) KeyStore文件<br>
|
||||
* KeyStore文件用于数字证书的密钥对保存<br>
|
||||
* see: http://snowolf.iteye.com/blog/391931
|
||||
* see: <a href="http://snowolf.iteye.com/blog/391931">...</a>
|
||||
*
|
||||
* @param keyFile 证书文件
|
||||
* @param password 密码
|
||||
@ -677,9 +677,9 @@ public class KeyUtil {
|
||||
/**
|
||||
* 读取密钥库(Java Key Store,JKS) KeyStore文件<br>
|
||||
* KeyStore文件用于数字证书的密钥对保存<br>
|
||||
* see: http://snowolf.iteye.com/blog/391931
|
||||
* see: <a href="http://snowolf.iteye.com/blog/391931">...</a>
|
||||
*
|
||||
* @param in {@link InputStream} 如果想从文件读取.keystore文件,使用 {@link FileUtil#getInputStream(java.io.File)} 读取
|
||||
* @param in {@link InputStream} 如果想从文件读取.keystore文件,使用 {@link FileUtil#getInputStream(File)} 读取
|
||||
* @param password 密码
|
||||
* @return {@link KeyStore}
|
||||
*/
|
||||
@ -716,7 +716,7 @@ public class KeyUtil {
|
||||
/**
|
||||
* 读取KeyStore文件<br>
|
||||
* KeyStore文件用于数字证书的密钥对保存<br>
|
||||
* see: http://snowolf.iteye.com/blog/391931
|
||||
* see: <a href="http://snowolf.iteye.com/blog/391931">...</a>
|
||||
*
|
||||
* @param type 类型
|
||||
* @param keyFile 证书文件
|
||||
@ -737,10 +737,10 @@ public class KeyUtil {
|
||||
/**
|
||||
* 读取KeyStore文件<br>
|
||||
* KeyStore文件用于数字证书的密钥对保存<br>
|
||||
* see: http://snowolf.iteye.com/blog/391931
|
||||
* see: <a href="http://snowolf.iteye.com/blog/391931">...</a>
|
||||
*
|
||||
* @param type 类型
|
||||
* @param in {@link InputStream} 如果想从文件读取.keystore文件,使用 {@link FileUtil#getInputStream(java.io.File)} 读取
|
||||
* @param in {@link InputStream} 如果想从文件读取.keystore文件,使用 {@link FileUtil#getInputStream(File)} 读取
|
||||
* @param password 密码,null表示无密码
|
||||
* @return {@link KeyStore}
|
||||
*/
|
||||
@ -794,9 +794,9 @@ public class KeyUtil {
|
||||
/**
|
||||
* 读取X.509 Certification文件<br>
|
||||
* Certification为证书文件<br>
|
||||
* see: http://snowolf.iteye.com/blog/391931
|
||||
* see: <a href="http://snowolf.iteye.com/blog/391931">...</a>
|
||||
*
|
||||
* @param in {@link InputStream} 如果想从文件读取.cer文件,使用 {@link FileUtil#getInputStream(java.io.File)} 读取
|
||||
* @param in {@link InputStream} 如果想从文件读取.cer文件,使用 {@link FileUtil#getInputStream(File)} 读取
|
||||
* @param password 密码
|
||||
* @param alias 别名
|
||||
* @return {@link KeyStore}
|
||||
@ -809,9 +809,9 @@ public class KeyUtil {
|
||||
/**
|
||||
* 读取X.509 Certification文件中的公钥<br>
|
||||
* Certification为证书文件<br>
|
||||
* see: https://www.cnblogs.com/yinliang/p/10115519.html
|
||||
* see: <a href="https://www.cnblogs.com/yinliang/p/10115519.html">...</a>
|
||||
*
|
||||
* @param in {@link InputStream} 如果想从文件读取.cer文件,使用 {@link FileUtil#getInputStream(java.io.File)} 读取
|
||||
* @param in {@link InputStream} 如果想从文件读取.cer文件,使用 {@link FileUtil#getInputStream(File)} 读取
|
||||
* @return {@link KeyStore}
|
||||
* @since 4.5.2
|
||||
*/
|
||||
@ -826,9 +826,9 @@ public class KeyUtil {
|
||||
/**
|
||||
* 读取X.509 Certification文件<br>
|
||||
* Certification为证书文件<br>
|
||||
* see: http://snowolf.iteye.com/blog/391931
|
||||
* see: <a href="http://snowolf.iteye.com/blog/391931">...</a>
|
||||
*
|
||||
* @param in {@link InputStream} 如果想从文件读取.cer文件,使用 {@link FileUtil#getInputStream(java.io.File)} 读取
|
||||
* @param in {@link InputStream} 如果想从文件读取.cer文件,使用 {@link FileUtil#getInputStream(File)} 读取
|
||||
* @return {@link KeyStore}
|
||||
* @since 4.4.1
|
||||
*/
|
||||
@ -839,10 +839,10 @@ public class KeyUtil {
|
||||
/**
|
||||
* 读取Certification文件<br>
|
||||
* Certification为证书文件<br>
|
||||
* see: http://snowolf.iteye.com/blog/391931
|
||||
* see: <a href="http://snowolf.iteye.com/blog/391931">...</a>
|
||||
*
|
||||
* @param type 类型,例如X.509
|
||||
* @param in {@link InputStream} 如果想从文件读取.cer文件,使用 {@link FileUtil#getInputStream(java.io.File)} 读取
|
||||
* @param in {@link InputStream} 如果想从文件读取.cer文件,使用 {@link FileUtil#getInputStream(File)} 读取
|
||||
* @param password 密码
|
||||
* @param alias 别名
|
||||
* @return {@link KeyStore}
|
||||
@ -860,10 +860,10 @@ public class KeyUtil {
|
||||
/**
|
||||
* 读取Certification文件<br>
|
||||
* Certification为证书文件<br>
|
||||
* see: http://snowolf.iteye.com/blog/391931
|
||||
* see: <a href="http://snowolf.iteye.com/blog/391931">...</a>
|
||||
*
|
||||
* @param type 类型,例如X.509
|
||||
* @param in {@link InputStream} 如果想从文件读取.cer文件,使用 {@link FileUtil#getInputStream(java.io.File)} 读取
|
||||
* @param in {@link InputStream} 如果想从文件读取.cer文件,使用 {@link FileUtil#getInputStream(File)} 读取
|
||||
* @return {@link Certificate}
|
||||
*/
|
||||
public static Certificate readCertificate(final String type, final InputStream in) {
|
||||
@ -910,7 +910,7 @@ public class KeyUtil {
|
||||
|
||||
/**
|
||||
* 编码压缩EC公钥(基于BouncyCastle)<br>
|
||||
* 见:https://www.cnblogs.com/xinzhao/p/8963724.html
|
||||
* 见:<a href="https://www.cnblogs.com/xinzhao/p/8963724.html">...</a>
|
||||
*
|
||||
* @param publicKey {@link PublicKey},必须为org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPublicKey
|
||||
* @return 压缩得到的X
|
||||
@ -922,7 +922,7 @@ public class KeyUtil {
|
||||
|
||||
/**
|
||||
* 解码恢复EC压缩公钥,支持Base64和Hex编码,(基于BouncyCastle)<br>
|
||||
* 见:https://www.cnblogs.com/xinzhao/p/8963724.html
|
||||
* 见:<a href="https://www.cnblogs.com/xinzhao/p/8963724.html">...</a>
|
||||
*
|
||||
* @param encode 压缩公钥
|
||||
* @param curveName EC曲线名
|
||||
@ -935,7 +935,7 @@ public class KeyUtil {
|
||||
|
||||
/**
|
||||
* 解码恢复EC压缩公钥,支持Base64和Hex编码,(基于BouncyCastle)<br>
|
||||
* 见:https://www.cnblogs.com/xinzhao/p/8963724.html
|
||||
* 见:<a href="https://www.cnblogs.com/xinzhao/p/8963724.html">...</a>
|
||||
*
|
||||
* @param encodeByte 压缩公钥
|
||||
* @param curveName EC曲线名
|
||||
|
@ -1,14 +1,11 @@
|
||||
package cn.hutool.crypto;
|
||||
|
||||
import cn.hutool.core.codec.Base64;
|
||||
import cn.hutool.core.io.FileUtil;
|
||||
import cn.hutool.core.lang.Validator;
|
||||
import cn.hutool.core.codec.HexUtil;
|
||||
import cn.hutool.core.lang.Validator;
|
||||
import cn.hutool.core.text.StrUtil;
|
||||
import cn.hutool.crypto.asymmetric.AsymmetricAlgorithm;
|
||||
import cn.hutool.crypto.asymmetric.RSA;
|
||||
import cn.hutool.crypto.asymmetric.Sign;
|
||||
import cn.hutool.crypto.asymmetric.SignAlgorithm;
|
||||
import cn.hutool.crypto.digest.DigestAlgorithm;
|
||||
import cn.hutool.crypto.digest.Digester;
|
||||
import cn.hutool.crypto.digest.HMac;
|
||||
@ -29,19 +26,10 @@ import javax.crypto.Mac;
|
||||
import javax.crypto.SecretKey;
|
||||
import java.io.File;
|
||||
import java.io.InputStream;
|
||||
import java.security.KeyPair;
|
||||
import java.security.KeyStore;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.security.PrivateKey;
|
||||
import java.security.Provider;
|
||||
import java.security.PublicKey;
|
||||
import java.security.Security;
|
||||
import java.security.Signature;
|
||||
import java.security.cert.Certificate;
|
||||
import java.security.spec.AlgorithmParameterSpec;
|
||||
import java.security.spec.KeySpec;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 安全相关工具类<br>
|
||||
@ -54,219 +42,6 @@ import java.util.Map;
|
||||
*/
|
||||
public class SecureUtil {
|
||||
|
||||
/**
|
||||
* 默认密钥字节数
|
||||
*
|
||||
* <pre>
|
||||
* RSA/DSA
|
||||
* Default Keysize 1024
|
||||
* Keysize must be a multiple of 64, ranging from 512 to 1024 (inclusive).
|
||||
* </pre>
|
||||
*/
|
||||
public static final int DEFAULT_KEY_SIZE = KeyUtil.DEFAULT_KEY_SIZE;
|
||||
|
||||
/**
|
||||
* 生成 {@link SecretKey},仅用于对称加密和摘要算法密钥生成
|
||||
*
|
||||
* @param algorithm 算法,支持PBE算法
|
||||
* @return {@link SecretKey}
|
||||
*/
|
||||
public static SecretKey generateKey(final String algorithm) {
|
||||
return KeyUtil.generateKey(algorithm);
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成 {@link SecretKey},仅用于对称加密和摘要算法密钥生成
|
||||
*
|
||||
* @param algorithm 算法,支持PBE算法
|
||||
* @param keySize 密钥长度
|
||||
* @return {@link SecretKey}
|
||||
* @since 3.1.2
|
||||
*/
|
||||
public static SecretKey generateKey(final String algorithm, final int keySize) {
|
||||
return KeyUtil.generateKey(algorithm, keySize);
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成 {@link SecretKey},仅用于对称加密和摘要算法密钥生成
|
||||
*
|
||||
* @param algorithm 算法
|
||||
* @param key 密钥,如果为{@code null} 自动生成随机密钥
|
||||
* @return {@link SecretKey}
|
||||
*/
|
||||
public static SecretKey generateKey(final String algorithm, final byte[] key) {
|
||||
return KeyUtil.generateKey(algorithm, key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成 {@link SecretKey}
|
||||
*
|
||||
* @param algorithm DES算法,包括DES、DESede等
|
||||
* @param key 密钥
|
||||
* @return {@link SecretKey}
|
||||
*/
|
||||
public static SecretKey generateDESKey(final String algorithm, final byte[] key) {
|
||||
return KeyUtil.generateDESKey(algorithm, key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成PBE {@link SecretKey}
|
||||
*
|
||||
* @param algorithm PBE算法,包括:PBEWithMD5AndDES、PBEWithSHA1AndDESede、PBEWithSHA1AndRC2_40等
|
||||
* @param key 密钥
|
||||
* @return {@link SecretKey}
|
||||
*/
|
||||
public static SecretKey generatePBEKey(final String algorithm, final char[] key) {
|
||||
return KeyUtil.generatePBEKey(algorithm, key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成 {@link SecretKey},仅用于对称加密和摘要算法
|
||||
*
|
||||
* @param algorithm 算法
|
||||
* @param keySpec {@link KeySpec}
|
||||
* @return {@link SecretKey}
|
||||
*/
|
||||
public static SecretKey generateKey(final String algorithm, final KeySpec keySpec) {
|
||||
return KeyUtil.generateKey(algorithm, keySpec);
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成私钥,仅用于非对称加密<br>
|
||||
* 算法见:https://docs.oracle.com/javase/7/docs/technotes/guides/security/StandardNames.html#KeyFactory
|
||||
*
|
||||
* @param algorithm 算法
|
||||
* @param key 密钥
|
||||
* @return 私钥 {@link PrivateKey}
|
||||
*/
|
||||
public static PrivateKey generatePrivateKey(final String algorithm, final byte[] key) {
|
||||
return KeyUtil.generatePrivateKey(algorithm, key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成私钥,仅用于非对称加密<br>
|
||||
* 算法见:https://docs.oracle.com/javase/7/docs/technotes/guides/security/StandardNames.html#KeyFactory
|
||||
*
|
||||
* @param algorithm 算法
|
||||
* @param keySpec {@link KeySpec}
|
||||
* @return 私钥 {@link PrivateKey}
|
||||
* @since 3.1.1
|
||||
*/
|
||||
public static PrivateKey generatePrivateKey(final String algorithm, final KeySpec keySpec) {
|
||||
return KeyUtil.generatePrivateKey(algorithm, keySpec);
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成私钥,仅用于非对称加密
|
||||
*
|
||||
* @param keyStore {@link KeyStore}
|
||||
* @param alias 别名
|
||||
* @param password 密码
|
||||
* @return 私钥 {@link PrivateKey}
|
||||
*/
|
||||
public static PrivateKey generatePrivateKey(final KeyStore keyStore, final String alias, final char[] password) {
|
||||
return KeyUtil.generatePrivateKey(keyStore, alias, password);
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成公钥,仅用于非对称加密<br>
|
||||
* 算法见:https://docs.oracle.com/javase/7/docs/technotes/guides/security/StandardNames.html#KeyFactory
|
||||
*
|
||||
* @param algorithm 算法
|
||||
* @param key 密钥
|
||||
* @return 公钥 {@link PublicKey}
|
||||
*/
|
||||
public static PublicKey generatePublicKey(final String algorithm, final byte[] key) {
|
||||
return KeyUtil.generatePublicKey(algorithm, key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成公钥,仅用于非对称加密<br>
|
||||
* 算法见:https://docs.oracle.com/javase/7/docs/technotes/guides/security/StandardNames.html#KeyFactory
|
||||
*
|
||||
* @param algorithm 算法
|
||||
* @param keySpec {@link KeySpec}
|
||||
* @return 公钥 {@link PublicKey}
|
||||
* @since 3.1.1
|
||||
*/
|
||||
public static PublicKey generatePublicKey(final String algorithm, final KeySpec keySpec) {
|
||||
return KeyUtil.generatePublicKey(algorithm, keySpec);
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成用于非对称加密的公钥和私钥,仅用于非对称加密<br>
|
||||
* 密钥对生成算法见:https://docs.oracle.com/javase/7/docs/technotes/guides/security/StandardNames.html#KeyPairGenerator
|
||||
*
|
||||
* @param algorithm 非对称加密算法
|
||||
* @return {@link KeyPair}
|
||||
*/
|
||||
public static KeyPair generateKeyPair(final String algorithm) {
|
||||
return KeyUtil.generateKeyPair(algorithm);
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成用于非对称加密的公钥和私钥<br>
|
||||
* 密钥对生成算法见:https://docs.oracle.com/javase/7/docs/technotes/guides/security/StandardNames.html#KeyPairGenerator
|
||||
*
|
||||
* @param algorithm 非对称加密算法
|
||||
* @param keySize 密钥模(modulus )长度
|
||||
* @return {@link KeyPair}
|
||||
*/
|
||||
public static KeyPair generateKeyPair(final String algorithm, final int keySize) {
|
||||
return KeyUtil.generateKeyPair(algorithm, keySize);
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成用于非对称加密的公钥和私钥<br>
|
||||
* 密钥对生成算法见:https://docs.oracle.com/javase/7/docs/technotes/guides/security/StandardNames.html#KeyPairGenerator
|
||||
*
|
||||
* @param algorithm 非对称加密算法
|
||||
* @param keySize 密钥模(modulus )长度
|
||||
* @param seed 种子
|
||||
* @return {@link KeyPair}
|
||||
*/
|
||||
public static KeyPair generateKeyPair(final String algorithm, final int keySize, final byte[] seed) {
|
||||
return KeyUtil.generateKeyPair(algorithm, keySize, seed);
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成用于非对称加密的公钥和私钥<br>
|
||||
* 密钥对生成算法见:https://docs.oracle.com/javase/7/docs/technotes/guides/security/StandardNames.html#KeyPairGenerator
|
||||
*
|
||||
* @param algorithm 非对称加密算法
|
||||
* @param params {@link AlgorithmParameterSpec}
|
||||
* @return {@link KeyPair}
|
||||
* @since 4.3.3
|
||||
*/
|
||||
public static KeyPair generateKeyPair(final String algorithm, final AlgorithmParameterSpec params) {
|
||||
return KeyUtil.generateKeyPair(algorithm, params);
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成用于非对称加密的公钥和私钥<br>
|
||||
* 密钥对生成算法见:https://docs.oracle.com/javase/7/docs/technotes/guides/security/StandardNames.html#KeyPairGenerator
|
||||
*
|
||||
* @param algorithm 非对称加密算法
|
||||
* @param seed 种子
|
||||
* @param params {@link AlgorithmParameterSpec}
|
||||
* @return {@link KeyPair}
|
||||
* @since 4.3.3
|
||||
*/
|
||||
public static KeyPair generateKeyPair(final String algorithm, final byte[] seed, final AlgorithmParameterSpec params) {
|
||||
return KeyUtil.generateKeyPair(algorithm, seed, params);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取用于密钥生成的算法<br>
|
||||
* 获取XXXwithXXX算法的后半部分算法,如果为ECDSA或SM2,返回算法为EC
|
||||
*
|
||||
* @param algorithm XXXwithXXX算法
|
||||
* @return 算法
|
||||
*/
|
||||
public static String getAlgorithmAfterWith(final String algorithm) {
|
||||
return KeyUtil.getAlgorithmAfterWith(algorithm);
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成算法,格式为XXXwithXXX
|
||||
*
|
||||
@ -280,116 +55,6 @@ public class SecureUtil {
|
||||
return StrUtil.format("{}with{}", digestPart, asymmetricAlgorithm.getValue());
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成签名对象,仅用于非对称加密
|
||||
*
|
||||
* @param asymmetricAlgorithm {@link AsymmetricAlgorithm} 非对称加密算法
|
||||
* @param digestAlgorithm {@link DigestAlgorithm} 摘要算法
|
||||
* @return {@link Signature}
|
||||
*/
|
||||
public static Signature generateSignature(final AsymmetricAlgorithm asymmetricAlgorithm, final DigestAlgorithm digestAlgorithm) {
|
||||
try {
|
||||
return Signature.getInstance(generateAlgorithm(asymmetricAlgorithm, digestAlgorithm));
|
||||
} catch (final NoSuchAlgorithmException e) {
|
||||
throw new CryptoException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取密钥库(Java Key Store,JKS) KeyStore文件<br>
|
||||
* KeyStore文件用于数字证书的密钥对保存<br>
|
||||
* see: http://snowolf.iteye.com/blog/391931
|
||||
*
|
||||
* @param in {@link InputStream} 如果想从文件读取.keystore文件,使用 {@link FileUtil#getInputStream(java.io.File)} 读取
|
||||
* @param password 密码
|
||||
* @return {@link KeyStore}
|
||||
*/
|
||||
public static KeyStore readJKSKeyStore(final InputStream in, final char[] password) {
|
||||
return KeyUtil.readJKSKeyStore(in, password);
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取KeyStore文件<br>
|
||||
* KeyStore文件用于数字证书的密钥对保存<br>
|
||||
* see: http://snowolf.iteye.com/blog/391931
|
||||
*
|
||||
* @param type 类型
|
||||
* @param in {@link InputStream} 如果想从文件读取.keystore文件,使用 {@link FileUtil#getInputStream(java.io.File)} 读取
|
||||
* @param password 密码
|
||||
* @return {@link KeyStore}
|
||||
*/
|
||||
public static KeyStore readKeyStore(final String type, final InputStream in, final char[] password) {
|
||||
return KeyUtil.readKeyStore(type, in, password);
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取X.509 Certification文件<br>
|
||||
* Certification为证书文件<br>
|
||||
* see: http://snowolf.iteye.com/blog/391931
|
||||
*
|
||||
* @param in {@link InputStream} 如果想从文件读取.cer文件,使用 {@link FileUtil#getInputStream(java.io.File)} 读取
|
||||
* @param password 密码
|
||||
* @param alias 别名
|
||||
* @return {@link KeyStore}
|
||||
* @since 4.4.1
|
||||
*/
|
||||
public static Certificate readX509Certificate(final InputStream in, final char[] password, final String alias) {
|
||||
return KeyUtil.readX509Certificate(in, password, alias);
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取X.509 Certification文件<br>
|
||||
* Certification为证书文件<br>
|
||||
* see: http://snowolf.iteye.com/blog/391931
|
||||
*
|
||||
* @param in {@link InputStream} 如果想从文件读取.cer文件,使用 {@link FileUtil#getInputStream(java.io.File)} 读取
|
||||
* @return {@link KeyStore}
|
||||
* @since 4.4.1
|
||||
*/
|
||||
public static Certificate readX509Certificate(final InputStream in) {
|
||||
return KeyUtil.readX509Certificate(in);
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取Certification文件<br>
|
||||
* Certification为证书文件<br>
|
||||
* see: http://snowolf.iteye.com/blog/391931
|
||||
*
|
||||
* @param type 类型,例如X.509
|
||||
* @param in {@link InputStream} 如果想从文件读取.cer文件,使用 {@link FileUtil#getInputStream(java.io.File)} 读取
|
||||
* @param password 密码
|
||||
* @param alias 别名
|
||||
* @return {@link KeyStore}
|
||||
* @since 4.4.1
|
||||
*/
|
||||
public static Certificate readCertificate(final String type, final InputStream in, final char[] password, final String alias) {
|
||||
return KeyUtil.readCertificate(type, in, password, alias);
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取Certification文件<br>
|
||||
* Certification为证书文件<br>
|
||||
* see: http://snowolf.iteye.com/blog/391931
|
||||
*
|
||||
* @param type 类型,例如X.509
|
||||
* @param in {@link InputStream} 如果想从文件读取.cer文件,使用 {@link FileUtil#getInputStream(java.io.File)} 读取
|
||||
* @return {@link Certificate}
|
||||
*/
|
||||
public static Certificate readCertificate(final String type, final InputStream in) {
|
||||
return KeyUtil.readCertificate(type, in);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得 Certification
|
||||
*
|
||||
* @param keyStore {@link KeyStore}
|
||||
* @param alias 别名
|
||||
* @return {@link Certificate}
|
||||
*/
|
||||
public static Certificate getCertificate(final KeyStore keyStore, final String alias) {
|
||||
return KeyUtil.getCertificate(keyStore, alias);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------- 对称加密算法
|
||||
|
||||
/**
|
||||
@ -820,156 +485,6 @@ public class SecureUtil {
|
||||
return new RSA(privateKey, publicKey);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建签名算法对象<br>
|
||||
* 生成新的私钥公钥对
|
||||
*
|
||||
* @param algorithm 签名算法
|
||||
* @return {@link Sign}
|
||||
* @since 3.3.0
|
||||
*/
|
||||
public static Sign sign(final SignAlgorithm algorithm) {
|
||||
return SignUtil.sign(algorithm);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建签名算法对象<br>
|
||||
* 私钥和公钥同时为空时生成一对新的私钥和公钥<br>
|
||||
* 私钥和公钥可以单独传入一个,如此则只能使用此钥匙来做签名或验证
|
||||
*
|
||||
* @param algorithm 签名算法
|
||||
* @param privateKeyBase64 私钥Base64
|
||||
* @param publicKeyBase64 公钥Base64
|
||||
* @return {@link Sign}
|
||||
* @since 3.3.0
|
||||
*/
|
||||
public static Sign sign(final SignAlgorithm algorithm, final String privateKeyBase64, final String publicKeyBase64) {
|
||||
return SignUtil.sign(algorithm, privateKeyBase64, publicKeyBase64);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建Sign算法对象<br>
|
||||
* 私钥和公钥同时为空时生成一对新的私钥和公钥<br>
|
||||
* 私钥和公钥可以单独传入一个,如此则只能使用此钥匙来做签名或验证
|
||||
*
|
||||
* @param algorithm 算法枚举
|
||||
* @param privateKey 私钥
|
||||
* @param publicKey 公钥
|
||||
* @return {@link Sign}
|
||||
* @since 3.3.0
|
||||
*/
|
||||
public static Sign sign(final SignAlgorithm algorithm, final byte[] privateKey, final byte[] publicKey) {
|
||||
return SignUtil.sign(algorithm, privateKey, publicKey);
|
||||
}
|
||||
|
||||
/**
|
||||
* 对参数做签名<br>
|
||||
* 参数签名为对Map参数按照key的顺序排序后拼接为字符串,然后根据提供的签名算法生成签名字符串<br>
|
||||
* 拼接后的字符串键值对之间无符号,键值对之间无符号,忽略null值
|
||||
*
|
||||
* @param crypto 对称加密算法
|
||||
* @param params 参数
|
||||
* @param otherParams 其它附加参数字符串(例如密钥)
|
||||
* @return 签名
|
||||
* @since 4.0.1
|
||||
*/
|
||||
public static String signParams(final SymmetricCrypto crypto, final Map<?, ?> params, final String... otherParams) {
|
||||
return SignUtil.signParams(crypto, params, otherParams);
|
||||
}
|
||||
|
||||
/**
|
||||
* 对参数做签名<br>
|
||||
* 参数签名为对Map参数按照key的顺序排序后拼接为字符串,然后根据提供的签名算法生成签名字符串
|
||||
*
|
||||
* @param crypto 对称加密算法
|
||||
* @param params 参数
|
||||
* @param separator entry之间的连接符
|
||||
* @param keyValueSeparator kv之间的连接符
|
||||
* @param isIgnoreNull 是否忽略null的键和值
|
||||
* @param otherParams 其它附加参数字符串(例如密钥)
|
||||
* @return 签名
|
||||
* @since 4.0.1
|
||||
*/
|
||||
public static String signParams(final SymmetricCrypto crypto, final Map<?, ?> params, final String separator,
|
||||
final String keyValueSeparator, final boolean isIgnoreNull, final String... otherParams) {
|
||||
return SignUtil.signParams(crypto, params, separator, keyValueSeparator, isIgnoreNull, otherParams);
|
||||
}
|
||||
|
||||
/**
|
||||
* 对参数做md5签名<br>
|
||||
* 参数签名为对Map参数按照key的顺序排序后拼接为字符串,然后根据提供的签名算法生成签名字符串<br>
|
||||
* 拼接后的字符串键值对之间无符号,键值对之间无符号,忽略null值
|
||||
*
|
||||
* @param params 参数
|
||||
* @param otherParams 其它附加参数字符串(例如密钥)
|
||||
* @return 签名
|
||||
* @since 4.0.1
|
||||
*/
|
||||
public static String signParamsMd5(final Map<?, ?> params, final String... otherParams) {
|
||||
return SignUtil.signParamsMd5(params, otherParams);
|
||||
}
|
||||
|
||||
/**
|
||||
* 对参数做Sha1签名<br>
|
||||
* 参数签名为对Map参数按照key的顺序排序后拼接为字符串,然后根据提供的签名算法生成签名字符串<br>
|
||||
* 拼接后的字符串键值对之间无符号,键值对之间无符号,忽略null值
|
||||
*
|
||||
* @param params 参数
|
||||
* @param otherParams 其它附加参数字符串(例如密钥)
|
||||
* @return 签名
|
||||
* @since 4.0.8
|
||||
*/
|
||||
public static String signParamsSha1(final Map<?, ?> params, final String... otherParams) {
|
||||
return SignUtil.signParamsSha1(params, otherParams);
|
||||
}
|
||||
|
||||
/**
|
||||
* 对参数做Sha256签名<br>
|
||||
* 参数签名为对Map参数按照key的顺序排序后拼接为字符串,然后根据提供的签名算法生成签名字符串<br>
|
||||
* 拼接后的字符串键值对之间无符号,键值对之间无符号,忽略null值
|
||||
*
|
||||
* @param params 参数
|
||||
* @param otherParams 其它附加参数字符串(例如密钥)
|
||||
* @return 签名
|
||||
* @since 4.0.1
|
||||
*/
|
||||
public static String signParamsSha256(final Map<?, ?> params, final String... otherParams) {
|
||||
return SignUtil.signParamsSha256(params, otherParams);
|
||||
}
|
||||
|
||||
/**
|
||||
* 对参数做签名<br>
|
||||
* 参数签名为对Map参数按照key的顺序排序后拼接为字符串,然后根据提供的签名算法生成签名字符串<br>
|
||||
* 拼接后的字符串键值对之间无符号,键值对之间无符号,忽略null值
|
||||
*
|
||||
* @param digestAlgorithm 摘要算法
|
||||
* @param params 参数
|
||||
* @param otherParams 其它附加参数字符串(例如密钥)
|
||||
* @return 签名
|
||||
* @since 4.0.1
|
||||
*/
|
||||
public static String signParams(final DigestAlgorithm digestAlgorithm, final Map<?, ?> params, final String... otherParams) {
|
||||
return SignUtil.signParams(digestAlgorithm, params, otherParams);
|
||||
}
|
||||
|
||||
/**
|
||||
* 对参数做签名<br>
|
||||
* 参数签名为对Map参数按照key的顺序排序后拼接为字符串,然后根据提供的签名算法生成签名字符串
|
||||
*
|
||||
* @param digestAlgorithm 摘要算法
|
||||
* @param params 参数
|
||||
* @param separator entry之间的连接符
|
||||
* @param keyValueSeparator kv之间的连接符
|
||||
* @param isIgnoreNull 是否忽略null的键和值
|
||||
* @param otherParams 其它附加参数字符串(例如密钥)
|
||||
* @return 签名
|
||||
* @since 4.0.1
|
||||
*/
|
||||
public static String signParams(final DigestAlgorithm digestAlgorithm, final Map<?, ?> params, final String separator,
|
||||
final String keyValueSeparator, final boolean isIgnoreNull, final String... otherParams) {
|
||||
return SignUtil.signParams(digestAlgorithm, params, separator, keyValueSeparator, isIgnoreNull, otherParams);
|
||||
}
|
||||
|
||||
/**
|
||||
* 增加加密解密的算法提供者,默认优先使用,例如:
|
||||
*
|
||||
@ -1060,26 +575,6 @@ public class SecureUtil {
|
||||
return mac;
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建{@link Signature}
|
||||
*
|
||||
* @param algorithm 算法
|
||||
* @return {@link Signature}
|
||||
* @since 5.7.0
|
||||
*/
|
||||
public static Signature createSignature(final String algorithm) {
|
||||
final Provider provider = GlobalBouncyCastleProvider.INSTANCE.getProvider();
|
||||
|
||||
final Signature signature;
|
||||
try {
|
||||
signature = (null == provider) ? Signature.getInstance(algorithm) : Signature.getInstance(algorithm, provider);
|
||||
} catch (final NoSuchAlgorithmException e) {
|
||||
throw new CryptoException(e);
|
||||
}
|
||||
|
||||
return signature;
|
||||
}
|
||||
|
||||
/**
|
||||
* RC4算法
|
||||
*
|
||||
|
@ -2,12 +2,16 @@ package cn.hutool.crypto;
|
||||
|
||||
import cn.hutool.core.map.MapUtil;
|
||||
import cn.hutool.core.text.StrUtil;
|
||||
import cn.hutool.crypto.asymmetric.AsymmetricAlgorithm;
|
||||
import cn.hutool.crypto.asymmetric.Sign;
|
||||
import cn.hutool.crypto.asymmetric.SignAlgorithm;
|
||||
import cn.hutool.crypto.digest.DigestAlgorithm;
|
||||
import cn.hutool.crypto.digest.Digester;
|
||||
import cn.hutool.crypto.symmetric.SymmetricCrypto;
|
||||
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.security.Provider;
|
||||
import java.security.Signature;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
@ -24,6 +28,37 @@ import java.util.Map;
|
||||
*/
|
||||
public class SignUtil {
|
||||
|
||||
/**
|
||||
* 生成签名对象,仅用于非对称加密
|
||||
*
|
||||
* @param asymmetricAlgorithm {@link AsymmetricAlgorithm} 非对称加密算法
|
||||
* @param digestAlgorithm {@link DigestAlgorithm} 摘要算法
|
||||
* @return {@link Signature}
|
||||
*/
|
||||
public static Signature createSignature(final AsymmetricAlgorithm asymmetricAlgorithm, final DigestAlgorithm digestAlgorithm) {
|
||||
return createSignature(SecureUtil.generateAlgorithm(asymmetricAlgorithm, digestAlgorithm));
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建{@link Signature}签名对象
|
||||
*
|
||||
* @param algorithm 算法
|
||||
* @return {@link Signature}
|
||||
* @since 5.7.0
|
||||
*/
|
||||
public static Signature createSignature(final String algorithm) {
|
||||
final Provider provider = GlobalBouncyCastleProvider.INSTANCE.getProvider();
|
||||
|
||||
final Signature signature;
|
||||
try {
|
||||
signature = (null == provider) ? Signature.getInstance(algorithm) : Signature.getInstance(algorithm, provider);
|
||||
} catch (final NoSuchAlgorithmException e) {
|
||||
throw new CryptoException(e);
|
||||
}
|
||||
|
||||
return signature;
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建签名算法对象<br>
|
||||
* 生成新的私钥公钥对
|
||||
|
@ -2,7 +2,7 @@ package cn.hutool.crypto.asymmetric;
|
||||
|
||||
import cn.hutool.crypto.CryptoException;
|
||||
import cn.hutool.crypto.GlobalBouncyCastleProvider;
|
||||
import cn.hutool.crypto.SecureUtil;
|
||||
import cn.hutool.crypto.KeyUtil;
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
@ -42,7 +42,7 @@ public class RSA extends AsymmetricCrypto {
|
||||
* @return {@link PrivateKey}
|
||||
*/
|
||||
public static PrivateKey generatePrivateKey(final BigInteger modulus, final BigInteger privateExponent) {
|
||||
return SecureUtil.generatePrivateKey(ALGORITHM_RSA.getValue(), new RSAPrivateKeySpec(modulus, privateExponent));
|
||||
return KeyUtil.generatePrivateKey(ALGORITHM_RSA.getValue(), new RSAPrivateKeySpec(modulus, privateExponent));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -53,7 +53,7 @@ public class RSA extends AsymmetricCrypto {
|
||||
* @return {@link PublicKey}
|
||||
*/
|
||||
public static PublicKey generatePublicKey(final BigInteger modulus, final BigInteger publicExponent) {
|
||||
return SecureUtil.generatePublicKey(ALGORITHM_RSA.getValue(), new RSAPublicKeySpec(modulus, publicExponent));
|
||||
return KeyUtil.generatePublicKey(ALGORITHM_RSA.getValue(), new RSAPublicKeySpec(modulus, publicExponent));
|
||||
}
|
||||
// ------------------------------------------------------------------ Static method end
|
||||
|
||||
|
@ -7,7 +7,9 @@ import cn.hutool.core.util.CharsetUtil;
|
||||
import cn.hutool.core.codec.HexUtil;
|
||||
import cn.hutool.core.text.StrUtil;
|
||||
import cn.hutool.crypto.CryptoException;
|
||||
import cn.hutool.crypto.KeyUtil;
|
||||
import cn.hutool.crypto.SecureUtil;
|
||||
import cn.hutool.crypto.SignUtil;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.InputStream;
|
||||
@ -124,8 +126,8 @@ public class Sign extends BaseAsymmetric<Sign> {
|
||||
*/
|
||||
public Sign(final String algorithm, final byte[] privateKey, final byte[] publicKey) {
|
||||
this(algorithm, //
|
||||
SecureUtil.generatePrivateKey(algorithm, privateKey), //
|
||||
SecureUtil.generatePublicKey(algorithm, publicKey)//
|
||||
KeyUtil.generatePrivateKey(algorithm, privateKey), //
|
||||
KeyUtil.generatePublicKey(algorithm, publicKey)//
|
||||
);
|
||||
}
|
||||
|
||||
@ -165,7 +167,7 @@ public class Sign extends BaseAsymmetric<Sign> {
|
||||
*/
|
||||
@Override
|
||||
public Sign init(final String algorithm, final PrivateKey privateKey, final PublicKey publicKey) {
|
||||
signature = SecureUtil.createSignature(algorithm);
|
||||
signature = SignUtil.createSignature(algorithm);
|
||||
super.init(algorithm, privateKey, publicKey);
|
||||
return this;
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
package cn.hutool.crypto.digest.mac;
|
||||
|
||||
import cn.hutool.crypto.CryptoException;
|
||||
import cn.hutool.crypto.KeyUtil;
|
||||
import cn.hutool.crypto.SecureUtil;
|
||||
|
||||
import javax.crypto.Mac;
|
||||
@ -94,7 +95,7 @@ public class DefaultHMacEngine implements MacEngine {
|
||||
try {
|
||||
mac = SecureUtil.createMac(algorithm);
|
||||
if (null == key) {
|
||||
key = SecureUtil.generateKey(algorithm);
|
||||
key = KeyUtil.generateKey(algorithm);
|
||||
}
|
||||
if (null != spec) {
|
||||
mac.init(key, spec);
|
||||
|
@ -1,13 +1,13 @@
|
||||
package cn.hutool.crypto.symmetric;
|
||||
|
||||
import cn.hutool.core.text.StrUtil;
|
||||
import cn.hutool.crypto.KeyUtil;
|
||||
import cn.hutool.crypto.Mode;
|
||||
import cn.hutool.crypto.Padding;
|
||||
|
||||
import javax.crypto.SecretKey;
|
||||
import javax.crypto.spec.IvParameterSpec;
|
||||
|
||||
import cn.hutool.core.text.StrUtil;
|
||||
import cn.hutool.crypto.Mode;
|
||||
import cn.hutool.crypto.Padding;
|
||||
import cn.hutool.crypto.SecureUtil;
|
||||
|
||||
/**
|
||||
* DES加密算法实现<br>
|
||||
* DES全称为Data Encryption Standard,即数据加密标准,是一种使用密钥加密的块算法<br>
|
||||
@ -113,7 +113,7 @@ public class DES extends SymmetricCrypto {
|
||||
* @param key 密钥,长度:8的倍数
|
||||
*/
|
||||
public DES(final String mode, final String padding, final byte[] key) {
|
||||
this(mode, padding, SecureUtil.generateKey("DES", key), null);
|
||||
this(mode, padding, KeyUtil.generateKey("DES", key), null);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -125,7 +125,7 @@ public class DES extends SymmetricCrypto {
|
||||
* @param iv 加盐
|
||||
*/
|
||||
public DES(final String mode, final String padding, final byte[] key, final byte[] iv) {
|
||||
this(mode, padding, SecureUtil.generateKey("DES", key), null == iv ? null : new IvParameterSpec(iv));
|
||||
this(mode, padding, KeyUtil.generateKey("DES", key), null == iv ? null : new IvParameterSpec(iv));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1,13 +1,13 @@
|
||||
package cn.hutool.crypto.symmetric;
|
||||
|
||||
import cn.hutool.core.text.StrUtil;
|
||||
import cn.hutool.crypto.KeyUtil;
|
||||
import cn.hutool.crypto.Mode;
|
||||
import cn.hutool.crypto.Padding;
|
||||
|
||||
import javax.crypto.SecretKey;
|
||||
import javax.crypto.spec.IvParameterSpec;
|
||||
|
||||
import cn.hutool.core.text.StrUtil;
|
||||
import cn.hutool.crypto.Mode;
|
||||
import cn.hutool.crypto.Padding;
|
||||
import cn.hutool.crypto.SecureUtil;
|
||||
|
||||
/**
|
||||
* DESede是由DES对称加密算法改进后的一种对称加密算法,又名3DES、TripleDES。<br>
|
||||
* 使用 168 位的密钥对资料进行三次加密的一种机制;它通常(但非始终)提供极其强大的安全性。<br>
|
||||
@ -126,7 +126,7 @@ public class DESede extends SymmetricCrypto {
|
||||
* @param iv 加盐
|
||||
*/
|
||||
public DESede(final String mode, final String padding, final byte[] key, final byte[] iv) {
|
||||
this(mode, padding, SecureUtil.generateKey(SymmetricAlgorithm.DESede.getValue(), key), null == iv ? null : new IvParameterSpec(iv));
|
||||
this(mode, padding, KeyUtil.generateKey(SymmetricAlgorithm.DESede.getValue(), key), null == iv ? null : new IvParameterSpec(iv));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1,10 +1,10 @@
|
||||
package cn.hutool.crypto.symmetric;
|
||||
|
||||
import cn.hutool.core.util.ArrayUtil;
|
||||
import cn.hutool.core.text.StrUtil;
|
||||
import cn.hutool.core.util.ArrayUtil;
|
||||
import cn.hutool.crypto.KeyUtil;
|
||||
import cn.hutool.crypto.Mode;
|
||||
import cn.hutool.crypto.Padding;
|
||||
import cn.hutool.crypto.SecureUtil;
|
||||
|
||||
import javax.crypto.SecretKey;
|
||||
import javax.crypto.spec.IvParameterSpec;
|
||||
@ -144,7 +144,7 @@ public class SM4 extends SymmetricCrypto{
|
||||
*/
|
||||
public SM4(final String mode, final String padding, final byte[] key, final byte[] iv) {
|
||||
this(mode, padding,//
|
||||
SecureUtil.generateKey(ALGORITHM_NAME, key),//
|
||||
KeyUtil.generateKey(ALGORITHM_NAME, key),//
|
||||
ArrayUtil.isEmpty(iv) ? null : new IvParameterSpec(iv));
|
||||
}
|
||||
|
||||
|
@ -1,6 +1,5 @@
|
||||
package cn.hutool.crypto;
|
||||
|
||||
import cn.hutool.crypto.BCUtil;
|
||||
import org.bouncycastle.crypto.params.ECPrivateKeyParameters;
|
||||
import org.bouncycastle.crypto.params.ECPublicKeyParameters;
|
||||
import org.junit.Assert;
|
||||
@ -9,7 +8,7 @@ import org.junit.Test;
|
||||
public class BCUtilTest {
|
||||
|
||||
/**
|
||||
* 密钥生成来自:https://i.goto327.top/CryptTools/SM2.aspx?tdsourcetag=s_pctim_aiomsg
|
||||
* 密钥生成来自:<a href="https://i.goto327.top/CryptTools/SM2.aspx?tdsourcetag=s_pctim_aiomsg">...</a>
|
||||
*/
|
||||
@Test
|
||||
public void createECPublicKeyParametersTest() {
|
||||
|
@ -1,8 +1,5 @@
|
||||
package cn.hutool.crypto;
|
||||
|
||||
import cn.hutool.crypto.CryptoException;
|
||||
import cn.hutool.crypto.GlobalBouncyCastleProvider;
|
||||
import cn.hutool.crypto.KeyUtil;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
@ -3,7 +3,6 @@ package cn.hutool.crypto;
|
||||
import cn.hutool.core.io.FileUtil;
|
||||
import cn.hutool.core.io.resource.ResourceUtil;
|
||||
import cn.hutool.core.text.StrUtil;
|
||||
import cn.hutool.crypto.PemUtil;
|
||||
import cn.hutool.crypto.asymmetric.KeyType;
|
||||
import cn.hutool.crypto.asymmetric.RSA;
|
||||
import cn.hutool.crypto.asymmetric.SM2;
|
||||
|
@ -1,10 +1,6 @@
|
||||
package cn.hutool.crypto;
|
||||
|
||||
import cn.hutool.core.util.CharsetUtil;
|
||||
import cn.hutool.crypto.KeyUtil;
|
||||
import cn.hutool.crypto.Mode;
|
||||
import cn.hutool.crypto.Padding;
|
||||
import cn.hutool.crypto.SmUtil;
|
||||
import cn.hutool.crypto.digest.HMac;
|
||||
import cn.hutool.crypto.symmetric.SM4;
|
||||
import org.junit.Assert;
|
||||
|
@ -28,7 +28,7 @@ public class SM2Test {
|
||||
|
||||
@Test
|
||||
public void generateKeyPairTest() {
|
||||
final KeyPair pair = SecureUtil.generateKeyPair("SM2");
|
||||
final KeyPair pair = KeyUtil.generateKeyPair("SM2");
|
||||
Assert.assertNotNull(pair.getPrivate());
|
||||
Assert.assertNotNull(pair.getPublic());
|
||||
}
|
||||
@ -37,14 +37,14 @@ public class SM2Test {
|
||||
public void KeyPairOIDTest() {
|
||||
// OBJECT IDENTIFIER 1.2.156.10197.1.301
|
||||
final String OID = "06082A811CCF5501822D";
|
||||
final KeyPair pair = SecureUtil.generateKeyPair("SM2");
|
||||
final KeyPair pair = KeyUtil.generateKeyPair("SM2");
|
||||
Assert.assertTrue(HexUtil.encodeHexStr(pair.getPrivate().getEncoded()).toUpperCase().contains(OID));
|
||||
Assert.assertTrue(HexUtil.encodeHexStr(pair.getPublic().getEncoded()).toUpperCase().contains(OID));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sm2CustomKeyTest() {
|
||||
final KeyPair pair = SecureUtil.generateKeyPair("SM2");
|
||||
final KeyPair pair = KeyUtil.generateKeyPair("SM2");
|
||||
final byte[] privateKey = pair.getPrivate().getEncoded();
|
||||
final byte[] publicKey = pair.getPublic().getEncoded();
|
||||
|
||||
@ -167,7 +167,7 @@ public class SM2Test {
|
||||
public void sm2SignAndVerifyUseKeyTest() {
|
||||
final String content = "我是Hanley.";
|
||||
|
||||
final KeyPair pair = SecureUtil.generateKeyPair("SM2");
|
||||
final KeyPair pair = KeyUtil.generateKeyPair("SM2");
|
||||
|
||||
final SM2 sm2 = new SM2(pair.getPrivate(), pair.getPublic());
|
||||
|
||||
@ -180,7 +180,7 @@ public class SM2Test {
|
||||
public void sm2SignAndVerifyUseKeyTest2() {
|
||||
final String content = "我是Hanley.";
|
||||
|
||||
final KeyPair pair = SecureUtil.generateKeyPair("SM2");
|
||||
final KeyPair pair = KeyUtil.generateKeyPair("SM2");
|
||||
|
||||
final SM2 sm2 = new SM2(//
|
||||
HexUtil.encodeHexStr(pair.getPrivate().getEncoded()), //
|
||||
@ -194,7 +194,7 @@ public class SM2Test {
|
||||
|
||||
@Test
|
||||
public void sm2PublicKeyEncodeDecodeTest() {
|
||||
final KeyPair pair = SecureUtil.generateKeyPair("SM2");
|
||||
final KeyPair pair = KeyUtil.generateKeyPair("SM2");
|
||||
final PublicKey publicKey = pair.getPublic();
|
||||
final byte[] data = KeyUtil.encodeECPublicKey(publicKey);
|
||||
final String encodeHex = HexUtil.encodeHexStr(data);
|
||||
|
@ -2,7 +2,7 @@ package cn.hutool.crypto.asymmetric;
|
||||
|
||||
import cn.hutool.core.map.MapUtil;
|
||||
import cn.hutool.core.text.StrUtil;
|
||||
import cn.hutool.crypto.SecureUtil;
|
||||
import cn.hutool.crypto.SignUtil;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
@ -13,7 +13,6 @@ import java.util.Map;
|
||||
* 签名单元测试
|
||||
*
|
||||
* @author looly
|
||||
*
|
||||
*/
|
||||
public class SignTest {
|
||||
|
||||
@ -22,13 +21,13 @@ public class SignTest {
|
||||
final String content = "我是Hanley.";
|
||||
|
||||
final String privateKey = "MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAJ4fG8vJ0tzu7tjXMSJhyNjlE5B7GkTKMKEQlR6LY3IhIhMFVjuA6W+DqH1VMxl9h3GIM4yCKG2VRZEYEPazgVxa5/ifO8W0pfmrzWCPrddUq4t0Slz5u2lLKymLpPjCzboHoDb8VlF+1HOxjKQckAXq9q7U7dV5VxOzJDuZXlz3AgMBAAECgYABo2LfVqT3owYYewpIR+kTzjPIsG3SPqIIWSqiWWFbYlp/BfQhw7EndZ6+Ra602ecYVwfpscOHdx90ZGJwm+WAMkKT4HiWYwyb0ZqQzRBGYDHFjPpfCBxrzSIJ3QL+B8c8YHq4HaLKRKmq7VUF1gtyWaek87rETWAmQoGjt8DyAQJBAOG4OxsT901zjfxrgKwCv6fV8wGXrNfDSViP1t9r3u6tRPsE6Gli0dfMyzxwENDTI75sOEAfyu6xBlemQGmNsfcCQQCzVWQkl9YUoVDWEitvI5MpkvVKYsFLRXKvLfyxLcY3LxpLKBcEeJ/n5wLxjH0GorhJMmM2Rw3hkjUTJCoqqe0BAkATt8FKC0N2O5ryqv1xiUfuxGzW/cX2jzOwDdiqacTuuqok93fKBPzpyhUS8YM2iss7jj6Xs29JzKMOMxK7ZcpfAkAf21lwzrAu9gEgJhYlJhKsXfjJAAYKUwnuaKLs7o65mtp242ZDWxI85eK1+hjzptBJ4HOTXsfufESFY/VBovIBAkAltO886qQRoNSc0OsVlCi4X1DGo6x2RqQ9EsWPrxWEZGYuyEdODrc54b8L+zaUJLfMJdsCIHEUbM7WXxvFVXNv";
|
||||
Sign sign = SecureUtil.sign(SignAlgorithm.SHA1withRSA, privateKey, null);
|
||||
Sign sign = SignUtil.sign(SignAlgorithm.SHA1withRSA, privateKey, null);
|
||||
Assert.assertNull(sign.getPublicKeyBase64());
|
||||
// 签名
|
||||
final byte[] signed = sign.sign(content.getBytes());
|
||||
|
||||
final String publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCeHxvLydLc7u7Y1zEiYcjY5ROQexpEyjChEJUei2NyISITBVY7gOlvg6h9VTMZfYdxiDOMgihtlUWRGBD2s4FcWuf4nzvFtKX5q81gj63XVKuLdEpc+btpSyspi6T4ws26B6A2/FZRftRzsYykHJAF6vau1O3VeVcTsyQ7mV5c9wIDAQAB";
|
||||
sign = SecureUtil.sign(SignAlgorithm.SHA1withRSA, null, publicKey);
|
||||
sign = SignUtil.sign(SignAlgorithm.SHA1withRSA, null, publicKey);
|
||||
// 验证签名
|
||||
final boolean verify = sign.verify(content.getBytes(), signed);
|
||||
Assert.assertTrue(verify);
|
||||
@ -63,7 +62,7 @@ public class SignTest {
|
||||
*/
|
||||
private void signAndVerify(final SignAlgorithm signAlgorithm) {
|
||||
final byte[] data = StrUtil.utf8Bytes("我是一段测试ab");
|
||||
final Sign sign = SecureUtil.sign(signAlgorithm);
|
||||
final Sign sign = SignUtil.sign(signAlgorithm);
|
||||
|
||||
// 签名
|
||||
final byte[] signed = sign.sign(data);
|
||||
@ -80,7 +79,7 @@ public class SignTest {
|
||||
public void signAndVerifyTest2() {
|
||||
final String str = "wx2421b1c4370ec43b 支付测试 JSAPI支付测试 10000100 1add1a30ac87aa2db72f57a2375d8fec http://wxpay.wxutil.com/pub_v2/pay/notify.v2.php oUpF8uMuAJO_M2pxb1Q9zNjWeS6o 1415659990 14.23.150.211 1 JSAPI 0CB01533B8C1EF103065174F50BCA001";
|
||||
final byte[] data = StrUtil.utf8Bytes(str);
|
||||
final Sign sign = SecureUtil.sign(SignAlgorithm.MD5withRSA);
|
||||
final Sign sign = SignUtil.sign(SignAlgorithm.MD5withRSA);
|
||||
|
||||
// 签名
|
||||
final byte[] signed = sign.sign(data);
|
||||
@ -91,16 +90,16 @@ public class SignTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void signParamsTest(){
|
||||
public void signParamsTest() {
|
||||
final Map<String, String> build = MapUtil.builder(new HashMap<String, String>())
|
||||
.put("key1", "value1")
|
||||
.put("key2", "value2").build();
|
||||
|
||||
final String sign1 = SecureUtil.signParamsSha1(build);
|
||||
final String sign1 = SignUtil.signParamsSha1(build);
|
||||
Assert.assertEquals("9ed30bfe2efbc7038a824b6c55c24a11bfc0dce5", sign1);
|
||||
final String sign2 = SecureUtil.signParamsSha1(build, "12345678");
|
||||
final String sign2 = SignUtil.signParamsSha1(build, "12345678");
|
||||
Assert.assertEquals("944b68d94c952ec178c4caf16b9416b6661f7720", sign2);
|
||||
final String sign3 = SecureUtil.signParamsSha1(build, "12345678", "abc");
|
||||
final String sign3 = SignUtil.signParamsSha1(build, "12345678", "abc");
|
||||
Assert.assertEquals("edee1b477af1b96ebd20fdf08d818f352928d25d", sign3);
|
||||
}
|
||||
|
||||
@ -111,7 +110,7 @@ public class SignTest {
|
||||
public void signAndVerifyPSSTest() {
|
||||
final String str = "wx2421b1c4370ec43b 支付测试 JSAPI支付测试 10000100 1add1a30ac87aa2db72f57a2375d8fec http://wxpay.wxutil.com/pub_v2/pay/notify.v2.php oUpF8uMuAJO_M2pxb1Q9zNjWeS6o 1415659990 14.23.150.211 1 JSAPI 0CB01533B8C1EF103065174F50BCA001";
|
||||
final byte[] data = StrUtil.utf8Bytes(str);
|
||||
final Sign sign = SecureUtil.sign(SignAlgorithm.SHA256withRSA_PSS);
|
||||
final Sign sign = SignUtil.sign(SignAlgorithm.SHA256withRSA_PSS);
|
||||
|
||||
// 签名
|
||||
final byte[] signed = sign.sign(data);
|
||||
|
@ -176,7 +176,7 @@ public class SymmetricTest {
|
||||
public void desTest() {
|
||||
final String content = "test中文";
|
||||
|
||||
final byte[] key = SecureUtil.generateKey(SymmetricAlgorithm.DES.getValue()).getEncoded();
|
||||
final byte[] key = KeyUtil.generateKey(SymmetricAlgorithm.DES.getValue()).getEncoded();
|
||||
|
||||
final SymmetricCrypto des = new SymmetricCrypto(SymmetricAlgorithm.DES, key);
|
||||
final byte[] encrypt = des.encrypt(content);
|
||||
@ -194,7 +194,7 @@ public class SymmetricTest {
|
||||
public void desTest2() {
|
||||
final String content = "test中文";
|
||||
|
||||
final byte[] key = SecureUtil.generateKey(SymmetricAlgorithm.DES.getValue()).getEncoded();
|
||||
final byte[] key = KeyUtil.generateKey(SymmetricAlgorithm.DES.getValue()).getEncoded();
|
||||
|
||||
final DES des = SecureUtil.des(key);
|
||||
final byte[] encrypt = des.encrypt(content);
|
||||
@ -229,7 +229,7 @@ public class SymmetricTest {
|
||||
public void desdeTest() {
|
||||
final String content = "test中文";
|
||||
|
||||
final byte[] key = SecureUtil.generateKey(SymmetricAlgorithm.DESede.getValue()).getEncoded();
|
||||
final byte[] key = KeyUtil.generateKey(SymmetricAlgorithm.DESede.getValue()).getEncoded();
|
||||
|
||||
final DESede des = SecureUtil.desede(key);
|
||||
|
||||
@ -248,7 +248,7 @@ public class SymmetricTest {
|
||||
public void desdeTest2() {
|
||||
final String content = "test中文";
|
||||
|
||||
final byte[] key = SecureUtil.generateKey(SymmetricAlgorithm.DESede.getValue()).getEncoded();
|
||||
final byte[] key = KeyUtil.generateKey(SymmetricAlgorithm.DESede.getValue()).getEncoded();
|
||||
|
||||
final DESede des = new DESede(Mode.CBC, Padding.PKCS5Padding, key, "12345678".getBytes());
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user