add field

This commit is contained in:
Looly 2019-10-05 18:35:54 +08:00
parent a4515e218b
commit 90cd958e37

View File

@ -44,18 +44,34 @@ import cn.hutool.crypto.symmetric.SymmetricAlgorithm;
/**
* 密钥工具类
*
* <p>
* 包括:
* <pre>
* 1生成密钥单密钥密钥对
* 2读取密钥文件
* </pre>
*
* @author looly, Gsealy
* @since 4.4.1
*/
public class KeyUtil {
/** Java密钥库(Java Key StoreJKS)KEY_STORE */
/**
* Java密钥库(Java Key StoreJKS)KEY_STORE
*/
public static final String KEY_TYPE_JKS = "JKS";
/** jceks */
/**
* jceks
*/
public static final String KEY_TYPE_JCEKS = "jceks";
/** PKCS12是公钥加密标准它规定了可包含所有私钥、公钥和证书。其以二进制格式存储也称为 PFX 文件 */
/**
* PKCS12是公钥加密标准它规定了可包含所有私钥公钥和证书其以二进制格式存储也称为 PFX 文件
*/
public static final String KEY_TYPE_PKCS12 = "pkcs12";
public static final String KEY_TYPE_X509 = "X.509";
/**
* Certification类型X.509
*/
public static final String CERT_TYPE_X509 = "X.509";
/**
* 默认密钥字节数
@ -91,7 +107,7 @@ public class KeyUtil {
* 生成 {@link SecretKey}仅用于对称加密和摘要算法密钥生成
*
* @param algorithm 算法支持PBE算法
* @param keySize 密钥长度
* @param keySize 密钥长度
* @return {@link SecretKey}
* @since 3.1.2
*/
@ -112,7 +128,7 @@ public class KeyUtil {
* 生成 {@link SecretKey}仅用于对称加密和摘要算法密钥生成
*
* @param algorithm 算法
* @param key 密钥如果为{@code null} 自动生成随机密钥
* @param key 密钥如果为{@code null} 自动生成随机密钥
* @return {@link SecretKey}
*/
public static SecretKey generateKey(String algorithm, byte[] key) {
@ -135,7 +151,7 @@ public class KeyUtil {
* 生成 {@link SecretKey}
*
* @param algorithm DES算法包括DESDESede等
* @param key 密钥
* @param key 密钥
* @return {@link SecretKey}
*/
public static SecretKey generateDESKey(String algorithm, byte[] key) {
@ -167,7 +183,7 @@ public class KeyUtil {
* 生成PBE {@link SecretKey}
*
* @param algorithm PBE算法包括PBEWithMD5AndDESPBEWithSHA1AndDESedePBEWithSHA1AndRC2_40等
* @param key 密钥
* @param key 密钥
* @return {@link SecretKey}
*/
public static SecretKey generatePBEKey(String algorithm, char[] key) {
@ -186,7 +202,7 @@ public class KeyUtil {
* 生成 {@link SecretKey}仅用于对称加密和摘要算法
*
* @param algorithm 算法
* @param keySpec {@link KeySpec}
* @param keySpec {@link KeySpec}
* @return {@link SecretKey}
*/
public static SecretKey generateKey(String algorithm, KeySpec keySpec) {
@ -217,7 +233,7 @@ public class KeyUtil {
* 算法见https://docs.oracle.com/javase/7/docs/technotes/guides/security/StandardNames.html#KeyFactory
*
* @param algorithm 算法
* @param key 密钥必须为DER编码存储
* @param key 密钥必须为DER编码存储
* @return 私钥 {@link PrivateKey}
*/
public static PrivateKey generatePrivateKey(String algorithm, byte[] key) {
@ -232,7 +248,7 @@ public class KeyUtil {
* 算法见https://docs.oracle.com/javase/7/docs/technotes/guides/security/StandardNames.html#KeyFactory
*
* @param algorithm 算法
* @param keySpec {@link KeySpec}
* @param keySpec {@link KeySpec}
* @return 私钥 {@link PrivateKey}
* @since 3.1.1
*/
@ -252,7 +268,7 @@ public class KeyUtil {
* 生成私钥仅用于非对称加密
*
* @param keyStore {@link KeyStore}
* @param alias 别名
* @param alias 别名
* @param password 密码
* @return 私钥 {@link PrivateKey}
*/
@ -283,7 +299,7 @@ public class KeyUtil {
* 算法见https://docs.oracle.com/javase/7/docs/technotes/guides/security/StandardNames.html#KeyFactory
*
* @param algorithm 算法
* @param key 密钥必须为DER编码存储
* @param key 密钥必须为DER编码存储
* @return 公钥 {@link PublicKey}
*/
public static PublicKey generatePublicKey(String algorithm, byte[] key) {
@ -298,7 +314,7 @@ public class KeyUtil {
* 算法见https://docs.oracle.com/javase/7/docs/technotes/guides/security/StandardNames.html#KeyFactory
*
* @param algorithm 算法
* @param keySpec {@link KeySpec}
* @param keySpec {@link KeySpec}
* @return 公钥 {@link PublicKey}
* @since 3.1.1
*/
@ -330,7 +346,7 @@ public class KeyUtil {
* 密钥对生成算法见https://docs.oracle.com/javase/7/docs/technotes/guides/security/StandardNames.html#KeyPairGenerator
*
* @param algorithm 非对称加密算法
* @param keySize 密钥模modulus 长度
* @param keySize 密钥模modulus 长度
* @return {@link KeyPair}
*/
public static KeyPair generateKeyPair(String algorithm, int keySize) {
@ -342,8 +358,8 @@ public class KeyUtil {
* 密钥对生成算法见https://docs.oracle.com/javase/7/docs/technotes/guides/security/StandardNames.html#KeyPairGenerator
*
* @param algorithm 非对称加密算法
* @param keySize 密钥模modulus 长度
* @param seed 种子
* @param keySize 密钥模modulus 长度
* @param seed 种子
* @return {@link KeyPair}
*/
public static KeyPair generateKeyPair(String algorithm, int keySize, byte[] seed) {
@ -361,7 +377,7 @@ public class KeyUtil {
* 密钥对生成算法见https://docs.oracle.com/javase/7/docs/technotes/guides/security/StandardNames.html#KeyPairGenerator
*
* @param algorithm 非对称加密算法
* @param params {@link AlgorithmParameterSpec}
* @param params {@link AlgorithmParameterSpec}
* @return {@link KeyPair}
* @since 4.3.3
*/
@ -374,8 +390,8 @@ public class KeyUtil {
* 密钥对生成算法见https://docs.oracle.com/javase/7/docs/technotes/guides/security/StandardNames.html#KeyPairGenerator
*
* @param algorithm 非对称加密算法
* @param param {@link AlgorithmParameterSpec}
* @param seed 种子
* @param param {@link AlgorithmParameterSpec}
* @param seed 种子
* @return {@link KeyPair}
* @since 4.3.3
*/
@ -407,9 +423,9 @@ public class KeyUtil {
* </pre>
*
* @param algorithm 非对称加密算法
* @param keySize 密钥模modulus 长度单位bit
* @param seed 种子
* @param params {@link AlgorithmParameterSpec}
* @param keySize 密钥模modulus 长度单位bit
* @param seed 种子
* @param params {@link AlgorithmParameterSpec}
* @return {@link KeyPair}
* @since 4.3.3
*/
@ -441,9 +457,9 @@ public class KeyUtil {
* </pre>
*
* @param algorithm 非对称加密算法
* @param keySize 密钥模modulus 长度单位bit
* @param random {@link SecureRandom} 对象创建时可选传入seed
* @param params {@link AlgorithmParameterSpec}
* @param keySize 密钥模modulus 长度单位bit
* @param random {@link SecureRandom} 对象创建时可选传入seed
* @param params {@link AlgorithmParameterSpec}
* @return {@link KeyPair}
* @since 4.6.5
*/
@ -607,7 +623,7 @@ public class KeyUtil {
* KeyStore文件用于数字证书的密钥对保存<br>
* see: http://snowolf.iteye.com/blog/391931
*
* @param in {@link InputStream} 如果想从文件读取.keystore文件使用 {@link FileUtil#getInputStream(java.io.File)} 读取
* @param in {@link InputStream} 如果想从文件读取.keystore文件使用 {@link FileUtil#getInputStream(java.io.File)} 读取
* @param password 密码
* @return {@link KeyStore}
*/
@ -620,8 +636,8 @@ public class KeyUtil {
* KeyStore文件用于数字证书的密钥对保存<br>
* see: http://snowolf.iteye.com/blog/391931
*
* @param type 类型
* @param in {@link InputStream} 如果想从文件读取.keystore文件使用 {@link FileUtil#getInputStream(java.io.File)} 读取
* @param type 类型
* @param in {@link InputStream} 如果想从文件读取.keystore文件使用 {@link FileUtil#getInputStream(java.io.File)} 读取
* @param password 密码null表示无密码
* @return {@link KeyStore}
*/
@ -639,10 +655,10 @@ public class KeyUtil {
/**
* 从KeyStore中获取私钥公钥
*
* @param type 类型
* @param in {@link InputStream} 如果想从文件读取.keystore文件使用 {@link FileUtil#getInputStream(java.io.File)} 读取
* @param type 类型
* @param in {@link InputStream} 如果想从文件读取.keystore文件使用 {@link FileUtil#getInputStream(java.io.File)} 读取
* @param password 密码
* @param alias 别名
* @param alias 别名
* @return {@link KeyPair}
* @since 4.4.1
*/
@ -656,7 +672,7 @@ public class KeyUtil {
*
* @param keyStore {@link KeyStore}
* @param password 密码
* @param alias 别名
* @param alias 别名
* @return {@link KeyPair}
* @since 4.4.1
*/
@ -677,14 +693,14 @@ public class KeyUtil {
* Certification为证书文件<br>
* see: http://snowolf.iteye.com/blog/391931
*
* @param in {@link InputStream} 如果想从文件读取.cer文件使用 {@link FileUtil#getInputStream(java.io.File)} 读取
* @param in {@link InputStream} 如果想从文件读取.cer文件使用 {@link FileUtil#getInputStream(java.io.File)} 读取
* @param password 密码
* @param alias 别名
* @param alias 别名
* @return {@link KeyStore}
* @since 4.4.1
*/
public static Certificate readX509Certificate(InputStream in, char[] password, String alias) {
return readCertificate(KEY_TYPE_X509, in, password, alias);
return readCertificate(CERT_TYPE_X509, in, password, alias);
}
/**
@ -714,7 +730,7 @@ public class KeyUtil {
* @since 4.4.1
*/
public static Certificate readX509Certificate(InputStream in) {
return readCertificate(KEY_TYPE_X509, in);
return readCertificate(CERT_TYPE_X509, in);
}
/**
@ -722,10 +738,10 @@ public class KeyUtil {
* 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 type 类型例如X.509
* @param in {@link InputStream} 如果想从文件读取.cer文件使用 {@link FileUtil#getInputStream(java.io.File)} 读取
* @param password 密码
* @param alias 别名
* @param alias 别名
* @return {@link KeyStore}
* @since 4.4.1
*/
@ -744,7 +760,7 @@ public class KeyUtil {
* see: http://snowolf.iteye.com/blog/391931
*
* @param type 类型例如X.509
* @param in {@link InputStream} 如果想从文件读取.cer文件使用 {@link FileUtil#getInputStream(java.io.File)} 读取
* @param in {@link InputStream} 如果想从文件读取.cer文件使用 {@link FileUtil#getInputStream(java.io.File)} 读取
* @return {@link Certificate}
*/
public static Certificate readCertificate(String type, InputStream in) {
@ -759,7 +775,7 @@ public class KeyUtil {
* 获得 Certification
*
* @param keyStore {@link KeyStore}
* @param alias 别名
* @param alias 别名
* @return {@link Certificate}
*/
public static Certificate getCertificate(KeyStore keyStore, String alias) {
@ -805,7 +821,7 @@ public class KeyUtil {
* 解码恢复EC压缩公钥,支持Base64和Hex编码,基于BouncyCastle<br>
* https://www.cnblogs.com/xinzhao/p/8963724.html
*
* @param encode 压缩公钥
* @param encode 压缩公钥
* @param curveName EC曲线名
* @since 4.4.4
*/
@ -818,7 +834,7 @@ public class KeyUtil {
* https://www.cnblogs.com/xinzhao/p/8963724.html
*
* @param encodeByte 压缩公钥
* @param curveName EC曲线名
* @param curveName EC曲线名
* @since 4.4.4
*/
public static PublicKey decodeECPoint(byte[] encodeByte, String curveName) {