const BLOCK_SIZE
pub const BLOCK_SIZE = 16Purpose
block_size is the AES block size in bytes (16 bytes, 128 bits).
#crypto/aesFIPS-197 AES (Advanced Encryption Standard: AES-128 / AES-192 / AES-256) block cipher. Modes and multi-block operations (CBC / CTR / GCM) live in #crypto/cipher.
#crypto/aes implements the standard AES (Rijndael) symmetric block cipher. It supports 128-bit, 192-bit, and 256-bit key sizes. The active implementation depends on the compiler target and backend:
const BLOCK_SIZE = 16.Cipher.hw_accel reports whether the active cipher uses hardware acceleration; CPU support alone does not guarantee it.#crypto/cipher:
cipher.encrypt_cbc_pkcs7 / cipher.decrypt_cbc_pkcs7).cipher.encrypt_ctr / cipher.decrypt_ctr).@main
#crypto/aes
fn main() int {
// 16-byte key for AES-128
key := "0123456789abcdef"
c := aes.new_cipher(&key) >>> (err) {
println("Cipher init error: " + err.msg())
<< 1
}
// Single block must be exactly 16 bytes
block := "16_bytes_secret!"
enc := c.encrypt(&block) >>> (err) {
println("Encryption error: " + err.msg())
<< 1
}
dec := c.decrypt(&enc) >>> (err) {
println("Decryption error: " + err.msg())
<< 1
}
assert(dec == block)
println("Single-block roundtrip passed!")
<< 0
}#crypto/cipher@main
#crypto/aes
#crypto/cipher
fn main() int {
key := "my-secret-key-16" // 16-byte key
iv := "1234567890123456" // 16-byte initialization vector
c := aes.new_cipher(&key) >>> (err) { << 1 }
// Arbitrary length payload with automatic PKCS#7 padding
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 decrypted successfully!")
<< 0
}const BLOCK_SIZEpub const BLOCK_SIZE = 16block_size is the AES block size in bytes (16 bytes, 128 bits).
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
}Cipher represents an instantiated AES cipher with expanded key schedule. Supports AES-128 (10 rounds), AES-192 (12 rounds), and AES-256 (14 rounds). Hardware acceleration (AES-NI / ARMv8 Crypto) is indicated by hw_accel.
nr intrk []intsbox []intinv []intte []inttd []interk []intdrk []inthw_accel boolrk_bytes stringdrk_bytes stringnew_cipherpub fn new_cipher(key &string) Ciphernew_cipher creates and returns a new Cipher instance from the given key. The key argument must be 16 bytes (AES-128), 24 bytes (AES-192), or 32 bytes (AES-256). Throws an error if the key length is not 16, 24, or 32 bytes. Throws on failure; handle the error or propagate it to the caller.
key &stringCipher — a value of this type
(&Cipher) block_sizepub fn (c &Cipher) block_size() intblock_size returns the cipher's block size in bytes (always 16).
c &CipherCipher instance used by this call (borrow).int — a value of this type
(&Cipher) encrypt_hw_intopub fn (c &Cipher) encrypt_hw_into(out $&string, src &string) intOperation on &Cipher: encrypt hw into. Throws on failure; handle the error or propagate it to the caller.
c &CipherCipher instance used by this call (borrow).out $&stringsrc &stringint — a value of this type
(&Cipher) encrypt_arr16pub fn (c &Cipher) encrypt_arr16(dst $&[16]int, src &[16]int) intOperation on &Cipher: encrypt arr16.
c &CipherCipher instance used by this call (borrow).dst $&[16]intsrc &[16]intint — a value of this type
(&Cipher) encryptpub fn (c &Cipher) encrypt(src &string) stringencrypt encrypts a single 16-byte block from src and returns the 16-byte ciphertext string. Throws an error if src.len != 16. For multi-block or streaming encryption, use crypto/cipher. Throws on failure; handle the error or propagate it to the caller.
c &CipherCipher instance used by this call (borrow).src &stringstring — result string
(&Cipher) encrypt_blockpub fn (c &Cipher) encrypt_block(dst $&[]int, src &[]int) boolencrypt_block encrypts a single 16-byte block from src into dst in place. Throws an error if src.len != 16 or dst.len != 16. Throws on failure; handle the error or propagate it to the caller.
c &CipherCipher instance used by this call (borrow).dst $&[]intsrc &[]intbool — true or false
(&Cipher) decrypt_hw_intopub fn (c &Cipher) decrypt_hw_into(out $&string, src &string) intOperation on &Cipher: decrypt hw into. Throws on failure; handle the error or propagate it to the caller.
c &CipherCipher instance used by this call (borrow).out $&stringsrc &stringint — a value of this type
(&Cipher) decrypt_arr16pub fn (c &Cipher) decrypt_arr16(dst $&[16]int, src &[16]int) intOperation on &Cipher: decrypt arr16.
c &CipherCipher instance used by this call (borrow).dst $&[16]intsrc &[16]intint — a value of this type
(&Cipher) decryptpub fn (c &Cipher) decrypt(src &string) stringdecrypt decrypts a single 16-byte block from src and returns the 16-byte plaintext string. Throws an error if src.len != 16. For multi-block or streaming decryption, use crypto/cipher. Throws on failure; handle the error or propagate it to the caller.
c &CipherCipher instance used by this call (borrow).src &stringstring — result string
(&Cipher) decrypt_blockpub fn (c &Cipher) decrypt_block(dst $&[]int, src &[]int) booldecrypt_block decrypts a single 16-byte block from src into dst in place. Throws an error if src.len != 16 or dst.len != 16. Throws on failure; handle the error or propagate it to the caller.
c &CipherCipher instance used by this call (borrow).dst $&[]intsrc &[]intbool — true or false