Search in sources :

Example 11 with CryptoException

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();
    }
}
Also used : WriteLock(java.util.concurrent.locks.ReentrantReadWriteLock.WriteLock) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) CryptoException(cn.hutool.crypto.CryptoException)

Example 12 with CryptoException

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;
}
Also used : IOException(java.io.IOException) CryptoException(cn.hutool.crypto.CryptoException)

Example 13 with CryptoException

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();
    }
}
Also used : CryptoException(cn.hutool.crypto.CryptoException) CryptoException(cn.hutool.crypto.CryptoException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Example 14 with CryptoException

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;
}
Also used : IOException(java.io.IOException) CryptoException(cn.hutool.crypto.CryptoException)

Example 15 with CryptoException

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;
}
Also used : CryptoException(cn.hutool.crypto.CryptoException) IOException(java.io.IOException) CryptoException(cn.hutool.crypto.CryptoException)

Aggregations

CryptoException (cn.hutool.crypto.CryptoException)20 IOException (java.io.IOException)10 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)8 InvalidKeyException (java.security.InvalidKeyException)6 Cipher (javax.crypto.Cipher)6 IORuntimeException (cn.hutool.core.io.IORuntimeException)4 Key (java.security.Key)4 PrivateKey (java.security.PrivateKey)4 PublicKey (java.security.PublicKey)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 RSAKey (java.security.interfaces.RSAKey)2 BadPaddingException (javax.crypto.BadPaddingException)2 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)2 InvalidCipherTextException (org.bouncycastle.crypto.InvalidCipherTextException)2 SM2Engine (org.bouncycastle.crypto.engines.SM2Engine)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 X509Certificate (java.security.cert.X509Certificate)1 ReentrantReadWriteLock (java.util.concurrent.locks.ReentrantReadWriteLock)1 WriteLock (java.util.concurrent.locks.ReentrantReadWriteLock.WriteLock)1 CipherInputStream (javax.crypto.CipherInputStream)1