const BLOCK_SIZE
pub const BLOCK_SIZE = 16用途
常量 BLOCK_SIZE,定义了相关的协议或算法配置参数。
#crypto/aesFIPS-197 AES 高级加密标准(AES-128 / AES-192 / AES-256)单块加密。多分组与工作模式(CBC / CTR / GCM)见 #crypto/cipher。
#crypto/aes 实现了国家标准与工业通用的 AES(Rijndael)对称分组加密算法。支持 128 位、192 位与 256 位密钥长度,具体实现取决于编译目标与后端:
const BLOCK_SIZE = 16。Cipher.hw_accel 表示当前密码器是否使用硬件加速;CPU 支持相关指令不代表当前实现一定启用。#crypto/cipher:
cipher.encrypt_cbc_pkcs7 / cipher.decrypt_cbc_pkcs7)。cipher.encrypt_ctr / cipher.decrypt_ctr)。@main
#crypto/aes
fn main() int {
// 16 字节密钥(AES-128)
key := "0123456789abcdef"
c := aes.new_cipher(&key) >>> (err) {
println("初始化失败: " + err.msg())
<< 1
}
// 单块明文必须正好 16 字节
block := "16_bytes_secret!"
enc := c.encrypt(&block) >>> (err) {
println("加密失败: " + err.msg())
<< 1
}
dec := c.decrypt(&enc) >>> (err) {
println("解密失败: " + err.msg())
<< 1
}
assert(dec == block)
println("单块加解密验证成功")
<< 0
}#crypto/cipher 进行变长 AES-CBC + PKCS#7 填充加密@main
#crypto/aes
#crypto/cipher
fn main() int {
key := "my-secret-key-16" // 16 字节密钥
iv := "1234567890123456" // 16 字节初始向量
c := aes.new_cipher(&key) >>> (err) { << 1 }
// 任意长度的业务数据(自动 PKCS#7 填充并分块加密)
msg := "Xu systems language provides supersonic native AES encryption."
enc := cipher.encrypt_cbc_pkcs7(&c, &iv, &msg) >>> (err) { << 1 }
dec := cipher.decrypt_cbc_pkcs7(&c, &iv, &enc) >>> (err) { << 1 }
assert(dec == msg)
println("CBC PKCS#7 解密还原成功")
<< 0
}const BLOCK_SIZEpub const BLOCK_SIZE = 16常量 BLOCK_SIZE,定义了相关的协议或算法配置参数。
struct Cipherpub struct Cipher {
pub nr int
pub rk []int
pub sbox []int
pub inv []int
pub te []int
pub td []int
pub erk []int
pub drk []int
pub hw_accel bool
pub rk_bytes string
pub drk_bytes string
}Aes 密码算法的分组或流式加解密上下文。
nr intrk []intsbox []intinv []intte []inttd []interk []intdrk []inthw_accel boolrk_bytes stringdrk_bytes stringnew_cipherpub fn new_cipher(key &string) Cipher构造 cipher。失败时抛出错误,调用方需处理或继续传播。
key &stringCipher — 该类型的值
(&Cipher) block_sizepub fn (c &Cipher) block_size() int&Cipher 上的操作:块尺寸。
c &CipherCipher 实例;以只读借用传入,调用方仍持有实例。int — 该类型的值
(&Cipher) encrypt_hw_intopub fn (c &Cipher) encrypt_hw_into(out $&string, src &string) int&Cipher 上的操作:执行 encrypt hw into 操作。失败时抛出错误,调用方需处理或继续传播。
c &CipherCipher 实例;以只读借用传入,调用方仍持有实例。out $&stringsrc &stringint — 该类型的值
(&Cipher) encrypt_arr16pub fn (c &Cipher) encrypt_arr16(dst $&[16]int, src &[16]int) int&Cipher 上的操作:加密arr16。
c &CipherCipher 实例;以只读借用传入,调用方仍持有实例。dst $&[16]intsrc &[16]intint — 该类型的值
(&Cipher) encryptpub fn (c &Cipher) encrypt(src &string) string&Cipher 上的操作:加密。失败时抛出错误,调用方需处理或继续传播。
c &CipherCipher 实例;以只读借用传入,调用方仍持有实例。src &stringstring — 结果字符串
(&Cipher) encrypt_blockpub fn (c &Cipher) encrypt_block(dst $&[]int, src &[]int) bool&Cipher 上的操作:加密块。失败时抛出错误,调用方需处理或继续传播。
c &CipherCipher 实例;以只读借用传入,调用方仍持有实例。dst $&[]intsrc &[]intbool — 是否成立
(&Cipher) decrypt_hw_intopub fn (c &Cipher) decrypt_hw_into(out $&string, src &string) int&Cipher 上的操作:执行 decrypt hw into 操作。失败时抛出错误,调用方需处理或继续传播。
c &CipherCipher 实例;以只读借用传入,调用方仍持有实例。out $&stringsrc &stringint — 该类型的值
(&Cipher) decrypt_arr16pub fn (c &Cipher) decrypt_arr16(dst $&[16]int, src &[16]int) int&Cipher 上的操作:解密arr16。
c &CipherCipher 实例;以只读借用传入,调用方仍持有实例。dst $&[16]intsrc &[16]intint — 该类型的值
(&Cipher) decryptpub fn (c &Cipher) decrypt(src &string) string&Cipher 上的操作:解密。失败时抛出错误,调用方需处理或继续传播。
c &CipherCipher 实例;以只读借用传入,调用方仍持有实例。src &stringstring — 结果字符串
(&Cipher) decrypt_blockpub fn (c &Cipher) decrypt_block(dst $&[]int, src &[]int) bool&Cipher 上的操作:解密块。失败时抛出错误,调用方需处理或继续传播。
c &CipherCipher 实例;以只读借用传入,调用方仍持有实例。dst $&[]intsrc &[]intbool — 是否成立