mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
add chacha20
This commit is contained in:
@@ -2,13 +2,10 @@ package cn.hutool.crypto.test.symmetric;
|
||||
|
||||
import cn.hutool.core.util.CharsetUtil;
|
||||
import cn.hutool.core.util.RandomUtil;
|
||||
import cn.hutool.crypto.KeyUtil;
|
||||
import cn.hutool.crypto.symmetric.SymmetricCrypto;
|
||||
import cn.hutool.crypto.symmetric.ChaCha20;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import javax.crypto.spec.IvParameterSpec;
|
||||
|
||||
/**
|
||||
* 见:https://stackoverflow.com/questions/32672241/using-bouncycastles-chacha-for-file-encryption
|
||||
*/
|
||||
@@ -16,18 +13,14 @@ public class ChaCha20Test {
|
||||
|
||||
@Test
|
||||
public void encryptAndDecryptTest() {
|
||||
String content = "test中文";
|
||||
|
||||
// 32 for 256 bit key or 16 for 128 bit
|
||||
byte[] key = RandomUtil.randomBytes(32);
|
||||
// 64 bit IV required by ChaCha20
|
||||
byte[] iv = RandomUtil.randomBytes(12);
|
||||
|
||||
final SymmetricCrypto chacha = new SymmetricCrypto("ChaCha20",
|
||||
KeyUtil.generateKey("ChaCha20", key),
|
||||
new IvParameterSpec(iv)
|
||||
);
|
||||
final ChaCha20 chacha = new ChaCha20(key, iv);
|
||||
|
||||
String content = "test中文";
|
||||
// 加密为16进制表示
|
||||
String encryptHex = chacha.encryptHex(content);
|
||||
// 解密为字符串
|
||||
|
Reference in New Issue
Block a user