FlashSwirl 闪旋,一款高性能的对称加密算法库,提供流加密、AEAD认证加密、HASH、HMAC、HKDF、PBKDF2
概述
由风之暇想研究的对称加密算法,基于ARX(Add-Rotate-XOR)结构设计,灵感来源于ChaCha20;加密库提供流加密、AEAD认证加密、HASH、HMAC、HKDF密钥派生、PBKDF2密钥派生的密码学功能。
✨ 特性
多种加密模式:支持流加密(Stream)和AEAD认证加密
高性能设计:批量处理、并行计算、内存池优化
跨平台支持:提供C++、Go、JavaScript三种语言代码
算法规范
算法规范文档
三种语言库调用说明
C++ 版本
使用示例:
#include "FlashSwirl.h"
#include <iostream>
#include <vector>
int main() {
// 准备密钥和Nonce
// ===== 1. 流加密 =====
FlashSwirl_EncryptBuffer(key, 32, nonce, 24, data.data(), data.size(), 20);
// data现在包含密文
FlashSwirl_DecryptBuffer(key, 32, nonce, 24, data.data(), data.size(), 20);
// data现在恢复为明文
// ===== 2. AEAD认证加密 =====
uint8_t plaintext[] = "Secret message";
uint8_t ciphertext[256];
int outLen = sizeof(ciphertext);
uint8_t ad[] = "additional-data";
FlashSwirl_EncryptAEADBuffer(key, 32, nonce, 24,
plaintext, sizeof(plaintext)-1,
ciphertext, &outLen, ad, sizeof(ad)-1, 20);
uint8_t decrypted[256];
int plainLen = sizeof(decrypted);
FlashSwirl_DecryptAEADBuffer(key, 32, nonce, 24,
ciphertext, outLen,
decrypted, &plainLen, ad, sizeof(ad)-1, 20);
// ===== 3. HASH =====
const char* message = "Hello, FlashSwirl!";
uint8_t hash[32];
FlashSwirl_Hash((const uint8_t*)message, strlen(message), 20, hash);
// ===== 4. HMAC =====
uint8_t hmacKey[] = "secret-key";
uint8_t hmacOut[32];
FlashSwirl_HMAC(hmacKey, sizeof(hmacKey)-1,
(const uint8_t*)message, strlen(message),
20, hmacOut);
// ===== 5. HKDF密钥派生 =====
uint8_t info[] = "my-app";
uint8_t derivedKey[32];
FlashSwirl_HKDF(masterKey, 32, salt, 32, info, sizeof(info)-1, 32, 20, derivedKey);
// ===== 6. PBKDF2密钥派生 =====
const char* password = "user-password";
uint8_t pbkdf2Salt[] = "random-salt";
uint8_t keyFromPassword[32];
FlashSwirl_PBKDF2((const uint8_t*)password, strlen(password),
pbkdf2Salt, sizeof(pbkdf2Salt)-1,
10000, 32, 20, keyFromPassword);
return 0;
Go 版本
ac2K9s2c8@1M7s2y4Q4x3@1q4Q4x3V1k6Q4x3V1k6H3K9$3N6Q4x3X3g2Y4L8#2)9J5k6h3c8W2N6W2)9J5c8X3N6A6N6r3S2#2j5W2)9J5k6h3y4G2L8g2)9J5c8X3k6*7P5s2S2Q4x3V1k6r3L8r3q4K6K9q4y4%4K9i4u0D9i4K6u0r3c8@1!0Q4x3V1k6r3L8r3q4K6K9q4y4%4K9i4u0D9
使用示例:
package main
import (
"bytes"
"crypto/rand"
"fmt"
"FlashSwirl"
func main() {
// 准备密钥和Nonce
key := make([]byte, 32)
nonce := make([]byte, 24)
rand.Read(key)
rand.Read(nonce)
// ===== 1. 流加密 =====
plaintext := []byte("Secret message")
var encrypted bytes.Buffer
FlashSwirl.Encrypt(key, nonce, bytes.NewReader(plaintext), &encrypted, 20)
var decrypted bytes.Buffer
FlashSwirl.Decrypt(key, nonce, &encrypted, &decrypted, 20)
fmt.Printf("Decrypted: %s\n", decrypted.Bytes())
// ===== 2. AEAD认证加密 =====
var aeadEncrypted bytes.Buffer
additionalData := []byte("context info")
FlashSwirl.EncryptAEAD(key, nonce, bytes.NewReader(plaintext), &aeadEncrypted, additionalData, 20
...(已截断)
---
来源: 看雪论坛
原文链接: https://bbs.kanxue.com/thread-290985.htm
[原创自研]FlashSwirl 闪旋,一款高性能的对称加密算法库
192 浏览
0 回复
暂无回复,快来抢沙发吧!