use of cn.hutool.crypto.CryptoException in project hutool by looly.
the class RC4 method setKey.
/**
* 设置密钥
*
* @param key 密钥
* @throws CryptoException key长度小于5或者大于255抛出此异常
*/
public void setKey(String key) throws CryptoException {
final int length = key.length();
if (length < KEY_MIN_LENGTH || length >= SBOX_LENGTH) {
throw new CryptoException("Key length has to be between {} and {}", KEY_MIN_LENGTH, (SBOX_LENGTH - 1));
}
final WriteLock writeLock = this.lock.writeLock();
writeLock.lock();
try {
this.sbox = initSBox(StrUtil.utf8Bytes(key));
} finally {
writeLock.unlock();
}
}
use of cn.hutool.crypto.CryptoException in project hutool by looly.
the class MacEngine method digest.
/**
* 生成摘要
*
* @param data {@link InputStream} 数据流
* @param bufferLength 缓存长度,不足1使用 {@link IoUtil#DEFAULT_BUFFER_SIZE} 做为默认值
* @return 摘要bytes
*/
default byte[] digest(InputStream data, int bufferLength) {
if (bufferLength < 1) {
bufferLength = IoUtil.DEFAULT_BUFFER_SIZE;
}
final byte[] buffer = new byte[bufferLength];
byte[] result;
try {
int read = data.read(buffer, 0, bufferLength);
while (read > -1) {
update(buffer, 0, read);
read = data.read(buffer, 0, bufferLength);
}
result = doFinal();
} catch (IOException e) {
throw new CryptoException(e);
} finally {
reset();
}
return result;
}
use of cn.hutool.crypto.CryptoException in project hutool by looly.
the class Sign method sign.
// --------------------------------------------------------------------------------- Sign and Verify
/**
* 用私钥对信息生成数字签名
*
* @param data 加密数据
* @return 签名
*/
public byte[] sign(byte[] data) {
lock.lock();
try {
signature.initSign(this.privateKey);
signature.update(data);
return signature.sign();
} catch (Exception e) {
throw new CryptoException(e);
} finally {
lock.unlock();
}
}
use of cn.hutool.crypto.CryptoException in project hutool by looly.
the class Digester method digest.
/**
* 生成摘要
*
* @param data {@link InputStream} 数据流
* @param bufferLength 缓存长度,不足1使用 {@link IoUtil#DEFAULT_BUFFER_SIZE} 做为默认值
* @return 摘要bytes
* @throws CryptoException IO异常
*/
public byte[] digest(InputStream data, int bufferLength) throws CryptoException {
if (bufferLength < 1) {
bufferLength = IoUtil.DEFAULT_BUFFER_SIZE;
}
byte[] buffer = new byte[bufferLength];
byte[] result = null;
try {
int read = data.read(buffer, 0, bufferLength);
while (read > -1) {
digest.update(buffer, 0, read);
read = data.read(buffer, 0, bufferLength);
}
result = digest.digest();
} catch (IOException e) {
throw new CryptoException(e);
} finally {
digest.reset();
}
return result;
}
use of cn.hutool.crypto.CryptoException in project hutool by looly.
the class HMac method init.
/**
* 初始化
* @param algorithm 算法
* @param key 密钥 {@link SecretKey}
* @return {@link HMac}
* @throws CryptoException Cause by IOException
*/
public HMac init(String algorithm, SecretKey key) {
try {
mac = Mac.getInstance(algorithm);
if (null != key) {
this.secretKey = key;
} else {
this.secretKey = SecureUtil.generateKey(algorithm);
}
mac.init(this.secretKey);
} catch (Exception e) {
throw new CryptoException(e);
}
return this;
}
Aggregations