mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
add chacha20
This commit is contained in:
parent
6ddf2a19d9
commit
6b8adc8a90
@ -0,0 +1,44 @@
|
|||||||
|
package cn.hutool.crypto.symmetric;
|
||||||
|
|
||||||
|
import cn.hutool.core.util.RandomUtil;
|
||||||
|
import cn.hutool.crypto.KeyUtil;
|
||||||
|
|
||||||
|
import javax.crypto.spec.IvParameterSpec;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ChaCha20算法实现<br>
|
||||||
|
* ChaCha系列流密码,作为salsa密码的改良版,具有更强的抵抗密码分析攻击的特性,“20”表示该算法有20轮的加密计算。
|
||||||
|
*
|
||||||
|
* @author looly
|
||||||
|
* @since 5.7.12
|
||||||
|
*/
|
||||||
|
public class ChaCha20 extends SymmetricCrypto {
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
|
public static final String ALGORITHM_NAME = "ChaCha20";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 构造
|
||||||
|
*
|
||||||
|
* @param key 密钥
|
||||||
|
* @param iv 加盐,12bytes(64bit)
|
||||||
|
*/
|
||||||
|
public ChaCha20(byte[] key, byte[] iv) {
|
||||||
|
super(ALGORITHM_NAME,
|
||||||
|
KeyUtil.generateKey(ALGORITHM_NAME, key),
|
||||||
|
generateIvParam(iv));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 生成加盐参数
|
||||||
|
*
|
||||||
|
* @param iv 加盐
|
||||||
|
* @return {@link IvParameterSpec}
|
||||||
|
*/
|
||||||
|
private static IvParameterSpec generateIvParam(byte[] iv) {
|
||||||
|
if (null == iv) {
|
||||||
|
iv = RandomUtil.randomBytes(12);
|
||||||
|
}
|
||||||
|
return new IvParameterSpec(iv);
|
||||||
|
}
|
||||||
|
}
|
@ -2,13 +2,10 @@ package cn.hutool.crypto.test.symmetric;
|
|||||||
|
|
||||||
import cn.hutool.core.util.CharsetUtil;
|
import cn.hutool.core.util.CharsetUtil;
|
||||||
import cn.hutool.core.util.RandomUtil;
|
import cn.hutool.core.util.RandomUtil;
|
||||||
import cn.hutool.crypto.KeyUtil;
|
import cn.hutool.crypto.symmetric.ChaCha20;
|
||||||
import cn.hutool.crypto.symmetric.SymmetricCrypto;
|
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import javax.crypto.spec.IvParameterSpec;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 见:https://stackoverflow.com/questions/32672241/using-bouncycastles-chacha-for-file-encryption
|
* 见:https://stackoverflow.com/questions/32672241/using-bouncycastles-chacha-for-file-encryption
|
||||||
*/
|
*/
|
||||||
@ -16,18 +13,14 @@ public class ChaCha20Test {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void encryptAndDecryptTest() {
|
public void encryptAndDecryptTest() {
|
||||||
String content = "test中文";
|
|
||||||
|
|
||||||
// 32 for 256 bit key or 16 for 128 bit
|
// 32 for 256 bit key or 16 for 128 bit
|
||||||
byte[] key = RandomUtil.randomBytes(32);
|
byte[] key = RandomUtil.randomBytes(32);
|
||||||
// 64 bit IV required by ChaCha20
|
// 64 bit IV required by ChaCha20
|
||||||
byte[] iv = RandomUtil.randomBytes(12);
|
byte[] iv = RandomUtil.randomBytes(12);
|
||||||
|
|
||||||
final SymmetricCrypto chacha = new SymmetricCrypto("ChaCha20",
|
final ChaCha20 chacha = new ChaCha20(key, iv);
|
||||||
KeyUtil.generateKey("ChaCha20", key),
|
|
||||||
new IvParameterSpec(iv)
|
|
||||||
);
|
|
||||||
|
|
||||||
|
String content = "test中文";
|
||||||
// 加密为16进制表示
|
// 加密为16进制表示
|
||||||
String encryptHex = chacha.encryptHex(content);
|
String encryptHex = chacha.encryptHex(content);
|
||||||
// 解密为字符串
|
// 解密为字符串
|
||||||
|
Loading…
x
Reference in New Issue
Block a user