API
Core
Text
IO / OS
Net
Encoding
Crypto
Compress / image / math
Database
External packages
UI
API
#crypto/sm4
Xu 0.8.8 API
import: #crypto/sm4
counts: 13 fns, 3 methods, 1 types, 1 consts, 0 ifaces
SM4 block plus CBC/CTR/CFB/OFB/GCM. Pure Xu, not commercial-crypto certified.
const block_size
pub const block_size = 16
Purpose
SM4 (GB/T 32907 / GM/T 0002). Xu addition — has no crypto/sm4. 128-bit key, 128-bit block, 32 rounds. Single-block encrypt/decrypt aligned with crypto/aes (new_cipher + encrypt/decrypt/block_size). CBC/CTR/CFB/OFB/GCM live in this package (not crypto/cipher, which stays AES-bound). Pure i64 with 32-bit masks. Zero C, not a commercial-crypto certification. Value `16`.
Parameters
none
struct Cipher
pub struct Cipher {
pub rk []int
pub sbox []int
}
Purpose
Public struct `Cipher`.
Parameters
rk []int field ([]int)
sbox []int field ([]int)
new_cipher
pub fn new_cipher(key &string ) Cipher
Purpose
Construct cipher. Throws on failure.
Parameters
key &string key (borrowed; caller keeps it; type &string)
Returns
Cipher — a value of this type
(&Cipher) block_size
pub fn (c &Cipher) block_size() int
Purpose
Method on `&Cipher`: block size.
Parameters
c &Cipher receiver &Cipher (borrow)
Returns
int — a value of this type
(&Cipher) encrypt
pub fn (c &Cipher) encrypt(src &string ) string
Purpose
Method on `&Cipher`: encrypt. Throws on failure.
Parameters
c &Cipher receiver &Cipher (borrow)
src &string source (borrowed; caller keeps it; type &string)
Returns
string — result string
(&Cipher) decrypt
pub fn (c &Cipher) decrypt(src &string ) string
Purpose
Method on `&Cipher`: decrypt. Throws on failure.
Parameters
c &Cipher receiver &Cipher (borrow)
src &string source (borrowed; caller keeps it; type &string)
Returns
string — result string
seal_gcm
pub fn seal_gcm(c &Cipher, nonce &string , plaintext &string , aad &string ) string
Purpose
Seal gcm. Throws on failure.
Parameters
c &Cipher byte or character code (borrowed; caller keeps it; type &Cipher)
nonce &string nonce (borrowed; caller keeps it; type &string)
plaintext &string plaintext (borrowed; caller keeps it; type &string)
aad &string aad (borrowed; caller keeps it; type &string)
Returns
string — result string
open_gcm
pub fn open_gcm(c &Cipher, nonce &string , sealed &string , aad &string ) string
Purpose
Open gcm. Throws on failure.
Parameters
c &Cipher byte or character code (borrowed; caller keeps it; type &Cipher)
nonce &string nonce (borrowed; caller keeps it; type &string)
sealed &string sealed (borrowed; caller keeps it; type &string)
aad &string aad (borrowed; caller keeps it; type &string)
Returns
string — result string
encrypt_cbc
pub fn encrypt_cbc(c &Cipher, iv &string , src &string ) string
Purpose
Encrypt cbc. Throws on failure.
Parameters
c &Cipher byte or character code (borrowed; caller keeps it; type &Cipher)
iv &string int payload or initialization vector (borrowed; caller keeps it; type &string)
src &string source (borrowed; caller keeps it; type &string)
Returns
string — result string
decrypt_cbc
pub fn decrypt_cbc(c &Cipher, iv &string , src &string ) string
Purpose
Decrypt cbc. Throws on failure.
Parameters
c &Cipher byte or character code (borrowed; caller keeps it; type &Cipher)
iv &string int payload or initialization vector (borrowed; caller keeps it; type &string)
src &string source (borrowed; caller keeps it; type &string)
Returns
string — result string
encrypt_cbc_pkcs7
pub fn encrypt_cbc_pkcs7(c &Cipher, iv &string , src &string ) string
Purpose
Encrypt cbc pkcs7.
Parameters
c &Cipher byte or character code (borrowed; caller keeps it; type &Cipher)
iv &string int payload or initialization vector (borrowed; caller keeps it; type &string)
src &string source (borrowed; caller keeps it; type &string)
Returns
string — result string
decrypt_cbc_pkcs7
pub fn decrypt_cbc_pkcs7(c &Cipher, iv &string , src &string ) string
Purpose
Decrypt cbc pkcs7. Throws on failure.
Parameters
c &Cipher byte or character code (borrowed; caller keeps it; type &Cipher)
iv &string int payload or initialization vector (borrowed; caller keeps it; type &string)
src &string source (borrowed; caller keeps it; type &string)
Returns
string — result string
encrypt_ctr
pub fn encrypt_ctr(c &Cipher, iv &string , src &string ) string
Purpose
Encrypt ctr.
Parameters
c &Cipher byte or character code (borrowed; caller keeps it; type &Cipher)
iv &string int payload or initialization vector (borrowed; caller keeps it; type &string)
src &string source (borrowed; caller keeps it; type &string)
Returns
string — result string
decrypt_ctr
pub fn decrypt_ctr(c &Cipher, iv &string , src &string ) string
Purpose
Decrypt ctr.
Parameters
c &Cipher byte or character code (borrowed; caller keeps it; type &Cipher)
iv &string int payload or initialization vector (borrowed; caller keeps it; type &string)
src &string source (borrowed; caller keeps it; type &string)
Returns
string — result string
encrypt_cfb
pub fn encrypt_cfb(c &Cipher, iv &string , src &string ) string
Purpose
Encrypt cfb.
Parameters
c &Cipher byte or character code (borrowed; caller keeps it; type &Cipher)
iv &string int payload or initialization vector (borrowed; caller keeps it; type &string)
src &string source (borrowed; caller keeps it; type &string)
Returns
string — result string
decrypt_cfb
pub fn decrypt_cfb(c &Cipher, iv &string , src &string ) string
Purpose
Decrypt cfb.
Parameters
c &Cipher byte or character code (borrowed; caller keeps it; type &Cipher)
iv &string int payload or initialization vector (borrowed; caller keeps it; type &string)
src &string source (borrowed; caller keeps it; type &string)
Returns
string — result string
encrypt_ofb
pub fn encrypt_ofb(c &Cipher, iv &string , src &string ) string
Purpose
Encrypt ofb.
Parameters
c &Cipher byte or character code (borrowed; caller keeps it; type &Cipher)
iv &string int payload or initialization vector (borrowed; caller keeps it; type &string)
src &string source (borrowed; caller keeps it; type &string)
Returns
string — result string
decrypt_ofb
pub fn decrypt_ofb(c &Cipher, iv &string , src &string ) string
Purpose
Decrypt ofb.
Parameters
c &Cipher byte or character code (borrowed; caller keeps it; type &Cipher)
iv &string int payload or initialization vector (borrowed; caller keeps it; type &string)
src &string source (borrowed; caller keeps it; type &string)
Returns
string — result string