org.nervousync.utils.SecurityUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of utils-jdk11 Show documentation
Show all versions of utils-jdk11 Show documentation
Java utility collections, development by Nervousync Studio (NSYC)
/*
* Licensed to the Nervousync Studio (NSYC) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.nervousync.utils;
import java.io.*;
import java.nio.ByteOrder;
import java.security.*;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.*;
import java.util.*;
import jcifs.smb.SmbFile;
import jcifs.smb.SmbFileInputStream;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.nervousync.exceptions.utils.DataInvalidException;
import org.nervousync.security.api.SecureAdapter;
import org.nervousync.security.config.CRCConfig;
import org.nervousync.security.config.CipherConfig;
import org.nervousync.security.digest.impl.*;
import org.nervousync.security.crypto.BaseCryptoAdapter;
import org.nervousync.security.crypto.impl.*;
import org.nervousync.enumerations.crypto.CryptoMode;
import org.nervousync.exceptions.crypto.CryptoException;
import org.nervousync.commons.Globals;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
/**
* Security utilities
*
* Current utilities implements features:
* Initialize CRC/Blowfish/MD5/HmacMD5/DES/3DES/SHA/HmacSHA/AES/RSA/SM2/SM3/SM4/RC2/RC4 SecureAdapter
* Calculate CRC/MD5/HmacMD5/SHA/HmacSHA/SM3/HmacSM3/SHAKE128/SHAKE256 result
*
* 安全工具集
*
* 此工具集实现以下功能:
* 初始化CRC/Blowfish/MD5/HmacMD5/DES/3DES/SHA/HmacSHA/AES/RSA/SM2/SM3/SM4/RC2/RC4安全适配器
* 计算CRC/MD5/HmacMD5/SHA/HmacSHA/SM3/HmacSM3/SHAKE128/SHAKE256结果
*
*
* @author Steven Wee [email protected]
* @version $Revision: 1.1.3 $ $Date: Jan 13, 2010 11:23:13 $
*/
public final class SecurityUtils {
/**
* Logger instance
* 日志实例
*/
private static final LoggerUtils.Logger LOGGER = LoggerUtils.getLogger(SecurityUtils.class);
/**
* Registered CRC configure information
* 注册的CRC配置值映射表
*/
private static final Map REGISTERED_CRC_CONFIG = new HashMap<>();
static {
Security.addProvider(new BouncyCastleProvider());
SecurityUtils.registerConfig("CRC-3/GSM",
new CRCConfig(3, 0x3, 0x0, 0x7, Boolean.FALSE, Boolean.FALSE));
SecurityUtils.registerConfig("CRC-3/ROHC",
new CRCConfig(3, 0x3, 0x7, 0x0, Boolean.TRUE, Boolean.TRUE));
SecurityUtils.registerConfig("CRC-4/G-704",
new CRCConfig(4, 0x3, 0x0, 0x0, Boolean.TRUE, Boolean.TRUE));
SecurityUtils.registerConfig("CRC-4/INTERLAKEN",
new CRCConfig(4, 0x3, 0xF, 0xF, Boolean.FALSE, Boolean.FALSE));
SecurityUtils.registerConfig("CRC-5/EPC-C1G2",
new CRCConfig(5, 0x09, 0x09, 0x00, Boolean.FALSE, Boolean.FALSE));
SecurityUtils.registerConfig("CRC-5/G-704",
new CRCConfig(5, 0x15, 0x00, 0x00, Boolean.TRUE, Boolean.TRUE));
SecurityUtils.registerConfig("CRC-5/USB",
new CRCConfig(5, 0x05, 0x1F, 0x1F, Boolean.TRUE, Boolean.TRUE));
SecurityUtils.registerConfig("CRC-6/CDMA2000-A",
new CRCConfig(6, 0x27, 0x3F, 0x00, Boolean.FALSE, Boolean.FALSE));
SecurityUtils.registerConfig("CRC-6/CDMA2000-B",
new CRCConfig(6, 0x07, 0x3F, 0x00, Boolean.FALSE, Boolean.FALSE));
SecurityUtils.registerConfig("CRC-6/DARC",
new CRCConfig(6, 0x19, 0x00, 0x00, Boolean.TRUE, Boolean.TRUE));
SecurityUtils.registerConfig("CRC-6/G-704",
new CRCConfig(6, 0x03, 0x00, 0x00, Boolean.TRUE, Boolean.TRUE));
SecurityUtils.registerConfig("CRC-6/GSM",
new CRCConfig(6, 0x2F, 0x00, 0x3F, Boolean.FALSE, Boolean.FALSE));
SecurityUtils.registerConfig("CRC-7/MMC",
new CRCConfig(7, 0x09, 0x00, 0x00, Boolean.FALSE, Boolean.FALSE));
SecurityUtils.registerConfig("CRC-7/ROHC",
new CRCConfig(7, 0x4F, 0x7F, 0x00, Boolean.TRUE, Boolean.TRUE));
SecurityUtils.registerConfig("CRC-7/UMTS",
new CRCConfig(7, 0x45, 0x00, 0x00, Boolean.FALSE, Boolean.FALSE));
SecurityUtils.registerConfig("CRC-8/AUTOSAR",
new CRCConfig(8, 0x2F, 0xFF, 0xFF, Boolean.FALSE, Boolean.FALSE));
SecurityUtils.registerConfig("CRC-8/BLUETOOTH",
new CRCConfig(8, 0xA7, 0x00, 0x00, Boolean.TRUE, Boolean.TRUE));
SecurityUtils.registerConfig("CRC-8/CDMA2000",
new CRCConfig(8, 0x9B, 0xFF, 0x00, Boolean.FALSE, Boolean.FALSE));
SecurityUtils.registerConfig("CRC-8/DARC",
new CRCConfig(8, 0x39, 0x00, 0x00, Boolean.TRUE, Boolean.TRUE));
SecurityUtils.registerConfig("CRC-8/DVB-S2",
new CRCConfig(8, 0xD5, 0x00, 0x00, Boolean.FALSE, Boolean.FALSE));
SecurityUtils.registerConfig("CRC-8/GSM-A",
new CRCConfig(8, 0x1D, 0x00, 0x00, Boolean.FALSE, Boolean.FALSE));
SecurityUtils.registerConfig("CRC-8/GSM-B",
new CRCConfig(8, 0x49, 0x00, 0xFF, Boolean.FALSE, Boolean.FALSE));
SecurityUtils.registerConfig("CRC-8/I-432-1",
new CRCConfig(8, 0x07, 0x00, 0x55, Boolean.FALSE, Boolean.FALSE));
SecurityUtils.registerConfig("CRC-8/I-CODE",
new CRCConfig(8, 0x1D, 0xFD, 0x00, Boolean.FALSE, Boolean.FALSE));
SecurityUtils.registerConfig("CRC-8/LTE",
new CRCConfig(8, 0x9B, 0x00, 0x00, Boolean.FALSE, Boolean.FALSE));
SecurityUtils.registerConfig("CRC-8/MAXIM-DOW",
new CRCConfig(8, 0x31, 0x00, 0x00, Boolean.TRUE, Boolean.TRUE));
SecurityUtils.registerConfig("CRC-8/MIFARE-MAD",
new CRCConfig(8, 0x1D, 0xC7, 0x00, Boolean.FALSE, Boolean.FALSE));
SecurityUtils.registerConfig("CRC-8/NRSC-5",
new CRCConfig(8, 0x31, 0xFF, 0x00, Boolean.FALSE, Boolean.FALSE));
SecurityUtils.registerConfig("CRC-8/OPENSAFETY",
new CRCConfig(8, 0x2F, 0x00, 0x00, Boolean.FALSE, Boolean.FALSE));
SecurityUtils.registerConfig("CRC-8/ROHC",
new CRCConfig(8, 0x07, 0xFF, 0x00, Boolean.TRUE, Boolean.TRUE));
SecurityUtils.registerConfig("CRC-8/SAE-J1850",
new CRCConfig(8, 0x1D, 0xFF, 0xFF, Boolean.FALSE, Boolean.FALSE));
SecurityUtils.registerConfig("CRC-8/SMBUS",
new CRCConfig(8, 0x07, 0x00, 0x00, Boolean.FALSE, Boolean.FALSE));
SecurityUtils.registerConfig("CRC-8/TECH-3250",
new CRCConfig(8, 0x1D, 0xFF, 0x00, Boolean.TRUE, Boolean.TRUE));
SecurityUtils.registerConfig("CRC-8/WCDMA",
new CRCConfig(8, 0x9B, 0x00, 0x00, Boolean.TRUE, Boolean.TRUE));
SecurityUtils.registerConfig("CRC-10/ATM",
new CRCConfig(10, 0x233, 0x000, 0x000, Boolean.FALSE, Boolean.FALSE));
SecurityUtils.registerConfig("CRC-10/CDMA2000",
new CRCConfig(10, 0x3D9, 0x3FF, 0x000, Boolean.FALSE, Boolean.FALSE));
SecurityUtils.registerConfig("CRC-10/GSM",
new CRCConfig(10, 0x175, 0x000, 0x3FF, Boolean.FALSE, Boolean.FALSE));
SecurityUtils.registerConfig("CRC-11/FLEXRAY",
new CRCConfig(11, 0x385, 0x01A, 0x000, Boolean.FALSE, Boolean.FALSE));
SecurityUtils.registerConfig("CRC-11/UMTS",
new CRCConfig(11, 0x307, 0x000, 0x000, Boolean.FALSE, Boolean.FALSE));
SecurityUtils.registerConfig("CRC-12/CDMA2000",
new CRCConfig(12, 0xF13, 0xFFF, 0x000, Boolean.FALSE, Boolean.FALSE));
SecurityUtils.registerConfig("CRC-12/DECT",
new CRCConfig(12, 0x80F, 0x000, 0x000, Boolean.FALSE, Boolean.FALSE));
SecurityUtils.registerConfig("CRC-12/GSM",
new CRCConfig(12, 0xD31, 0x000, 0xFFF, Boolean.FALSE, Boolean.FALSE));
SecurityUtils.registerConfig("CRC-12/UMTS",
new CRCConfig(12, 0x80F, 0x000, 0x000, Boolean.FALSE, Boolean.TRUE));
SecurityUtils.registerConfig("CRC-13/BBC",
new CRCConfig(13, 0x1CF5, 0x0000, 0x0000, Boolean.FALSE, Boolean.FALSE));
SecurityUtils.registerConfig("CRC-14/DARC",
new CRCConfig(14, 0x0805, 0x0000, 0x0000, Boolean.TRUE, Boolean.TRUE));
SecurityUtils.registerConfig("CRC-14/GSM",
new CRCConfig(14, 0x202D, 0x0000, 0x3FFF, Boolean.FALSE, Boolean.FALSE));
SecurityUtils.registerConfig("CRC-15/CAN",
new CRCConfig(15, 0x4599, 0x0000, 0x0000, Boolean.FALSE, Boolean.FALSE));
SecurityUtils.registerConfig("CRC-15/MPT1327",
new CRCConfig(15, 0x6815, 0x0000, 0x0001, Boolean.FALSE, Boolean.FALSE));
SecurityUtils.registerConfig("CRC-16/ARC",
new CRCConfig(16, 0x8005, 0x0000, 0x0000, Boolean.TRUE, Boolean.TRUE));
SecurityUtils.registerConfig("CRC-16/CDMA2000",
new CRCConfig(16, 0xC867, 0xFFFF, 0x0000, Boolean.FALSE, Boolean.FALSE));
SecurityUtils.registerConfig("CRC-16/CMS",
new CRCConfig(16, 0x8005, 0xFFFF, 0x0000, Boolean.FALSE, Boolean.FALSE));
SecurityUtils.registerConfig("CRC-16/DDS-110",
new CRCConfig(16, 0x8005, 0x800D, 0x0000, Boolean.FALSE, Boolean.FALSE));
SecurityUtils.registerConfig("CRC-16/DECT-R",
new CRCConfig(16, 0x0589, 0x0000, 0x0001, Boolean.FALSE, Boolean.FALSE));
SecurityUtils.registerConfig("CRC-16/DECT-X",
new CRCConfig(16, 0x0589, 0x0000, 0x0000, Boolean.FALSE, Boolean.FALSE));
SecurityUtils.registerConfig("CRC-16/DNP",
new CRCConfig(16, 0x3D65, 0x0000, 0xFFFF, Boolean.TRUE, Boolean.TRUE));
SecurityUtils.registerConfig("CRC-16/EN-13757",
new CRCConfig(16, 0x3D65, 0x0000, 0xFFFF, Boolean.FALSE, Boolean.FALSE));
SecurityUtils.registerConfig("CRC-16/GENIBUS",
new CRCConfig(16, 0x1021, 0xFFFF, 0xFFFF, Boolean.FALSE, Boolean.FALSE));
SecurityUtils.registerConfig("CRC-16/GSM",
new CRCConfig(16, 0x1021, 0x0000, 0xFFFF, Boolean.FALSE, Boolean.FALSE));
SecurityUtils.registerConfig("CRC-16/IBM-3740",
new CRCConfig(16, 0x1021, 0xFFFF, 0x0000, Boolean.FALSE, Boolean.FALSE));
SecurityUtils.registerConfig("CRC-16/IBM-SDLC",
new CRCConfig(16, 0x1021, 0xFFFF, 0xFFFF, Boolean.TRUE, Boolean.TRUE));
SecurityUtils.registerConfig("CRC-16/ISO-IEC-14443-3-A",
new CRCConfig(16, 0x1021, 0xC6C6, 0x0000, Boolean.TRUE, Boolean.TRUE));
SecurityUtils.registerConfig("CRC-16/KERMIT",
new CRCConfig(16, 0x1021, 0x0000, 0x0000, Boolean.TRUE, Boolean.TRUE));
SecurityUtils.registerConfig("CRC-16/LJ1200",
new CRCConfig(16, 0x6F63, 0x0000, 0x0000, Boolean.FALSE, Boolean.FALSE));
SecurityUtils.registerConfig("CRC-16/MAXIM-DOW",
new CRCConfig(16, 0x8005, 0x0000, 0xFFFF, Boolean.TRUE, Boolean.TRUE));
SecurityUtils.registerConfig("CRC-16/MCRF4XX",
new CRCConfig(16, 0x1021, 0xFFFF, 0x0000, Boolean.TRUE, Boolean.TRUE));
SecurityUtils.registerConfig("CRC-16/MODBUS",
new CRCConfig(16, 0x8005, 0xFFFF, 0x0000, Boolean.TRUE, Boolean.TRUE));
SecurityUtils.registerConfig("CRC-16/NRSC-5",
new CRCConfig(16, 0x080B, 0xFFFF, 0x0000, Boolean.TRUE, Boolean.TRUE));
SecurityUtils.registerConfig("CRC-16/OPENSAFETY-A",
new CRCConfig(16, 0x5935, 0x0000, 0x0000, Boolean.FALSE, Boolean.FALSE));
SecurityUtils.registerConfig("CRC-16/OPENSAFETY-B",
new CRCConfig(16, 0x755B, 0x0000, 0x0000, Boolean.FALSE, Boolean.FALSE));
SecurityUtils.registerConfig("CRC-16/PROFIBUS",
new CRCConfig(16, 0x1DCF, 0xFFFF, 0xFFFF, Boolean.FALSE, Boolean.FALSE));
SecurityUtils.registerConfig("CRC-16/RIELLO",
new CRCConfig(16, 0x1021, 0xB2AA, 0x0000, Boolean.TRUE, Boolean.TRUE));
SecurityUtils.registerConfig("CRC-16/SPI-FUJITSU",
new CRCConfig(16, 0x1021, 0x1D0F, 0x0000, Boolean.FALSE, Boolean.FALSE));
SecurityUtils.registerConfig("CRC-16/T10-DIF",
new CRCConfig(16, 0x8BB7, 0x0000, 0x0000, Boolean.FALSE, Boolean.FALSE));
SecurityUtils.registerConfig("CRC-16/TELEDISK",
new CRCConfig(16, 0xA097, 0x0000, 0x0000, Boolean.FALSE, Boolean.FALSE));
SecurityUtils.registerConfig("CRC-16/TMS37157",
new CRCConfig(16, 0x1021, 0x89EC, 0x0000, Boolean.TRUE, Boolean.TRUE));
SecurityUtils.registerConfig("CRC-16/UMTS",
new CRCConfig(16, 0x8005, 0x0000, 0x0000, Boolean.FALSE, Boolean.FALSE));
SecurityUtils.registerConfig("CRC-16/USB",
new CRCConfig(16, 0x8005, 0xFFFF, 0xFFFF, Boolean.TRUE, Boolean.TRUE));
SecurityUtils.registerConfig("CRC-16/XMODEM",
new CRCConfig(16, 0x1021, 0x0000, 0x0000, Boolean.FALSE, Boolean.FALSE));
SecurityUtils.registerConfig("CRC-17/CAN-FD",
new CRCConfig(17, 0x1685B, 0x00000, 0x00000, Boolean.FALSE, Boolean.FALSE));
SecurityUtils.registerConfig("CRC-21/CAN-FD",
new CRCConfig(21, 0x102899, 0x000000, 0x000000, Boolean.FALSE, Boolean.FALSE));
SecurityUtils.registerConfig("CRC-24/BLE",
new CRCConfig(24, 0x00065B, 0x555555, 0x000000, Boolean.TRUE, Boolean.TRUE));
SecurityUtils.registerConfig("CRC-24/FLEXRAY-A",
new CRCConfig(24, 0x5D6DCB, 0xFEDCBA, 0x000000, Boolean.FALSE, Boolean.FALSE));
SecurityUtils.registerConfig("CRC-24/FLEXRAY-B",
new CRCConfig(24, 0x5D6DCB, 0xABCDEF, 0x000000, Boolean.FALSE, Boolean.FALSE));
SecurityUtils.registerConfig("CRC-24/INTERLAKEN",
new CRCConfig(24, 0x328B63, 0xFFFFFF, 0xFFFFFF, Boolean.FALSE, Boolean.FALSE));
SecurityUtils.registerConfig("CRC-24/LTE-A",
new CRCConfig(24, 0x864CFB, 0x000000, 0x000000, Boolean.FALSE, Boolean.FALSE));
SecurityUtils.registerConfig("CRC-24/LTE-B",
new CRCConfig(24, 0x800063, 0x000000, 0x000000, Boolean.FALSE, Boolean.FALSE));
SecurityUtils.registerConfig("CRC-24/OPENPGP",
new CRCConfig(24, 0x864CFB, 0xB704CE, 0x000000, Boolean.FALSE, Boolean.FALSE));
SecurityUtils.registerConfig("CRC-24/OS-9",
new CRCConfig(24, 0x800063, 0xFFFFFF, 0xFFFFFF, Boolean.FALSE, Boolean.FALSE));
SecurityUtils.registerConfig("CRC-30/CDMA",
new CRCConfig(30, 0x2030B9C7, 0x3FFFFFFF, 0x3FFFFFFF, Boolean.FALSE, Boolean.FALSE));
SecurityUtils.registerConfig("CRC-31/PHILIPS",
new CRCConfig(31, 0x04C11DB7, 0x7FFFFFFF, 0x7FFFFFFF, Boolean.FALSE, Boolean.FALSE));
SecurityUtils.registerConfig("CRC-32/AIXM",
new CRCConfig(32, 0x814141ABL, 0x00000000, 0x00000000, Boolean.FALSE, Boolean.FALSE));
SecurityUtils.registerConfig("CRC-32/AUTOSAR",
new CRCConfig(32, 0xF4ACFB13L, 0xFFFFFFFFL, 0xFFFFFFFFL, Boolean.TRUE, Boolean.TRUE));
SecurityUtils.registerConfig("CRC-32/BASE91-D",
new CRCConfig(32, 0xA833982BL, 0xFFFFFFFFL, 0xFFFFFFFFL, Boolean.TRUE, Boolean.TRUE));
SecurityUtils.registerConfig("CRC-32/BZIP2",
new CRCConfig(32, 0x04C11DB7, 0xFFFFFFFFL, 0xFFFFFFFFL, Boolean.FALSE, Boolean.FALSE));
SecurityUtils.registerConfig("CRC-32/CD-ROM-EDC",
new CRCConfig(32, 0x8001801BL, 0x00000000, 0x00000000, Boolean.TRUE, Boolean.TRUE));
SecurityUtils.registerConfig("CRC-32/CKSUM",
new CRCConfig(32, 0x04C11DB7, 0x00000000, 0xFFFFFFFFL, Boolean.FALSE, Boolean.FALSE));
SecurityUtils.registerConfig("CRC-32/ISCSI",
new CRCConfig(32, 0x1EDC6F41, 0xFFFFFFFFL, 0xFFFFFFFFL, Boolean.TRUE, Boolean.TRUE));
SecurityUtils.registerConfig("CRC-32/ISO-HDLC",
new CRCConfig(32, 0x04C11DB7, 0xFFFFFFFFL, 0xFFFFFFFFL, Boolean.TRUE, Boolean.TRUE));
SecurityUtils.registerConfig("CRC-32/JAMCRC",
new CRCConfig(32, 0x04C11DB7, 0xFFFFFFFFL, 0x00000000, Boolean.TRUE, Boolean.TRUE));
SecurityUtils.registerConfig("CRC-32/MPEG-2",
new CRCConfig(32, 0x04C11DB7, 0xFFFFFFFFL, 0x00000000, Boolean.FALSE, Boolean.FALSE));
SecurityUtils.registerConfig("CRC-32/XFER",
new CRCConfig(32, 0x000000AF, 0x00000000, 0x00000000, Boolean.FALSE, Boolean.FALSE));
LOGGER.info("Registered_CRC_Algorithm",
String.join(",", new ArrayList<>(REGISTERED_CRC_CONFIG.keySet())));
}
/**
* Private constructor for SecurityUtils
* 安全工具集的私有构造方法
*/
private SecurityUtils() {
}
/**
* Register CRC configure information
* 注册CRC配置信息
*
* @param algorithm Algorithm name
* 算法名称
* @param crcConfig CRC configure information
* CRC配置信息
*/
public static void registerConfig(final String algorithm, final CRCConfig crcConfig) {
if (StringUtils.isEmpty(algorithm) || crcConfig == null) {
LOGGER.error("Parameter_Null_Register_Security_Error");
return;
}
if (crcConfig.getBit() > 32) {
LOGGER.error("Lager_CRC_Security_Error");
return;
}
if (REGISTERED_CRC_CONFIG.containsKey(algorithm)) {
LOGGER.warn("Override_Config_Register_Security_Warn", algorithm);
}
REGISTERED_CRC_CONFIG.put(algorithm, crcConfig);
}
/**
* Registered CRC algorithm name list
* 已注册的CRC算法名列表
*
* @return CRC algorithm name list
* CRC算法名列表
*/
public static List registeredCRC() {
return new ArrayList<>(REGISTERED_CRC_CONFIG.keySet());
}
/**
* Initialize CRC secure provider
* 初始化CRC安全适配器实例对象
*
* @param algorithm Algorithm name
* 算法名称
*
* @return Initialized secure provider instance
* 初始化的安全适配器实例对象
*
* @throws CryptoException
* If CRC algorithm didn't find
* 如果CRC算法未找到
*/
public static SecureAdapter CRC(final String algorithm) throws CryptoException {
if (REGISTERED_CRC_CONFIG.containsKey(algorithm)) {
return new CRCDigestAdapterImpl(REGISTERED_CRC_CONFIG.get(algorithm));
}
throw new CryptoException(0x00000015000DL, "Unknown_Algorithm_Digits_Error", algorithm);
}
/**
* Retrieve registered CRC configure information
* 查找已注册的CRC配置信息
*
* @param algorithm Algorithm name
* 算法名称
*
* @return CRC configure information instance or null if not found
* 找到的CRC配置信息实例对象,如果未找到返回null
*/
public static CRCConfig crcConfig(final String algorithm) {
return REGISTERED_CRC_CONFIG.get(algorithm);
}
/**
* Convert crc result from byte arrays to string
* 将CRC结果字节数组转换为字符串
*
* @param algorithm Algorithm name
* 算法名称
* @param result CRC result byte array
* CRC结果字节数组
*
* @return Converted string result
* 转换后的结果字符串
*
* @throws CryptoException
* If CRC algorithm didn't find
* 如果CRC算法未找到
*/
public static String CRCResult(final String algorithm, final byte[] result) throws DataInvalidException, CryptoException {
long crc = RawUtils.readLong(result, ByteOrder.LITTLE_ENDIAN);
return Optional.ofNullable(SecurityUtils.crcConfig(algorithm))
.map(crcConfig -> {
StringBuilder stringBuilder = new StringBuilder(Long.toString(crc, 16));
while (stringBuilder.length() < crcConfig.getOutLength()) {
stringBuilder.insert(0, "0");
}
return "0x" + stringBuilder;
})
.orElseThrow(() -> new CryptoException(0x00000015000DL, "Unknown_Algorithm_Digits_Error", algorithm));
}
/*
* Digest Methods
*/
/**
* Initialize MD5 secure provider
* 初始化MD5安全适配器实例对象
*
* @return Initialized secure provider instance
* 初始化的安全适配器实例对象
*
* @throws CryptoException
* If algorithm didn't find
* 如果算法未找到
*/
@Deprecated
public static SecureAdapter MD5() throws CryptoException {
return new MD5DigestAdapterImpl();
}
/**
* Calculate MD5 value of the given source object
* 计算给定的原始数据对象的MD5值
*
* @param source source object
* 原始数据对象
*
* @return Calculate result or zero-length arrays if processes have error
* 计算结果,如果出现错误则返回长度为0的字节数组
*/
@Deprecated
public static byte[] MD5(final Object source) {
try {
return calculate(source, MD5());
} catch (CryptoException e) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Calculate_Value_Security_Error", e, "MD5");
}
return new byte[0];
}
}
/**
* Initialize HmacMD5 secure provider
* 初始化HmacMD5安全适配器实例对象
*
* @param keyBytes HMAC key bytes
* HMAC密钥字节数组
*
* @return Initialized secure provider instance
* 初始化的安全适配器实例对象
*
* @throws CryptoException
* If algorithm didn't find
* 如果算法未找到
*/
public static SecureAdapter HmacMD5(final byte[] keyBytes) throws CryptoException {
return new MD5DigestAdapterImpl(keyBytes);
}
/**
* Calculate HmacMD5 value of the given source object
* 计算给定的原始数据对象的HmacMD5值
*
* @param keyBytes HMAC key bytes
* HMAC密钥字节数组
*
* @param source source object
* 原始数据对象
*
* @return Calculate result or zero-length arrays if processes have error
* 计算结果,如果出现错误则返回长度为0的字节数组
*/
public static byte[] HmacMD5(final byte[] keyBytes, final Object source) {
try {
return calculate(source, HmacMD5(keyBytes));
} catch (CryptoException e) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Calculate_Value_Security_Error", e, "HmacMD5");
}
return new byte[0];
}
}
/**
* Initialize SHA1 secure provider
* 初始化SHA1安全适配器实例对象
*
* @return Initialized secure provider instance
* 初始化的安全适配器实例对象
*
* @throws CryptoException
* If algorithm didn't find
* 如果算法未找到
*/
@Deprecated
public static SecureAdapter SHA1() throws CryptoException {
return new SHA1DigestAdapterImpl();
}
/**
* Calculate SHA1 value of the given source object
* 计算给定的原始数据对象的SHA1值
*
* @param source source object
* 原始数据对象
*
* @return Calculate result or zero-length arrays if processes have error
* 计算结果,如果出现错误则返回长度为0的字节数组
*/
@Deprecated
public static byte[] SHA1(final Object source) {
try {
return calculate(source, SHA1());
} catch (CryptoException e) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Calculate_Value_Security_Error", e, "SHA1");
}
return new byte[0];
}
}
/**
* Initialize HmacSHA1 secure provider
* 初始化HmacSHA1安全适配器实例对象
*
* @param keyBytes HMAC key bytes
* HMAC密钥字节数组
*
* @return Initialized secure provider instance
* 初始化的安全适配器实例对象
*
* @throws CryptoException
* If algorithm didn't find
* 如果算法未找到
*/
public static SecureAdapter HmacSHA1(final byte[] keyBytes) throws CryptoException {
return new SHA1DigestAdapterImpl(keyBytes);
}
/**
* Calculate HmacSHA1 value of the given source object
* 计算给定的原始数据对象的HmacSHA1值
*
* @param keyBytes HMAC key bytes
* HMAC密钥字节数组
* @param source source object
* 原始数据对象
*
* @return Calculate result or zero-length arrays if processes have error
* 计算结果,如果出现错误则返回长度为0的字节数组
*/
public static byte[] HmacSHA1(final byte[] keyBytes, final Object source) {
try {
return calculate(source, HmacSHA1(keyBytes));
} catch (CryptoException e) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Calculate_Value_Security_Error", e, "HmacSHA1");
}
return new byte[0];
}
}
/**
* Initialize SHA-224 secure provider
* 初始化SHA-224安全适配器实例对象
*
* @return Initialized secure provider instance
* 初始化的安全适配器实例对象
*
* @throws CryptoException
* If algorithm didn't find
* 如果算法未找到
*/
public static SecureAdapter SHA224() throws CryptoException {
return new SHA2DigestAdapterImpl("SHA-224", new byte[0]);
}
/**
* Calculate SHA224 value of the given source object
* 计算给定的原始数据对象的SHA224值
*
* @param source source object
* 原始数据对象
*
* @return Calculate result or zero-length arrays if processes have error
* 计算结果,如果出现错误则返回长度为0的字节数组
*/
public static byte[] SHA224(final Object source) {
try {
return calculate(source, SHA224());
} catch (CryptoException e) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Calculate_Value_Security_Error", e, "SHA224");
}
return new byte[0];
}
}
/**
* Initialize HmacSHA224 secure provider
* 初始化HmacSHA224安全适配器实例对象
*
* @param keyBytes HMAC key bytes
* HMAC密钥字节数组
*
* @return Initialized secure provider instance
* 初始化的安全适配器实例对象
*
* @throws CryptoException
* If algorithm didn't find
* 如果算法未找到
*/
public static SecureAdapter HmacSHA224(final byte[] keyBytes) throws CryptoException {
return new SHA2DigestAdapterImpl("SHA-224/HMAC", keyBytes);
}
/**
* Calculate HmacSHA224 value of the given source object
* 计算给定的原始数据对象的HmacSHA224值
*
* @param keyBytes HMAC key bytes
* HMAC密钥字节数组
* @param source source object
* 原始数据对象
*
* @return Calculate result or zero-length arrays if processes have error
* 计算结果,如果出现错误则返回长度为0的字节数组
*/
public static byte[] HmacSHA224(final byte[] keyBytes, final Object source) {
try {
return calculate(source, HmacSHA224(keyBytes));
} catch (CryptoException e) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Calculate_Value_Security_Error", e, "HmacSHA224");
}
return new byte[0];
}
}
/**
* Initialize SHA256 secure provider
* 初始化SHA256安全适配器实例对象
*
* @return Initialized secure provider instance
* 初始化的安全适配器实例对象
*
* @throws CryptoException
* If algorithm didn't find
* 如果算法未找到
*/
public static SecureAdapter SHA256() throws CryptoException {
return new SHA2DigestAdapterImpl("SHA-256", new byte[0]);
}
/**
* Calculate SHA256 value of the given source object
* 计算给定的原始数据对象的SHA256值
*
* @param source source object
* 原始数据对象
*
* @return Calculate result or zero-length arrays if processes have error
* 计算结果,如果出现错误则返回长度为0的字节数组
*/
public static byte[] SHA256(final Object source) {
try {
return calculate(source, SHA256());
} catch (CryptoException e) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Calculate_Value_Security_Error", e, "SHA256");
}
return new byte[0];
}
}
/**
* Initialize HmacSHA256 secure provider
* 初始化HmacSHA256安全适配器实例对象
*
* @param keyBytes HMAC key bytes
* HMAC密钥字节数组
*
* @return Initialized secure provider instance
* 初始化的安全适配器实例对象
*
* @throws CryptoException
* If algorithm didn't find
* 如果算法未找到
*/
public static SecureAdapter HmacSHA256(final byte[] keyBytes) throws CryptoException {
return new SHA2DigestAdapterImpl("SHA-256/HMAC", keyBytes);
}
/**
* Calculate HmacSHA256 value of the given source object
* 计算给定的原始数据对象的HmacSHA256值
*
* @param keyBytes HMAC key bytes
* HMAC密钥字节数组
* @param source source object
* 原始数据对象
*
* @return Calculate result or zero-length arrays if processes have error
* 计算结果,如果出现错误则返回长度为0的字节数组
*/
public static byte[] HmacSHA256(final byte[] keyBytes, final Object source) {
try {
return calculate(source, HmacSHA256(keyBytes));
} catch (CryptoException e) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Calculate_Value_Security_Error", e, "HmacSHA256");
}
return new byte[0];
}
}
/**
* Initialize SHA384 secure provider
* 初始化SHA384安全适配器实例对象
*
* @return Initialized secure provider instance
* 初始化的安全适配器实例对象
*
* @throws CryptoException
* If algorithm didn't find
* 如果算法未找到
*/
public static SecureAdapter SHA384() throws CryptoException {
return new SHA2DigestAdapterImpl("SHA-384", new byte[0]);
}
/**
* Calculate SHA384 value of the given source object
* 计算给定的原始数据对象的SHA384值
*
* @param source source object
* 原始数据对象
*
* @return Calculate result or zero-length arrays if processes have error
* 计算结果,如果出现错误则返回长度为0的字节数组
*/
public static byte[] SHA384(final Object source) {
try {
return calculate(source, SHA384());
} catch (CryptoException e) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Calculate_Value_Security_Error", e, "SHA384");
}
return new byte[0];
}
}
/**
* Initialize HmacSHA384 secure provider
* 初始化HmacSHA384安全适配器实例对象
*
* @param keyBytes HMAC key bytes
* HMAC密钥字节数组
*
* @return Initialized secure provider instance
* 初始化的安全适配器实例对象
*
* @throws CryptoException
* If algorithm didn't find
* 如果算法未找到
*/
public static SecureAdapter HmacSHA384(final byte[] keyBytes) throws CryptoException {
return new SHA2DigestAdapterImpl("SHA-384/HMAC", keyBytes);
}
/**
* Calculate HmacSHA384 value of the given source object
* 计算给定的原始数据对象的HmacSHA384值
*
* @param keyBytes HMAC key bytes
* HMAC密钥字节数组
* @param source source object
* 原始数据对象
*
* @return Calculate result or zero-length arrays if processes have error
* 计算结果,如果出现错误则返回长度为0的字节数组
*/
public static byte[] HmacSHA384(final byte[] keyBytes, final Object source) {
try {
return calculate(source, HmacSHA384(keyBytes));
} catch (CryptoException e) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Calculate_Value_Security_Error", e, "HmacSHA384");
}
return new byte[0];
}
}
/**
* Initialize SHA512 secure provider
* 初始化SHA512安全适配器实例对象
*
* @return Initialized secure provider instance
* 初始化的安全适配器实例对象
*
* @throws CryptoException
* If algorithm didn't find
* 如果算法未找到
*/
public static SecureAdapter SHA512() throws CryptoException {
return new SHA2DigestAdapterImpl("SHA-512", new byte[0]);
}
/**
* Calculate SHA512 value of the given source object
* 计算给定的原始数据对象的SHA512值
*
* @param source source object
* 原始数据对象
*
* @return Calculate result or zero-length arrays if processes have error
* 计算结果,如果出现错误则返回长度为0的字节数组
*/
public static byte[] SHA512(final Object source) {
try {
return calculate(source, SHA512());
} catch (CryptoException e) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Calculate_Value_Security_Error", e, "SHA512");
}
return new byte[0];
}
}
/**
* Initialize HmacSHA512 secure provider
* 初始化HmacSHA512安全适配器实例对象
*
* @param keyBytes HMAC key bytes
* HMAC密钥字节数组
*
* @return Initialized secure provider instance
* 初始化的安全适配器实例对象
*
* @throws CryptoException
* If algorithm didn't find
* 如果算法未找到
*/
public static SecureAdapter HmacSHA512(final byte[] keyBytes) throws CryptoException {
return new SHA2DigestAdapterImpl("SHA-512/HMAC", keyBytes);
}
/**
* Calculate HmacSHA512 value of the given source object
* 计算给定的原始数据对象的HmacSHA512值
*
* @param keyBytes HMAC key bytes
* HMAC密钥字节数组
* @param source source object
* 原始数据对象
*
* @return Calculate result or zero-length arrays if processes have error
* 计算结果,如果出现错误则返回长度为0的字节数组
*/
public static byte[] HmacSHA512(final byte[] keyBytes, final Object source) {
try {
return calculate(source, HmacSHA512(keyBytes));
} catch (CryptoException e) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Calculate_Value_Security_Error", e, "HmacSHA512");
}
return new byte[0];
}
}
/**
* Initialize SHA512/224 secure provider
* 初始化SHA512/224安全适配器实例对象
*
* @return Initialized secure provider instance
* 初始化的安全适配器实例对象
*
* @throws CryptoException
* If algorithm didn't find
* 如果算法未找到
*/
public static SecureAdapter SHA512_224() throws CryptoException {
return new SHA2DigestAdapterImpl("SHA-512/224", new byte[0]);
}
/**
* Calculate SHA512/224 value of the given source object
* 计算给定的原始数据对象的SHA512/224值
*
* @param source source object
* 原始数据对象
*
* @return Calculate result or zero-length arrays if processes have error
* 计算结果,如果出现错误则返回长度为0的字节数组
*/
public static byte[] SHA512_224(final Object source) {
try {
return calculate(source, SHA512_224());
} catch (CryptoException e) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Calculate_Value_Security_Error", e, "SHA512-224");
}
return new byte[0];
}
}
/**
* Initialize HmacSHA512/224 secure provider
* 初始化HmacSHA512/224安全适配器实例对象
*
* @param keyBytes HMAC key bytes
* HMAC密钥字节数组
*
* @return Initialized secure provider instance
* 初始化的安全适配器实例对象
*
* @throws CryptoException
* If algorithm didn't find
* 如果算法未找到
*/
public static SecureAdapter HmacSHA512_224(final byte[] keyBytes) throws CryptoException {
return new SHA2DigestAdapterImpl("SHA-512/224/HMAC", keyBytes);
}
/**
* Calculate HmacSHA512/224 value of the given source object
* 计算给定的原始数据对象的HmacSHA512/224值
*
* @param keyBytes HMAC key bytes
* HMAC密钥字节数组
* @param source source object
* 原始数据对象
*
* @return Calculate result or zero-length arrays if processes have error
* 计算结果,如果出现错误则返回长度为0的字节数组
*/
public static byte[] HmacSHA512_224(final byte[] keyBytes, final Object source) {
try {
return calculate(source, HmacSHA512_224(keyBytes));
} catch (CryptoException e) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Calculate_Value_Security_Error", e, "HmacSHA512-224");
}
return new byte[0];
}
}
/**
* Initialize SHA512/256 secure provider
* 初始化SHA512/256安全适配器实例对象
*
* @return Initialized secure provider instance
* 初始化的安全适配器实例对象
*
* @throws CryptoException
* If algorithm didn't find
* 如果算法未找到
*/
public static SecureAdapter SHA512_256() throws CryptoException {
return new SHA2DigestAdapterImpl("SHA-512/256", new byte[0]);
}
/**
* Calculate SHA512/256 value of the given source object
* 计算给定的原始数据对象的SHA512/256值
*
* @param source source object
* 原始数据对象
*
* @return Calculate result or zero-length arrays if processes have error
* 计算结果,如果出现错误则返回长度为0的字节数组
*/
public static byte[] SHA512_256(final Object source) {
try {
return calculate(source, SHA512_256());
} catch (CryptoException e) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Calculate_Value_Security_Error", e, "SHA512-256");
}
return new byte[0];
}
}
/**
* Initialize HmacSHA512/256 secure provider
* 初始化HmacSHA512/256安全适配器实例对象
*
* @param keyBytes HMAC key bytes
* HMAC密钥字节数组
*
* @return Initialized secure provider instance
* 初始化的安全适配器实例对象
*
* @throws CryptoException
* If algorithm didn't find
* 如果算法未找到
*/
public static SecureAdapter HmacSHA512_256(final byte[] keyBytes) throws CryptoException {
return new SHA2DigestAdapterImpl("SHA-512/256/HMAC", keyBytes);
}
/**
* Calculate HmacSHA512/256 value of the given source object
* 计算给定的原始数据对象的HmacSHA512/256值
*
* @param keyBytes HMAC key bytes
* HMAC密钥字节数组
* @param source source object
* 原始数据对象
*
* @return Calculate result or zero-length arrays if processes have error
* 计算结果,如果出现错误则返回长度为0的字节数组
*/
public static byte[] HmacSHA512_256(final byte[] keyBytes, final Object source) {
try {
return calculate(source, HmacSHA512_256(keyBytes));
} catch (CryptoException e) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Calculate_Value_Security_Error", e, "HmacSHA512-256");
}
return new byte[0];
}
}
/**
* Initialize SHA3-224 secure provider
* 初始化SHA3-224安全适配器实例对象
*
* @return Initialized secure provider instance
* 初始化的安全适配器实例对象
*
* @throws CryptoException
* If algorithm didn't find
* 如果算法未找到
*/
public static SecureAdapter SHA3_224() throws CryptoException {
return new SHA3DigestAdapterImpl("SHA3-224");
}
/**
* Calculate SHA3-224 value of the given source object
* 计算给定的原始数据对象的SHA3-224值
*
* @param source source object
* 原始数据对象
*
* @return Calculate result or zero-length arrays if processes have error
* 计算结果,如果出现错误则返回长度为0的字节数组
*/
public static byte[] SHA3_224(final Object source) {
try {
return calculate(source, SHA3_224());
} catch (CryptoException e) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Calculate_Value_Security_Error", e, "SHA3-224");
}
return new byte[0];
}
}
/**
* Initialize HmacSHA3-224 secure provider
* 初始化HmacSHA3-224安全适配器实例对象
*
* @param keyBytes HMAC key bytes
* HMAC密钥字节数组
*
* @return Initialized secure provider instance
* 初始化的安全适配器实例对象
*
* @throws CryptoException
* If algorithm didn't find
* 如果算法未找到
*/
public static SecureAdapter HmacSHA3_224(final byte[] keyBytes) throws CryptoException {
return new SHA3DigestAdapterImpl("SHA3-224/HMAC", keyBytes);
}
/**
* Calculate HmacSHA3-224 value of the given source object
* 计算给定的原始数据对象的HmacSHA3-224值
*
* @param keyBytes HMAC key bytes
* HMAC密钥字节数组
* @param source source object
* 原始数据对象
*
* @return Calculate result or zero-length arrays if processes have error
* 计算结果,如果出现错误则返回长度为0的字节数组
*/
public static byte[] HmacSHA3_224(final byte[] keyBytes, final Object source) {
try {
return calculate(source, HmacSHA3_224(keyBytes));
} catch (CryptoException e) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Calculate_Value_Security_Error", e, "HmacSHA3-224");
}
return new byte[0];
}
}
/**
* Initialize SHA3-256 secure provider
* 初始化SHA3-256安全适配器实例对象
*
* @return Initialized secure provider instance
* 初始化的安全适配器实例对象
*
* @throws CryptoException
* If algorithm didn't find
* 如果算法未找到
*/
public static SecureAdapter SHA3_256() throws CryptoException {
return new SHA3DigestAdapterImpl("SHA3-256");
}
/**
* Calculate SHA3-256 value of the given source object
* 计算给定的原始数据对象的SHA3-256值
*
* @param source source object
* 原始数据对象
*
* @return Calculate result or zero-length arrays if processes have error
* 计算结果,如果出现错误则返回长度为0的字节数组
*/
public static byte[] SHA3_256(final Object source) {
try {
return calculate(source, SHA3_256());
} catch (CryptoException e) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Calculate_Value_Security_Error", e, "SHA3-256");
}
return new byte[0];
}
}
/**
* Initialize HmacSHA3-256 secure provider
* 初始化HmacSHA3-256安全适配器实例对象
*
* @param keyBytes HMAC key bytes
* HMAC密钥字节数组
*
* @return Initialized secure provider instance
* 初始化的安全适配器实例对象
*
* @throws CryptoException
* If algorithm didn't find
* 如果算法未找到
*/
public static SecureAdapter HmacSHA3_256(final byte[] keyBytes) throws CryptoException {
return new SHA3DigestAdapterImpl("SHA3-256/HMAC", keyBytes);
}
/**
* Calculate HmacSHA3-256 value of the given source object
* 计算给定的原始数据对象的HmacSHA3-256值
*
* @param keyBytes HMAC key bytes
* HMAC密钥字节数组
* @param source source object
* 原始数据对象
*
* @return Calculate result or zero-length arrays if processes have error
* 计算结果,如果出现错误则返回长度为0的字节数组
*/
public static byte[] HmacSHA3_256(final byte[] keyBytes, final Object source) {
try {
return calculate(source, HmacSHA3_256(keyBytes));
} catch (CryptoException e) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Calculate_Value_Security_Error", e, "HmacSHA3-256");
}
return new byte[0];
}
}
/**
* Initialize SHA3-384 secure provider
* 初始化SHA3-384安全适配器实例对象
*
* @return Initialized secure provider instance
* 初始化的安全适配器实例对象
*
* @throws CryptoException
* If algorithm didn't find
* 如果算法未找到
*/
public static SecureAdapter SHA3_384() throws CryptoException {
return new SHA3DigestAdapterImpl("SHA3-384");
}
/**
* Calculate SHA3-384 value of the given source object
* 计算给定的原始数据对象的SHA3-384值
*
* @param source source object
* 原始数据对象
*
* @return Calculate result or zero-length arrays if processes have error
* 计算结果,如果出现错误则返回长度为0的字节数组
*/
public static byte[] SHA3_384(final Object source) {
try {
return calculate(source, SHA3_384());
} catch (CryptoException e) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Calculate_Value_Security_Error", e, "SHA3-384");
}
return new byte[0];
}
}
/**
* Initialize HmacSHA3-384 secure provider
* 初始化HmacSHA3-384安全适配器实例对象
*
* @param keyBytes HMAC key bytes
* HMAC密钥字节数组
*
* @return Initialized secure provider instance
* 初始化的安全适配器实例对象
*
* @throws CryptoException
* If algorithm didn't find
* 如果算法未找到
*/
public static SecureAdapter HmacSHA3_384(final byte[] keyBytes) throws CryptoException {
return new SHA3DigestAdapterImpl("SHA3-384/HMAC", keyBytes);
}
/**
* Calculate HmacSHA3-384 value of the given source object
* 计算给定的原始数据对象的HmacSHA3-384值
*
* @param keyBytes HMAC key bytes
* HMAC密钥字节数组
* @param source source object
* 原始数据对象
*
* @return Calculate result or zero-length arrays if processes have error
* 计算结果,如果出现错误则返回长度为0的字节数组
*/
public static byte[] HmacSHA3_384(final byte[] keyBytes, final Object source) {
try {
return calculate(source, HmacSHA3_384(keyBytes));
} catch (CryptoException e) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Calculate_Value_Security_Error", e, "HmacSHA3-384");
}
return new byte[0];
}
}
/**
* Initialize SHA3-512 secure provider
* 初始化SHA3-512安全适配器实例对象
*
* @return Initialized secure provider instance
* 初始化的安全适配器实例对象
*
* @throws CryptoException
* If algorithm didn't find
* 如果算法未找到
*/
public static SecureAdapter SHA3_512() throws CryptoException {
return new SHA3DigestAdapterImpl("SHA3-512");
}
/**
* Calculate SHA3-512 value of the given source object
* 计算给定的原始数据对象的SHA3-512值
*
* @param source source object
* 原始数据对象
*
* @return Calculate result or zero-length arrays if processes have error
* 计算结果,如果出现错误则返回长度为0的字节数组
*/
public static byte[] SHA3_512(final Object source) {
try {
return calculate(source, SHA3_512());
} catch (CryptoException e) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Calculate_Value_Security_Error", e, "SHA3-512");
}
return new byte[0];
}
}
/**
* Initialize HmacSHA3-512 secure provider
* 初始化HmacSHA3-512安全适配器实例对象
*
* @param keyBytes HMAC key bytes
* HMAC密钥字节数组
*
* @return Initialized secure provider instance
* 初始化的安全适配器实例对象
*
* @throws CryptoException
* If algorithm didn't find
* 如果算法未找到
*/
public static SecureAdapter HmacSHA3_512(final byte[] keyBytes) throws CryptoException {
return new SHA3DigestAdapterImpl("SHA3-512/HMAC", keyBytes);
}
/**
* Calculate HmacSHA3-512 value of the given source object
* 计算给定的原始数据对象的HmacSHA3-512值
*
* @param keyBytes HMAC key bytes
* HMAC密钥字节数组
* @param source source object
* 原始数据对象
*
* @return Calculate result or zero-length arrays if processes have error
* 计算结果,如果出现错误则返回长度为0的字节数组
*/
public static byte[] HmacSHA3_512(final byte[] keyBytes, final Object source) {
try {
return calculate(source, HmacSHA3_512(keyBytes));
} catch (CryptoException e) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Calculate_Value_Security_Error", e, "HmacSHA3-512");
}
return new byte[0];
}
}
/**
* Initialize SHAKE128 secure provider
* 初始化SHAKE128安全适配器实例对象
*
* @return Initialized secure provider instance
* 初始化的安全适配器实例对象
*
* @throws CryptoException
* If algorithm didn't find
* 如果算法未找到
*/
public static SecureAdapter SHAKE128() throws CryptoException {
return new SHA3DigestAdapterImpl("SHAKE128");
}
/**
* Calculate SHAKE128 value of the given source object
* 计算给定的原始数据对象的SHAKE128值
*
* @param source source object
* 原始数据对象
*
* @return Calculate result or zero-length arrays if processes have error
* 计算结果,如果出现错误则返回长度为0的字节数组
*/
public static byte[] SHAKE128(final Object source) {
try {
return calculate(source, SHAKE128());
} catch (CryptoException e) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Calculate_Value_Security_Error", e, "SHAKE128");
}
return new byte[0];
}
}
/**
* Initialize SHAKE256 secure provider
* 初始化SHAKE256安全适配器实例对象
*
* @return Initialized secure provider instance
* 初始化的安全适配器实例对象
*
* @throws CryptoException
* If algorithm didn't find
* 如果算法未找到
*/
public static SecureAdapter SHAKE256() throws CryptoException {
return new SHA3DigestAdapterImpl("SHAKE256");
}
/**
* Calculate SHAKE256 value of the given source object
* 计算给定的原始数据对象的SHAKE256值
*
* @param source source object
* 原始数据对象
*
* @return Calculate result or zero-length arrays if processes have error
* 计算结果,如果出现错误则返回长度为0的字节数组
*/
public static byte[] SHAKE256(final Object source) {
try {
return calculate(source, SHAKE256());
} catch (CryptoException e) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Calculate_Value_Security_Error", e, "SHAKE256");
}
return new byte[0];
}
}
/**
* Initialize SM3 secure provider
* 初始化SM3安全适配器实例对象
*
* @return Initialized secure provider instance
* 初始化的安全适配器实例对象
*
* @throws CryptoException
* If algorithm didn't find
* 如果算法未找到
*/
public static SecureAdapter SM3() throws CryptoException {
return new SM3DigestAdapterImpl();
}
/**
* Calculate SM3 value of the given source object
* 计算给定的原始数据对象的SM3值
*
* @param source source object
* 原始数据对象
*
* @return Calculate result or zero-length arrays if processes have error
* 计算结果,如果出现错误则返回长度为0的字节数组
*/
public static byte[] SM3(final Object source) {
try {
return calculate(source, SM3());
} catch (CryptoException e) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Calculate_Value_Security_Error", e, "SM3");
}
return new byte[0];
}
}
/**
* Initialize HmacSM3 secure provider
* 初始化HmacSM3安全适配器实例对象
*
* @param keyBytes HMAC key bytes
* HMAC密钥字节数组
*
* @return Initialized secure provider instance
* 初始化的安全适配器实例对象
*
* @throws CryptoException
* If algorithm didn't find
* 如果算法未找到
*/
public static SecureAdapter HmacSM3(final byte[] keyBytes) throws CryptoException {
return new SM3DigestAdapterImpl(keyBytes);
}
/**
* Calculate HmacSM3 value of the given source object
* 计算给定的原始数据对象的HmacSM3值
*
* @param keyBytes HMAC key bytes
* HMAC密钥字节数组
* @param source source object
* 原始数据对象
*
* @return Calculate result or zero-length arrays if processes have error
* 计算结果,如果出现错误则返回长度为0的字节数组
*/
public static byte[] HmacSM3(final byte[] keyBytes, final Object source) {
try {
return calculate(source, HmacSM3(keyBytes));
} catch (CryptoException e) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Calculate_Value_Security_Error", e, "HmacSM3");
}
return new byte[0];
}
}
/*
* Symmetric methods
*/
/**
* Initialize Blowfish encryptor secure provider
* 初始化Blowfish加密安全适配器实例对象
*
* @param keyBytes key bytes
* 密钥字节数组
*
* @return Initialized secure provider instance
* 初始化的安全适配器实例对象
*
* @throws CryptoException
* If algorithm didn't find
* 如果算法未找到
*/
public static SecureAdapter BlowfishEncryptor(final byte[] keyBytes) throws CryptoException {
return BlowfishEncryptor("CBC", "NoPadding", keyBytes);
}
/**
* Initialize Blowfish encryptor secure provider
* 初始化Blowfish加密安全适配器实例对象
*
* mode: "ECB", "CBC", "CTR", "CTS", "CFB", "OFB", "CFB8", "OFB8"
* padding: "PKCS5Padding", "PKCS7Padding", "ISO10126Padding", "X9.23Padding"
*
*
* @param mode Cipher Mode
* 分组密码模式
* @param padding Padding Mode
* 数据填充模式
* @param keyBytes key bytes
* 密钥字节数组
*
* @return Initialized secure provider instance
* 初始化的安全适配器实例对象
*
* @throws CryptoException
* If algorithm didn't find
* 如果算法未找到
*/
public static SecureAdapter BlowfishEncryptor(final String mode, final String padding, final byte[] keyBytes)
throws CryptoException {
return new BlowfishCryptoAdapterImpl(new CipherConfig("Blowfish", mode, padding), CryptoMode.ENCRYPT, keyBytes);
}
/**
* Initialize Blowfish encryptor secure provider
* 初始化Blowfish解密安全适配器实例对象
*
* @param keyBytes key bytes
* 密钥字节数组
*
* @return Initialized secure provider instance
* 初始化的安全适配器实例对象
*
* @throws CryptoException
* If algorithm didn't find
* 如果算法未找到
*/
public static SecureAdapter BlowfishDecryptor(final byte[] keyBytes) throws CryptoException {
return BlowfishDecryptor("CBC", "NoPadding", keyBytes);
}
/**
* Initialize Blowfish encryptor secure provider
* 初始化Blowfish解密安全适配器实例对象
*
* mode: "ECB", "CBC", "CTR", "CTS", "CFB", "OFB", "CFB8", "OFB8"
* padding: "PKCS5Padding", "PKCS7Padding", "ISO10126Padding", "X9.23Padding"
*
*
* @param mode Cipher Mode
* 分组密码模式
* @param padding Padding Mode
* 数据填充模式
* @param keyBytes key bytes
* 密钥字节数组
*
* @return Initialized secure provider instance
* 初始化的安全适配器实例对象
*
* @throws CryptoException
* If algorithm didn't find
* 如果算法未找到
*/
public static SecureAdapter BlowfishDecryptor(final String mode, final String padding, final byte[] keyBytes)
throws CryptoException {
return new BlowfishCryptoAdapterImpl(new CipherConfig("Blowfish", mode, padding), CryptoMode.DECRYPT, keyBytes);
}
/**
* Generate Blowfish key bytes
* 生成Blowfish密钥字节数组
*
* @return Generated key bytes or 0 length byte array if process error
* 生成的密钥字节数组,如果出现异常则返回长度为0的字节数组
*/
public static byte[] BlowfishKey() {
try {
return symmetricKey("Blowfish", 56, Globals.DEFAULT_VALUE_STRING);
} catch (CryptoException e) {
return new byte[0];
}
}
/**
* Initialize DES encryptor secure provider
* 初始化DES加密安全适配器实例对象
*
* @param keyBytes key bytes
* 密钥字节数组
*
* @return Initialized secure provider instance
* 初始化的安全适配器实例对象
*
* @throws CryptoException
* If algorithm didn't find
* 如果算法未找到
*/
public static SecureAdapter DESEncryptor(final byte[] keyBytes) throws CryptoException {
return DESEncryptor("CBC", "PKCS5Padding", keyBytes);
}
/**
* Initialize DES encryptor secure provider
* 初始化DES加密安全适配器实例对象
*
* mode: "ECB", "CBC", "CTR", "CTS", "CFB", "OFB", "CFB8", "OFB8"
* padding: "PKCS5Padding", "PKCS7Padding", "ISO10126Padding", "X9.23Padding"
*
*
* @param mode Cipher Mode
* 分组密码模式
* @param padding Padding Mode
* 数据填充模式
* @param keyBytes key bytes
* 密钥字节数组
*
* @return Initialized secure provider instance
* 初始化的安全适配器实例对象
*
* @throws CryptoException
* If algorithm didn't find
* 如果算法未找到
*/
public static SecureAdapter DESEncryptor(final String mode, final String padding, final byte[] keyBytes)
throws CryptoException {
return new DESCryptoAdapterImpl(new CipherConfig("DES", mode, padding), CryptoMode.ENCRYPT, keyBytes);
}
/**
* Initialize DES decryptor secure provider
* 初始化DES解密安全适配器实例对象
*
* @param keyBytes key bytes
* 密钥字节数组
*
* @return Initialized secure provider instance
* 初始化的安全适配器实例对象
*
* @throws CryptoException
* If algorithm didn't find
* 如果算法未找到
*/
public static SecureAdapter DESDecryptor(final byte[] keyBytes) throws CryptoException {
return DESDecryptor("CBC", "PKCS5Padding", keyBytes);
}
/**
* Initialize DES decryptor secure provider
* 初始化DES解密安全适配器实例对象
*
* mode: "ECB", "CBC", "CTR", "CTS", "CFB", "OFB", "CFB8", "OFB8"
* padding: "PKCS5Padding", "PKCS7Padding", "ISO10126Padding", "X9.23Padding"
*
*
* @param mode Cipher Mode
* 分组密码模式
* @param padding Padding Mode
* 数据填充模式
* @param keyBytes key bytes
* 密钥字节数组
*
* @return Initialized secure provider instance
* 初始化的安全适配器实例对象
*
* @throws CryptoException
* If algorithm didn't find
* 如果算法未找到
*/
public static SecureAdapter DESDecryptor(final String mode, final String padding, final byte[] keyBytes)
throws CryptoException {
return new DESCryptoAdapterImpl(new CipherConfig("DES", mode, padding), CryptoMode.DECRYPT, keyBytes);
}
/**
* Generate DES key bytes
* 生成DES密钥字节数组
*
* @return Generated key bytes or 0 length byte array if process error
* 生成的密钥字节数组,如果出现异常则返回长度为0的字节数组
*/
public static byte[] DESKey() {
try {
return symmetricKey("DES", Globals.DEFAULT_VALUE_INT, Globals.DEFAULT_VALUE_STRING);
} catch (CryptoException e) {
return new byte[0];
}
}
/**
* Initialize TripleDES encryptor secure provider
* 初始化3DES加密安全适配器实例对象
*
* @param keyBytes key bytes
* 密钥字节数组
*
* @return Initialized secure provider instance
* 初始化的安全适配器实例对象
*
* @throws CryptoException
* If algorithm didn't find
* 如果算法未找到
*/
public static SecureAdapter TripleDESEncryptor(final byte[] keyBytes) throws CryptoException {
return TripleDESEncryptor("CBC", "PKCS5Padding", keyBytes);
}
/**
* Initialize TripleDES encryptor secure provider
* 初始化3DES加密安全适配器实例对象
*
* mode: "ECB", "CBC", "CTR", "CTS", "CFB", "OFB", "CFB8", "OFB8"
* padding: "PKCS5Padding", "PKCS7Padding", "ISO10126Padding", "X9.23Padding"
*
*
* @param mode Cipher Mode
* 分组密码模式
* @param padding Padding Mode
* 数据填充模式
* @param keyBytes key bytes
* 密钥字节数组
*
* @return Initialized secure provider instance
* 初始化的安全适配器实例对象
*
* @throws CryptoException
* If algorithm didn't find
* 如果算法未找到
*/
public static SecureAdapter TripleDESEncryptor(final String mode, final String padding, final byte[] keyBytes)
throws CryptoException {
return new TripleDESCryptoAdapterImpl(new CipherConfig("DESede", mode, padding),
CryptoMode.ENCRYPT, keyBytes);
}
/**
* Initialize TripleDES decryptor secure provider
* 初始化3DES解密安全适配器实例对象
*
* @param keyBytes key bytes
* 密钥字节数组
*
* @return Initialized secure provider instance
* 初始化的安全适配器实例对象
*
* @throws CryptoException
* If algorithm didn't find
* 如果算法未找到
*/
public static SecureAdapter TripleDESDecryptor(final byte[] keyBytes) throws CryptoException {
return TripleDESDecryptor("CBC", "PKCS5Padding", keyBytes);
}
/**
* Initialize TripleDES decryptor secure provider
* 初始化3DES解密安全适配器实例对象
*
* mode: "ECB", "CBC", "CTR", "CTS", "CFB", "OFB", "CFB8", "OFB8"
* padding: "PKCS5Padding", "PKCS7Padding", "ISO10126Padding", "X9.23Padding"
*
*
* @param mode Cipher Mode
* 分组密码模式
* @param padding Padding Mode
* 数据填充模式
* @param keyBytes key bytes
* 密钥字节数组
*
* @return Initialized secure provider instance
* 初始化的安全适配器实例对象
*
* @throws CryptoException
* If algorithm didn't find
* 如果算法未找到
*/
public static SecureAdapter TripleDESDecryptor(final String mode, final String padding, final byte[] keyBytes)
throws CryptoException {
return new TripleDESCryptoAdapterImpl(new CipherConfig("DESede", mode, padding),
CryptoMode.DECRYPT, keyBytes);
}
/**
* Generate TripleDES key bytes
* 生成3DES密钥字节数组
*
* @return Generated key bytes or 0 length byte array if process error
* 生成的密钥字节数组,如果出现异常则返回长度为0的字节数组
*/
public static byte[] TripleDESKey() {
try {
return symmetricKey("DESede", Globals.DEFAULT_VALUE_INT, Globals.DEFAULT_VALUE_STRING);
} catch (CryptoException e) {
return new byte[0];
}
}
/**
* Initialize SM4 encryptor secure provider
* 初始化SM4加密安全适配器实例对象
*
* @param keyBytes key bytes
* 密钥字节数组
*
* @return Initialized secure provider instance
* 初始化的安全适配器实例对象
*
* @throws CryptoException
* If algorithm didn't find
* 如果算法未找到
*/
public static SecureAdapter SM4Encryptor(final byte[] keyBytes) throws CryptoException {
return SM4Encryptor("CBC", "NoPadding", keyBytes, "SHA1PRNG");
}
/**
* Initialize SM4 encryptor secure provider
* 初始化SM4加密安全适配器实例对象
*
* mode: "ECB", "CBC", "CTR", "CTS", "CFB", "OFB", "CFB8", "OFB8", "CFB128", "OFB128"
* padding: "PKCS5Padding", "PKCS7Padding", "ISO10126Padding", "X9.23Padding"
*
*
* @param mode Cipher Mode
* 分组密码模式
* @param padding Padding Mode
* 数据填充模式
* @param keyBytes key bytes
* 密钥字节数组
*
* @return Initialized secure provider instance
* 初始化的安全适配器实例对象
*
* @throws CryptoException
* If algorithm didn't find
* 如果算法未找到
*/
public static SecureAdapter SM4Encryptor(final String mode, final String padding, final byte[] keyBytes)
throws CryptoException {
return SM4Encryptor(mode, padding, keyBytes, "SHA1PRNG");
}
/**
* Initialize SM4 encryptor secure provider
* 初始化SM4加密安全适配器实例对象
*
* mode: "ECB", "CBC", "CTR", "CTS", "CFB", "OFB", "CFB8", "OFB8", "CFB128", "OFB128"
* padding: "PKCS5Padding", "PKCS7Padding", "ISO10126Padding", "X9.23Padding"
*
*
* @param mode Cipher Mode
* 分组密码模式
* @param padding Padding Mode
* 数据填充模式
* @param keyBytes key bytes
* 密钥字节数组
* @param randomAlgorithm Random algorithm
* 随机数算法
*
* @return Initialized secure provider instance
* 初始化的安全适配器实例对象
*
* @throws CryptoException
* If algorithm didn't find
* 如果算法未找到
*/
public static SecureAdapter SM4Encryptor(final String mode, final String padding, final byte[] keyBytes,
final String randomAlgorithm) throws CryptoException {
return new SM4CryptoAdapterImpl(new CipherConfig("SM4", mode, padding),
CryptoMode.ENCRYPT, keyBytes, randomAlgorithm);
}
/**
* Initialize SM4 decryptor secure provider
* 初始化SM4解密安全适配器实例对象
*
* @param keyBytes key bytes
* 密钥字节数组
*
* @return Initialized secure provider instance
* 初始化的安全适配器实例对象
*
* @throws CryptoException
* If algorithm didn't find
* 如果算法未找到
*/
public static SecureAdapter SM4Decryptor(final byte[] keyBytes) throws CryptoException {
return SM4Decryptor("CBC", "PKCS5Padding", keyBytes, "SHA1PRNG");
}
/**
* Initialize SM4 decryptor secure provider
* 初始化SM4解密安全适配器实例对象
*
* mode: "ECB", "CBC", "CTR", "CTS", "CFB", "OFB", "CFB8", "OFB8", "CFB128", "OFB128"
* padding: "PKCS5Padding", "PKCS7Padding", "ISO10126Padding", "X9.23Padding"
*
*
* @param mode Cipher Mode
* 分组密码模式
* @param padding Padding Mode
* 数据填充模式
* @param keyBytes key bytes
* 密钥字节数组
*
* @return Initialized secure provider instance
* 初始化的安全适配器实例对象
*
* @throws CryptoException
* If algorithm didn't find
* 如果算法未找到
*/
public static SecureAdapter SM4Decryptor(final String mode, final String padding, final byte[] keyBytes)
throws CryptoException {
return SM4Decryptor(mode, padding, keyBytes, "SHA1PRNG");
}
/**
* Initialize SM4 decryptor secure provider
* 初始化SM4解密安全适配器实例对象
*
* mode: "ECB", "CBC", "CTR", "CTS", "CFB", "OFB", "CFB8", "OFB8", "CFB128", "OFB128"
* padding: "PKCS5Padding", "PKCS7Padding", "ISO10126Padding", "X9.23Padding"
*
*
* @param mode Cipher Mode
* 分组密码模式
* @param padding Padding Mode
* 数据填充模式
* @param keyBytes key bytes
* 密钥字节数组
* @param randomAlgorithm Random algorithm
* 随机数算法
*
* @return Initialized secure provider instance
* 初始化的安全适配器实例对象
*
* @throws CryptoException
* If algorithm didn't find
* 如果算法未找到
*/
public static SecureAdapter SM4Decryptor(final String mode, final String padding, final byte[] keyBytes,
final String randomAlgorithm) throws CryptoException {
return new SM4CryptoAdapterImpl(new CipherConfig("SM4", mode, padding),
CryptoMode.DECRYPT, keyBytes, randomAlgorithm);
}
/**
* Generate SM4 key bytes
* 生成SM4密钥字节数组
*
* @return Generated key bytes or 0 length byte array if process error
* 生成的密钥字节数组,如果出现异常则返回长度为0的字节数组
*/
public static byte[] SM4Key() {
try {
return symmetricKey("SM4", 128, Globals.DEFAULT_VALUE_STRING);
} catch (CryptoException e) {
return new byte[0];
}
}
/**
* Initialize RC2 encryptor secure provider
* 初始化RC2加密安全适配器实例对象
*
* @param keyBytes key bytes
* 密钥字节数组
*
* @return Initialized secure provider instance
* 初始化的安全适配器实例对象
*
* @throws CryptoException
* If algorithm didn't find
* 如果算法未找到
*/
public static SecureAdapter RC2Encryptor(final byte[] keyBytes) throws CryptoException {
return RC2Encryptor("CBC", "NoPadding", keyBytes);
}
/**
* Initialize RC2 encryptor secure provider
* 初始化RC2加密安全适配器实例对象
*
* mode: "ECB", "CBC", "CTR", "CTS", "CFB", "OFB", "CFB8", "OFB8"
* padding: "PKCS5Padding", "PKCS7Padding", "ISO10126Padding", "X9.23Padding"
*
*
* @param mode Cipher Mode
* 分组密码模式
* @param padding Padding Mode
* 数据填充模式
* @param keyBytes key bytes
* 密钥字节数组
*
* @return Initialized secure provider instance
* 初始化的安全适配器实例对象
*
* @throws CryptoException
* If algorithm didn't find
* 如果算法未找到
*/
public static SecureAdapter RC2Encryptor(final String mode, final String padding, final byte[] keyBytes)
throws CryptoException {
return new RC2CryptoAdapterImpl(new CipherConfig("RC2", mode, padding), CryptoMode.ENCRYPT, keyBytes);
}
/**
* Initialize RC2 encryptor secure provider
* 初始化RC2解密安全适配器实例对象
*
* @param keyBytes key bytes
* 密钥字节数组
*
* @return Initialized secure provider instance
* 初始化的安全适配器实例对象
*
* @throws CryptoException
* If algorithm didn't find
* 如果算法未找到
*/
public static SecureAdapter RC2Decryptor(final byte[] keyBytes) throws CryptoException {
return RC2Decryptor("CBC", "NoPadding", keyBytes);
}
/**
* Initialize RC2 encryptor secure provider
* 初始化RC2解密安全适配器实例对象
*
* mode: "ECB", "CBC", "CTR", "CTS", "CFB", "OFB", "CFB8", "OFB8"
* padding: "PKCS5Padding", "PKCS7Padding", "ISO10126Padding", "X9.23Padding"
*
*
* @param mode Cipher Mode
* 分组密码模式
* @param padding Padding Mode
* 数据填充模式
* @param keyBytes key bytes
* 密钥字节数组
*
* @return Initialized secure provider instance
* 初始化的安全适配器实例对象
*
* @throws CryptoException
* If algorithm didn't find
* 如果算法未找到
*/
public static SecureAdapter RC2Decryptor(final String mode, final String padding, final byte[] keyBytes)
throws CryptoException {
return new RC2CryptoAdapterImpl(new CipherConfig("RC2", mode, padding), CryptoMode.DECRYPT, keyBytes);
}
/**
* Generate RC2 key bytes
* 生成RC2密钥字节数组
*
* @return Generated key bytes or 0 length byte array if process error
* 生成的密钥字节数组,如果出现异常则返回长度为0的字节数组
*/
public static byte[] RC2Key() {
try {
return symmetricKey("RC2", 128, Globals.DEFAULT_VALUE_STRING);
} catch (CryptoException e) {
return new byte[0];
}
}
/**
* Initialize RC4 encryptor secure provider
* 初始化RC4加密安全适配器实例对象
*
* @param keyBytes key bytes
* 密钥字节数组
*
* @return Initialized secure provider instance
* 初始化的安全适配器实例对象
*
* @throws CryptoException
* If algorithm didn't find
* 如果算法未找到
*/
public static SecureAdapter RC4Encryptor(final byte[] keyBytes) throws CryptoException {
return RC4Encryptor(keyBytes, "SHA1PRNG");
}
/**
* Initialize RC4 encryptor secure provider
* 初始化RC4加密安全适配器实例对象
*
* @param keyBytes key bytes
* 密钥字节数组
* @param randomAlgorithm Random algorithm
* 随机数算法
*
* @return Initialized secure provider instance
* 初始化的安全适配器实例对象
*
* @throws CryptoException
* If algorithm didn't find
* 如果算法未找到
*/
public static SecureAdapter RC4Encryptor(final byte[] keyBytes, final String randomAlgorithm)
throws CryptoException {
return new RC4CryptoAdapterImpl(new CipherConfig("RC4", Globals.DEFAULT_VALUE_STRING, Globals.DEFAULT_VALUE_STRING),
CryptoMode.ENCRYPT, keyBytes, randomAlgorithm);
}
/**
* Initialize RC4 encryptor secure provider
* 初始化RC4解密安全适配器实例对象
*
* @param keyBytes key bytes
* 密钥字节数组
*
* @return Initialized secure provider instance
* 初始化的安全适配器实例对象
*
* @throws CryptoException
* If algorithm didn't find
* 如果算法未找到
*/
public static SecureAdapter RC4Decryptor(final byte[] keyBytes) throws CryptoException {
return RC4Decryptor(keyBytes, "SHA1PRNG");
}
/**
* Initialize RC4 encryptor secure provider
* 初始化RC4解密安全适配器实例对象
*
* @param keyBytes key bytes
* 密钥字节数组
* @param randomAlgorithm Random algorithm
* 随机数算法
*
* @return Initialized secure provider instance
* 初始化的安全适配器实例对象
*
* @throws CryptoException
* If algorithm didn't find
* 如果算法未找到
*/
public static SecureAdapter RC4Decryptor(final byte[] keyBytes, final String randomAlgorithm)
throws CryptoException {
return new RC4CryptoAdapterImpl(new CipherConfig("RC4", Globals.DEFAULT_VALUE_STRING, Globals.DEFAULT_VALUE_STRING),
CryptoMode.DECRYPT, keyBytes, randomAlgorithm);
}
/**
* Generate RC4 key bytes
* 生成RC4密钥字节数组
*
* @return Generated key bytes or 0 length byte array if process error
* 生成的密钥字节数组,如果出现异常则返回长度为0的字节数组
*/
public static byte[] RC4Key() {
try {
return symmetricKey("RC4", 128, Globals.DEFAULT_VALUE_STRING);
} catch (CryptoException e) {
return new byte[0];
}
}
/*
* Asymmetric methods
*/
/**
* Initialize RSA encryptor secure provider
* 初始化RSA加密安全适配器实例对象
*
* @param publicKey Public Key instance
* 公钥证书实例对象
*
* @return Initialized secure provider instance
* 初始化的安全适配器实例对象
*
* @throws CryptoException
* If algorithm didn't find
* 如果算法未找到
*/
public static SecureAdapter RSAEncryptor(final Key publicKey) throws CryptoException {
return RSAEncryptor("PKCS1Padding", publicKey);
}
/**
* Initialize RSA encryptor secure provider
* 初始化RSA加密安全适配器实例对象
*
* padding: "NoPadding", "PKCS1Padding", "OAEPWithSHA-1AndMGF1Padding",
* "OAEPWithSHA-224AndMGF1Padding", "OAEPWithSHA-256AndMGF1Padding",
* "OAEPWithSHA-384AndMGF1Padding", "OAEPWithSHA-512AndMGF1Padding",
* "OAEPWithSHA3-224AndMGF1Padding", "OAEPWithSHA3-256AndMGF1Padding",
* "OAEPWithSHA3-384AndMGF1Padding", "OAEPWithSHA3-512AndMGF1Padding"
*
*
* @param padding Padding Mode
* 数据填充模式
* @param publicKey Public Key instance
* 公钥证书实例对象
*
* @return Initialized secure provider instance
* 初始化的安全适配器实例对象
*
* @throws CryptoException
* If algorithm didn't find
* 如果算法未找到
*/
public static SecureAdapter RSAEncryptor(final String padding, final Key publicKey) throws CryptoException {
return new RSACryptoAdapterImpl(new CipherConfig("RSA", "ECB", padding),
CryptoMode.ENCRYPT, new BaseCryptoAdapter.CipherKey(publicKey));
}
/**
* Initialize RSA decryptor secure provider
* 初始化RSA解密安全适配器实例对象
*
* @param privateKey Private Key instance
* 私钥证书实例对象
*
* @return Initialized secure provider instance
* 初始化的安全适配器实例对象
*
* @throws CryptoException
* If algorithm didn't find
* 如果算法未找到
*/
public static SecureAdapter RSADecryptor(final Key privateKey) throws CryptoException {
return RSADecryptor("PKCS1Padding", privateKey);
}
/**
* Initialize RSA decryptor secure provider
* 初始化RSA解密安全适配器实例对象
*
* padding: "NoPadding", "PKCS1Padding", "OAEPWithSHA-1AndMGF1Padding",
* "OAEPWithSHA-224AndMGF1Padding", "OAEPWithSHA-256AndMGF1Padding",
* "OAEPWithSHA-384AndMGF1Padding", "OAEPWithSHA-512AndMGF1Padding",
* "OAEPWithSHA3-224AndMGF1Padding", "OAEPWithSHA3-256AndMGF1Padding",
* "OAEPWithSHA3-384AndMGF1Padding", "OAEPWithSHA3-512AndMGF1Padding"
*
*
* @param padding Padding Mode
* 数据填充模式
* @param privateKey Private Key instance
* 私钥证书实例对象
*
* @return Initialized secure provider instance
* 初始化的安全适配器实例对象
*
* @throws CryptoException
* If algorithm didn't find
* 如果算法未找到
*/
public static SecureAdapter RSADecryptor(final String padding, final Key privateKey) throws CryptoException {
return new RSACryptoAdapterImpl(new CipherConfig("RSA", "ECB", padding),
CryptoMode.DECRYPT, new BaseCryptoAdapter.CipherKey(privateKey));
}
/**
* Initialize RSA signer secure provider
* 初始化RSA签名安全适配器实例对象
*
* @param privateKey Private Key instance
* 私钥证书实例对象
*
* @return Initialized secure provider instance
* 初始化的安全适配器实例对象
*
* @throws CryptoException
* If algorithm didn't find
* 如果算法未找到
*/
public static SecureAdapter RSASigner(final PrivateKey privateKey) throws CryptoException {
return RSASigner("SHA256withRSA", privateKey);
}
/**
* Initialize RSA signer secure provider
* 初始化RSA签名安全适配器实例对象
*
* @param algorithm Signature algorithm name
* 签名算法名称
* @param privateKey Private Key instance
* 私钥证书实例对象
*
* @return Initialized secure provider instance
* 初始化的安全适配器实例对象
*
* @throws CryptoException
* If algorithm didn't find
* 如果算法未找到
*/
public static SecureAdapter RSASigner(final String algorithm, final PrivateKey privateKey) throws CryptoException {
return new RSACryptoAdapterImpl(
new CipherConfig(algorithm, Globals.DEFAULT_VALUE_STRING, Globals.DEFAULT_VALUE_STRING),
CryptoMode.SIGNATURE, new BaseCryptoAdapter.CipherKey(privateKey));
}
/**
* Initialize RSA signature verifier secure provider
* 初始化RSA签名验证安全适配器实例对象
*
* @param publicKey Public Key instance
* 公钥证书实例对象
*
* @return Initialized secure provider instance
* 初始化的安全适配器实例对象
*
* @throws CryptoException
* If algorithm didn't find
* 如果算法未找到
*/
public static SecureAdapter RSAVerifier(final PublicKey publicKey) throws CryptoException {
return RSAVerifier("SHA256withRSA", publicKey);
}
/**
* Initialize RSA signature verifier secure provider
* 初始化RSA签名验证安全适配器实例对象
*
* @param algorithm Signature algorithm name
* 签名算法名称
* @param publicKey Public Key instance
* 公钥证书实例对象
*
* @return Initialized secure provider instance
* 初始化的安全适配器实例对象
*
* @throws CryptoException
* If algorithm didn't find
* 如果算法未找到
*/
public static SecureAdapter RSAVerifier(final String algorithm, final PublicKey publicKey) throws CryptoException {
return new RSACryptoAdapterImpl(
new CipherConfig(algorithm, Globals.DEFAULT_VALUE_STRING, Globals.DEFAULT_VALUE_STRING),
CryptoMode.VERIFY, new BaseCryptoAdapter.CipherKey(publicKey));
}
/**
* Generate RSA KeyPair
* 生成RSA密钥对
*
* @return Generated keypair
* 生成的密钥对
*/
public static KeyPair RSAKeyPair() {
return RSAKeyPair(1024, "SHA1PRNG");
}
/**
* Generate RSA KeyPair
* 生成RSA密钥对
*
* @param keySize Key size
* 密钥长度
*
* @return Generated keypair
* 生成的密钥对
*/
public static KeyPair RSAKeyPair(final int keySize) {
return RSAKeyPair(keySize, "SHA1PRNG");
}
/**
* Generate RSA KeyPair
* 生成RSA密钥对
*
* @param keySize Key size
* 密钥长度
* @param randomAlgorithm Random algorithm
* 随机数算法
*
* @return Generated keypair
* 生成的密钥对
*/
public static KeyPair RSAKeyPair(final int keySize, final String randomAlgorithm) {
return CertificateUtils.keyPair("RSA", randomAlgorithm, keySize);
}
/**
* Initialize SM2 encryptor secure provider
* 初始化SM2加密安全适配器实例对象
*
* @param publicKey Public Key instance
* 公钥证书实例对象
*
* @return Initialized secure provider instance
* 初始化的安全适配器实例对象
*
* @throws CryptoException
* If algorithm didn't find
* 如果算法未找到
*/
public static SecureAdapter SM2Encryptor(final PublicKey publicKey) throws CryptoException {
return new SM2CryptoAdapterImpl(
new CipherConfig("SM2", Globals.DEFAULT_VALUE_STRING, Globals.DEFAULT_VALUE_STRING),
CryptoMode.ENCRYPT, new BaseCryptoAdapter.CipherKey(publicKey));
}
/**
* Initialize SM2 decryptor secure provider
* 初始化SM2解密安全适配器实例对象
*
* @param privateKey Private Key instance
* 私钥证书实例对象
*
* @return Initialized secure provider instance
* 初始化的安全适配器实例对象
*
* @throws CryptoException
* If algorithm didn't find
* 如果算法未找到
*/
public static SecureAdapter SM2Decryptor(final PrivateKey privateKey) throws CryptoException {
return new SM2CryptoAdapterImpl(
new CipherConfig("SM2", Globals.DEFAULT_VALUE_STRING, Globals.DEFAULT_VALUE_STRING),
CryptoMode.DECRYPT, new BaseCryptoAdapter.CipherKey(privateKey));
}
/**
* Initialize SM2 signer secure provider
* 初始化SM2签名安全适配器实例对象
*
* @param privateKey Private Key instance
* 私钥证书实例对象
*
* @return Initialized secure provider instance
* 初始化的安全适配器实例对象
*
* @throws CryptoException
* If algorithm didn't find
* 如果算法未找到
*/
public static SecureAdapter SM2Signer(final PrivateKey privateKey) throws CryptoException {
return new SM2CryptoAdapterImpl(
new CipherConfig("SM3withSM2", Globals.DEFAULT_VALUE_STRING, Globals.DEFAULT_VALUE_STRING),
CryptoMode.SIGNATURE, new BaseCryptoAdapter.CipherKey(privateKey));
}
/**
* Initialize SM2 signature verifier secure provider
* 初始化SM2签名验证安全适配器实例对象
*
* @param publicKey Public Key instance
* 公钥证书实例对象
*
* @return Initialized secure provider instance
* 初始化的安全适配器实例对象
*
* @throws CryptoException
* If algorithm didn't find
* 如果算法未找到
*/
public static SecureAdapter SM2Verifier(final PublicKey publicKey) throws CryptoException {
return new SM2CryptoAdapterImpl(
new CipherConfig("SM3withSM2", Globals.DEFAULT_VALUE_STRING, Globals.DEFAULT_VALUE_STRING),
CryptoMode.VERIFY, new BaseCryptoAdapter.CipherKey(publicKey));
}
/**
* Generate SM2 KeyPair
* 生成SM2密钥对
*
* @return Generated keypair
* 生成的密钥对
*/
public static KeyPair SM2KeyPair() {
return SM2KeyPair("SHA1PRNG");
}
/**
* Generate SM2 KeyPair
* 生成SM2密钥对
*
* @param randomAlgorithm Random algorithm
* 随机数算法
*
* @return Generated keypair
* 生成的密钥对
*/
public static KeyPair SM2KeyPair(final String randomAlgorithm) {
return CertificateUtils.keyPair("EC", randomAlgorithm, Globals.INITIALIZE_INT_VALUE);
}
/**
* Convert data bytes from C1|C2|C3 to C1|C3|C2
* 转换字节数组从C1|C2|C3到C1|C3|C2
*
* @param dataBytes C1|C2|C3 data bytes
* C1|C2|C3格式字节数组
*
* @return C1|C3|C2 data bytes
* C1|C3|C2格式字节数组
*/
public static byte[] C1C2C3toC1C3C2(final byte[] dataBytes) {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(dataBytes.length);
byteArrayOutputStream.write(dataBytes, 0, 65);
byteArrayOutputStream.write(dataBytes, 97, dataBytes.length - 97);
byteArrayOutputStream.write(dataBytes, 65, 32);
return byteArrayOutputStream.toByteArray();
}
/**
* Convert data bytes from C1|C3|C2 to C1|C2|C3
* 转换字节数组从C1|C2|C3到C1|C3|C2
*
* @param dataBytes C1|C3|C2 data bytes
* C1|C3|C2格式字节数组
*
* @return C1|C2|C3 data bytes
* C1|C2|C3格式字节数组
*/
public static byte[] C1C3C2toC1C2C3(final byte[] dataBytes) {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(dataBytes.length);
byteArrayOutputStream.write(dataBytes, 0, 65);
byteArrayOutputStream.write(dataBytes, dataBytes.length - 32, 32);
byteArrayOutputStream.write(dataBytes, 65, dataBytes.length - 97);
return byteArrayOutputStream.toByteArray();
}
/**
* Initialize AES encryptor secure provider
* 初始化AES加密安全适配器实例对象
*
* @param keyBytes key bytes
* 密钥字节数组
*
* @return Initialized secure provider instance
* 初始化的安全适配器实例对象
*
* @throws CryptoException
* If algorithm didn't find
* 如果算法未找到
*/
public static SecureAdapter AESEncryptor(final byte[] keyBytes) throws CryptoException {
return AESEncryptor("CBC", "PKCS5Padding", keyBytes);
}
/**
* Initialize AES encryptor secure provider
* 初始化AES加密安全适配器实例对象
*
* mode: "ECB", "CBC", "CTR", "CTS", "CFB", "OFB", "CFB8", "OFB8", "CFB128", "OFB128"
* padding: "PKCS5Padding", "PKCS7Padding", "ISO10126Padding", "X9.23Padding"
*
*
* @param mode Cipher Mode
* 分组密码模式
* @param padding Padding Mode
* 数据填充模式
* @param keyBytes key bytes
* 密钥字节数组
*
* @return Initialized secure provider instance
* 初始化的安全适配器实例对象
*
* @throws CryptoException
* If algorithm didn't find
* 如果算法未找到
*/
public static SecureAdapter AESEncryptor(final String mode, final String padding, final byte[] keyBytes)
throws CryptoException {
return new AESCryptoAdapterImpl(new CipherConfig("AES", mode, padding), CryptoMode.ENCRYPT, keyBytes);
}
/**
* Initialize AES decryptor secure provider
* 初始化AES解密安全适配器实例对象
*
* @param keyBytes key bytes
* 密钥字节数组
*
* @return Initialized secure provider instance
* 初始化的安全适配器实例对象
*
* @throws CryptoException
* If algorithm didn't find
* 如果算法未找到
*/
public static SecureAdapter AESDecryptor(final byte[] keyBytes) throws CryptoException {
return AESDecryptor("CBC", "PKCS5Padding", keyBytes);
}
/**
* Initialize AES decryptor secure provider
* 初始化AES解密安全适配器实例对象
*
* mode: "ECB", "CBC", "CTR", "CTS", "CFB", "OFB", "CFB8", "OFB8", "CFB128", "OFB128"
* padding: "PKCS5Padding", "PKCS7Padding", "ISO10126Padding", "X9.23Padding"
*
*
* @param mode Cipher Mode
* 分组密码模式
* @param padding Padding Mode
* 数据填充模式
* @param keyBytes key bytes
* 密钥字节数组
*
* @return Initialized secure provider instance
* 初始化的安全适配器实例对象
*
* @throws CryptoException
* If algorithm didn't find
* 如果算法未找到
*/
public static SecureAdapter AESDecryptor(final String mode, final String padding, final byte[] keyBytes)
throws CryptoException {
return new AESCryptoAdapterImpl(new CipherConfig("AES", mode, padding), CryptoMode.DECRYPT, keyBytes);
}
/*
* Key generators
*/
/**
* Generate AES128 key bytes
* 生成AES128密钥字节数组
*
* @return Generated key bytes or 0 length byte array if process error
* 生成的密钥字节数组,如果出现异常则返回长度为0的字节数组
*/
public static byte[] AES128Key() {
return AES128Key("SHA1PRNG");
}
/**
* Generate AES128 key bytes
* 生成AES128密钥字节数组
*
* @param randomAlgorithm Random algorithm
* 随机数算法
*
* @return Generated key bytes or 0 length byte array if process error
* 生成的密钥字节数组,如果出现异常则返回长度为0的字节数组
*/
public static byte[] AES128Key(final String randomAlgorithm) {
try {
return symmetricKey("AES", 128, randomAlgorithm);
} catch (CryptoException e) {
return new byte[0];
}
}
/**
* Generate AES192 key bytes
* 生成AES192密钥字节数组
*
* @return Generated key bytes or 0 length byte array if process error
* 生成的密钥字节数组,如果出现异常则返回长度为0的字节数组
*/
public static byte[] AES192Key() {
return AES192Key("SHA1PRNG");
}
/**
* Generate AES192 key bytes
* 生成AES192密钥字节数组
*
* @param randomAlgorithm Random algorithm
* 随机数算法
*
* @return Generated key bytes or 0 length byte array if process error
* 生成的密钥字节数组,如果出现异常则返回长度为0的字节数组
*/
public static byte[] AES192Key(final String randomAlgorithm) {
try {
return symmetricKey("AES", 192, randomAlgorithm);
} catch (CryptoException e) {
return new byte[0];
}
}
/**
* Generate AES256 key bytes
* 生成AES256密钥字节数组
*
* @return Generated key bytes or 0 length byte array if process error
* 生成的密钥字节数组,如果出现异常则返回长度为0的字节数组
*/
public static byte[] AES256Key() {
return AES256Key("SHA1PRNG");
}
/**
* Generate AES256 key bytes
* 生成AES256密钥字节数组
*
* @param randomAlgorithm Random algorithm
* 随机数算法
*
* @return Generated key bytes or 0 length byte array if process error
* 生成的密钥字节数组,如果出现异常则返回长度为0的字节数组
*/
public static byte[] AES256Key(final String randomAlgorithm) {
try {
return symmetricKey("AES", 256, randomAlgorithm);
} catch (CryptoException e) {
return new byte[0];
}
}
/**
* Retrieve RSA Key Size
* 读取RSA密钥长度
*
* @param key RSA key instance
* RSA密钥实例对象
*
* @return Retrieve key size
* 读取的密钥长度
*/
public static int rsaKeySize(final Key key) {
if (key == null) {
return Globals.DEFAULT_VALUE_INT;
}
try {
if (key instanceof PrivateKey) {
return KeyFactory.getInstance("RSA").getKeySpec(key, RSAPrivateKeySpec.class).getModulus().toString(2).length();
} else if (key instanceof RSAPublicKey) {
return KeyFactory.getInstance("RSA").getKeySpec(key, RSAPublicKeySpec.class).getModulus().toString(2).length();
}
return Globals.DEFAULT_VALUE_INT;
} catch (NoSuchAlgorithmException | InvalidKeySpecException ignored) {
return Globals.DEFAULT_VALUE_INT;
}
}
/**
* Generate symmetric key bytes
* 生成对称加密密钥字节数组
*
* @param algorithm Algorithm name
* 算法名称
* @param keySize Key size
* 密钥长度
* @param randomAlgorithm Random algorithm
* 随机数算法
*
* @return Generated key bytes or 0 length byte array if process error
* 生成的密钥字节数组,如果出现异常则返回长度为0的字节数组
*
* @throws CryptoException
* If algorithm didn't find
* 如果算法未找到
*/
public static byte[] symmetricKey(final String algorithm, final int keySize, final String randomAlgorithm)
throws CryptoException {
if (StringUtils.isEmpty(algorithm)) {
throw new CryptoException(0x00000015000DL, "Unknown_Algorithm_Digits_Error", algorithm);
}
try {
KeyGenerator keyGenerator = KeyGenerator.getInstance(algorithm, "BC");
switch (algorithm.toUpperCase()) {
case "AES":
case "RC4":
keyGenerator.init(keySize, SecureRandom.getInstance(randomAlgorithm));
break;
case "SM4":
case "RC2":
keyGenerator.init(keySize, new SecureRandom());
break;
case "DES":
case "DESEDE":
case "BLOWFISH":
break;
default:
throw new CryptoException(0x00000015000DL, "Unknown_Algorithm_Digits_Error", algorithm);
}
SecretKey secretKey = keyGenerator.generateKey();
return secretKey.getEncoded();
} catch (NoSuchAlgorithmException | NoSuchProviderException e) {
throw new CryptoException(0x000000150009L, "Init_Key_Crypto_Error", e);
}
}
/**
* Calculate digest result of given object using given secure adapter
* 使用给定的安全适配器计算给定数据的计算结果
*
* @param source source object
* 原始数据对象
* @param secureAdapter Secure provider instance
* 安全适配器实例对象
*
* @return Calculate result or zero-length arrays if processes have error
* 计算结果,如果出现错误则返回长度为0的字节数组
*
* @throws CryptoException
* If algorithm didn't find
* 如果算法未找到
*/
private static byte[] calculate(final Object source, final SecureAdapter secureAdapter) throws CryptoException {
if (source instanceof File) {
try (InputStream inputStream = new FileInputStream((File) source)) {
byte[] readBuffer = new byte[Globals.READ_FILE_BUFFER_SIZE];
int readLength;
while ((readLength = inputStream.read(readBuffer)) > 0) {
secureAdapter.append(readBuffer, 0, readLength);
}
} catch (Exception e) {
LOGGER.error("Calculate_Digits_Security_Error", e);
return new byte[0];
}
return secureAdapter.finish();
} else if (source instanceof SmbFile) {
try (InputStream inputStream = new SmbFileInputStream((SmbFile) source)) {
byte[] readBuffer = new byte[Globals.READ_FILE_BUFFER_SIZE];
int readLength;
while ((readLength = inputStream.read(readBuffer)) > 0) {
secureAdapter.append(readBuffer, 0, readLength);
}
} catch (Exception e) {
LOGGER.error("Calculate_Digits_Security_Error", e);
return new byte[0];
}
return secureAdapter.finish();
} else {
return secureAdapter.finish(ConvertUtils.toByteArray(source));
}
}
}