moonvault

    MoonVault -- Pure MoonBit password hashing and cryptographic library

    crypto
    password
    hash
    security
    bcrypt
    scrypt
    argon2
    sha256
    Download zip
    Author
    Version
    0.1.0
    License
    Apache-2.0
    Last updated
    2 months ago
    Downloads
    36

    No dependencies

    #MoonVault -- 暗码天鉴

    Pure MoonBit password hashing and cryptographic library.

    License MoonBit

    #功能特性

    模块标准说明
    SHA-256FIPS 180-4安全哈希算法
    HMAC-SHA-256RFC 2104密钥哈希消息认证码
    PBKDF2RFC 2898基于密码的密钥派生(HMAC-SHA-256)
    bcrypt—基于 Blowfish 的密码哈希
    scryptRFC 7914内存硬密码哈希(Salsa20/8)
    Argon2idRFC 9106内存硬密码哈希(Blake2b)
    AES-256-GCMFIPS 197 / SP 800-38D对称加密认证
    ChaCha20-Poly1305RFC 8439流密码 AEAD
    SHA-512/384FIPS 180-4SHA-512 族哈希
    HKDFRFC 5869密钥派生函数
    TOTP/HOTPRFC 6238 / RFC 4226二因素认证
    Constant-time—常量时间比较,防御时序侧信道攻击
    Random—密码学安全随机数生成
    Password—密码强度检测、自动生成、升级检测
    Base32RFC 4648TOTP 密钥编解码

    #快速开始

    // bcrypt 哈希与验证
    let hash = hash_password("my_secure_password")
    let ok = verify_password("my_secure_password", hash)

    // SHA-256
    let digest = sha256_hex("hello world")
    // => "b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9"

    // HMAC
    let mac = hmac_sha256_hex("my_key", "my_message")

    // PBKDF2
    let dk = pbkdf2_hex("password", "salt", 100000, 32)

    // 生成随机密码
    let pwd = generate_password(16)

    // 密码强度检测
    let score = password_strength("Str0ng!Passw0rd") // => 80+
    let level = password_strength_level("Str0ng!Passw0rd") // => "strong"

    #API 参考

    #SHA-256

    pub fn sha256(data : Bytes) -> Bytes
    pub fn sha256_hex(s : String) -> String
    pub fn str_to_utf8(s : String) -> Bytes
    pub fn bytes_to_hex(bytes : Bytes) -> String

    #HMAC-SHA-256

    pub fn HmacSha256::new(key : Bytes) -> HmacSha256
    pub fn HmacSha256::sign(self : HmacSha256, message : Bytes) -> Bytes
    pub fn HmacSha256::sign_string(self : HmacSha256, s : String) -> Bytes
    pub fn HmacSha256::verify(self : HmacSha256, message : Bytes, mac : Bytes) -> Bool
    pub fn hmac_sha256(key : Bytes, message : Bytes) -> Bytes
    pub fn hmac_sha256_hex(key : String, message : String) -> String
    pub fn hmac_verify(key : Bytes, message : Bytes, mac : Bytes) -> Bool

    #PBKDF2 (HMAC-SHA-256)

    pub fn pbkdf2(password : String, salt : String, iterations : Int, key_len : Int) -> Bytes
    pub fn pbkdf2_raw(password : Bytes, salt : Bytes, iterations : Int, key_len : Int) -> Bytes
    pub fn pbkdf2_hex(password : String, salt : String, iterations : Int, key_len : Int) -> String
    pub fn pbkdf2_verify(password : String, salt : String, iterations : Int, key_len : Int, expected : Bytes) -> Bool

    #bcrypt

    pub fn bcrypt_hash(password : String, cost : Int, salt : String) -> String
    pub fn bcrypt_verify(password : String, hash : String) -> Bool
    pub fn generate_salt() -> String

    cost 范围:4–31,推荐 ≥ 10。

    #scrypt

    pub fn scrypt(password : String, salt : String, n : Int, r : Int, p : Int, dk_len : Int) -> Bytes
    pub fn scrypt_hex(password : String, salt : String, n : Int, r : Int, p : Int, dk_len : Int) -> String
    pub fn scrypt_verify(password : String, salt : String, n : Int, r : Int, p : Int, dk_len : Int, expected : Bytes) -> Bool

    // 推荐参数
    pub fn scrypt_params_interactive() -> (Int, Int, Int) // (16384, 8, 1)
    pub fn scrypt_params_sensitive() -> (Int, Int, Int) // (1048576, 8, 1)
    pub fn scrypt_params_paranoid() -> (Int, Int, Int) // (4194304, 8, 1)

    #Argon2id

    pub fn argon2id(password : String, salt : String, t_cost : Int, m_cost : Int, parallelism : Int, hash_len : Int) -> Bytes
    pub fn argon2id_hash(password : String, salt : String, t_cost : Int, m_cost : Int, parallelism : Int) -> String
    pub fn argon2id_verify(password : String, salt : String, t_cost : Int, m_cost : Int, parallelism : Int, hash_len : Int, expected : Bytes) -> Bool

    // 推荐参数
    pub fn argon2id_params_interactive() -> (Int, Int, Int) // (2, 65536, 1)
    pub fn argon2id_params_moderate() -> (Int, Int, Int) // (3, 262144, 1)
    pub fn argon2id_params_sensitive() -> (Int, Int, Int) // (4, 1048576, 1)

    #Password 工具

    // bcrypt 一键哈希/验证
    pub fn hash_password(password : String) -> String
    pub fn verify_password(password : String, hash : String) -> Bool

    // scrypt 一键哈希/验证
    pub fn hash_password_scrypt(password : String) -> String
    pub fn verify_password_scrypt(password : String, hash : String) -> Bool

    // Argon2id 一键哈希/验证
    pub fn hash_password_argon2id(password : String) -> String
    pub fn verify_password_argon2id(password : String, hash : String) -> Bool

    // 工具函数
    pub fn password_needs_rehash(hash : String) -> Bool // 检测 $2a$ 需升级到 $2b$
    pub fn generate_password(length : Int) -> String // 生成随机密码
    pub fn password_strength(password : String) -> Int // 密码强度评分 (0–100)
    pub fn password_strength_level(password : String) -> String // "weak" | "fair" | "strong" | "very_strong"

    #Random

    pub fn random_bytes(len : Int) -> Bytes
    pub fn random_u32() -> UInt
    pub fn random_u64() -> UInt64
    pub fn random_int() -> Int
    pub fn random_uint_range(min : UInt, max : UInt) -> UInt
    pub fn random_int_range(min : Int, max : Int) -> Int
    pub fn[T] random_choice(items : Array[T]) -> T
    pub fn[T] shuffle(arr : Array[T]) -> Array[T]
    pub fn random_hex(len : Int) -> String
    pub fn random_base64(len : Int) -> String

    #Constant-time

    pub fn constant_eq(a : Bytes, b : Bytes) -> Bool
    pub fn constant_eq_string(a : String, b : String) -> Bool
    pub fn constant_ne(a : Bytes, b : Bytes) -> Bool
    pub fn constant_lt(a : Int, b : Int) -> Bool
    pub fn constant_ge(a : Int, b : Int) -> Bool
    pub fn constant_select(a : Int, b : Int, cond : Bool) -> Int
    pub fn constant_is_zero(x : UInt) -> Bool

    所有比较函数均为常量时间,防止时序侧信道攻击。

    #测试

    moon test

    43 项测试全部通过。

    #许可证

    Apache-2.0

    BlowfishState

    type BlowfishState

    EntropyState

    type EntropyState

    HmacSha256

    type HmacSha256

    HmacSha256::new

    fn HmacSha256::new(key : Bytes) -> HmacSha256

    HmacSha256::sign

    fn HmacSha256::sign(self : HmacSha256, message : Bytes) -> Bytes

    HmacSha256::sign_string

    fn HmacSha256::sign_string(self : HmacSha256, s : String) -> Bytes

    HmacSha256::verify

    fn HmacSha256::verify(self : HmacSha256, message : Bytes, mac : Bytes) -> Bool

    Sha256

    type Sha256

    Sha256::new

    fn Sha256::new() -> Sha256

    Sha256::sum

    fn Sha256::sum(self : Sha256) -> Bytes

    Sha256::write

    fn Sha256::write(self : Sha256, data : Bytes) -> Sha256

    Sha256::write_string

    fn Sha256::write_string(self : Sha256, s : String) -> Sha256

    aes256_gcm_decrypt

    fn aes256_gcm_decrypt(key : Bytes, nonce : Bytes, ciphertext : Bytes, aad : Bytes, tag : Bytes) -> Bytes?

    aes256_gcm_encrypt

    fn aes256_gcm_encrypt(key : Bytes, nonce : Bytes, plaintext : Bytes, aad : Bytes) -> (Bytes, Bytes)

    aes256_gcm_encrypt_hex

    fn aes256_gcm_encrypt_hex(key : String, nonce : String, plaintext : String, aad : String) -> (String, String)

    argon2id

    fn argon2id(password : String, salt : String, t_cost : Int, m_cost : Int, parallelism : Int, hash_len : Int) -> Bytes

    argon2id_hash

    fn argon2id_hash(password : String, salt : String, t_cost : Int, m_cost : Int, parallelism : Int) -> String

    argon2id_params_interactive

    fn argon2id_params_interactive() -> (Int, Int, Int)

    argon2id_params_moderate

    fn argon2id_params_moderate() -> (Int, Int, Int)

    argon2id_params_sensitive

    fn argon2id_params_sensitive() -> (Int, Int, Int)

    argon2id_verify

    fn argon2id_verify(password : String, salt : String, t_cost : Int, m_cost : Int, parallelism : Int, hash_len : Int, expected : Bytes) -> Bool

    base32_decode

    fn base32_decode(encoded : String) -> Bytes

    base32_encode

    fn base32_encode(data : Bytes) -> String

    bcrypt_hash

    fn bcrypt_hash(password : String, cost : Int, salt : String) -> String

    bcrypt_verify

    fn bcrypt_verify(password : String, hash : String) -> Bool

    bytes_to_hex

    fn bytes_to_hex(b : Bytes) -> String

    chacha20_decrypt

    fn chacha20_decrypt(key : Bytes, nonce : Bytes, ciphertext : Bytes) -> Bytes

    chacha20_encrypt

    fn chacha20_encrypt(key : Bytes, nonce : Bytes, plaintext : Bytes) -> Bytes

    chacha20_poly1305_decrypt

    fn chacha20_poly1305_decrypt(key : Bytes, nonce : Bytes, ciphertext : Bytes, aad : Bytes, tag : Bytes) -> Bytes?

    chacha20_poly1305_encrypt

    fn chacha20_poly1305_encrypt(key : Bytes, nonce : Bytes, plaintext : Bytes, aad : Bytes) -> (Bytes, Bytes)

    constant_eq

    fn constant_eq(a : Bytes, b : Bytes) -> Bool

    constant_eq_bytes

    fn constant_eq_bytes(a : Array[Byte], b : Array[Byte], len : Int) -> Bool

    constant_eq_string

    fn constant_eq_string(a : String, b : String) -> Bool

    constant_ge

    fn constant_ge(a : Int, b : Int) -> Bool

    constant_is_zero

    fn constant_is_zero(x : UInt) -> Bool

    constant_lt

    fn constant_lt(a : Int, b : Int) -> Bool

    constant_ne

    fn constant_ne(a : Bytes, b : Bytes) -> Bool

    constant_select

    fn constant_select(a : Int, b : Int, cond : Bool) -> Int

    generate_aes_key_256

    fn generate_aes_key_256() -> Bytes

    generate_chacha20_key

    fn generate_chacha20_key() -> Bytes

    generate_nonce_96

    fn generate_nonce_96() -> Bytes

    generate_password

    fn generate_password(length : Int) -> String

    generate_salt

    fn generate_salt() -> String

    generate_totp_secret

    fn generate_totp_secret() -> String

    generate_totp_secret_bytes

    fn generate_totp_secret_bytes() -> Bytes

    hash_password

    fn hash_password(password : String) -> String

    hash_password_argon2id

    fn hash_password_argon2id(password : String) -> String

    hash_password_scrypt

    fn hash_password_scrypt(password : String) -> String

    hkdf_expand_hex

    fn hkdf_expand_hex(prk_hex : String, info : String, length : Int) -> String

    hkdf_extract_hex

    fn hkdf_extract_hex(key_material : String, salt : String) -> String

    hkdf_sha256

    fn hkdf_sha256(ikm : Bytes, salt : Bytes, info : Bytes, length : Int) -> Bytes

    hkdf_sha256_hex

    fn hkdf_sha256_hex(ikm : String, salt : String, info : String, length : Int) -> String

    hmac_sha256

    fn hmac_sha256(key : Bytes, message : Bytes) -> Bytes

    hmac_sha256_hex

    fn hmac_sha256_hex(key : String, message : String) -> String

    hmac_verify

    fn hmac_verify(key : Bytes, message : Bytes, mac : Bytes) -> Bool

    hotp

    fn hotp(secret : Bytes, counter : UInt64, digits : Int) -> String

    hotp_verify

    fn hotp_verify(secret : Bytes, counter : UInt64, digits : Int, code : String) -> Bool

    password_needs_rehash

    fn password_needs_rehash(hash : String) -> Bool

    password_strength

    fn password_strength(password : String) -> Int

    password_strength_level

    fn password_strength_level(password : String) -> String

    pbkdf2

    fn pbkdf2(password : String, salt : String, iterations : Int, key_len : Int) -> Bytes

    pbkdf2_hex

    fn pbkdf2_hex(password : String, salt : String, iterations : Int, key_len : Int) -> String

    pbkdf2_raw

    fn pbkdf2_raw(password : Bytes, salt : Bytes, iterations : Int, key_len : Int) -> Bytes

    pbkdf2_sha256

    fn pbkdf2_sha256(password : String, salt : String, iterations : Int, key_len : Int) -> Bytes

    pbkdf2_verify

    fn pbkdf2_verify(password : String, salt : String, iterations : Int, key_len : Int, expected : Bytes) -> Bool

    poly1305_mac

    fn poly1305_mac(key : Bytes, message : Bytes) -> Bytes

    random_base64

    fn random_base64(len : Int) -> String

    random_bytes

    fn random_bytes(len : Int) -> Bytes

    random_choice

    fn[T] random_choice(items : Array[T]) -> T

    random_hex

    fn random_hex(len : Int) -> String

    random_int

    fn random_int() -> Int

    random_int_range

    fn random_int_range(min : Int, max : Int) -> Int

    random_u32

    fn random_u32() -> UInt

    random_u64

    fn random_u64() -> UInt64

    random_uint_range

    fn random_uint_range(min : UInt, max : UInt) -> UInt

    scrypt

    fn scrypt(password : String, salt : String, n : Int, r : Int, p : Int, dk_len : Int) -> Bytes

    scrypt_hex

    fn scrypt_hex(password : String, salt : String, n : Int, r : Int, p : Int, dk_len : Int) -> String

    scrypt_params_interactive

    fn scrypt_params_interactive() -> (Int, Int, Int)

    scrypt_params_paranoid

    fn scrypt_params_paranoid() -> (Int, Int, Int)

    scrypt_params_sensitive

    fn scrypt_params_sensitive() -> (Int, Int, Int)

    scrypt_simple

    fn scrypt_simple(password : String, salt : String, n : Int, r : Int, p : Int, dk_len : Int) -> Bytes

    scrypt_verify

    fn scrypt_verify(password : String, salt : String, n : Int, r : Int, p : Int, dk_len : Int, expected : Bytes) -> Bool

    sha256

    fn sha256(data : Bytes) -> Bytes

    sha256_hex

    fn sha256_hex(s : String) -> String

    sha384

    fn sha384(data : Bytes) -> Bytes

    sha384_hex

    fn sha384_hex(s : String) -> String

    sha512

    fn sha512(data : Bytes) -> Bytes

    sha512_256

    fn sha512_256(data : Bytes) -> Bytes

    sha512_256_hex

    fn sha512_256_hex(s : String) -> String

    sha512_hex

    fn sha512_hex(s : String) -> String

    shuffle

    fn[T] shuffle(arr : Array[T]) -> Array[T]

    str_to_utf8

    fn str_to_utf8(s : String) -> Bytes

    totp

    fn totp(secret : Bytes, time : UInt64, period : Int, digits : Int) -> String

    totp_now

    fn totp_now(secret : Bytes) -> String

    totp_sha256

    fn totp_sha256(secret : Bytes, time : UInt64, period : Int, digits : Int) -> String

    totp_sha512

    fn totp_sha512(secret : Bytes, time : UInt64, period : Int, digits : Int) -> String

    totp_uri

    fn totp_uri(secret : String, account : String, issuer : String) -> String

    totp_verify

    fn totp_verify(secret : Bytes, code : String) -> Bool

    verify_password

    fn verify_password(password : String, hash : String) -> Bool

    verify_password_argon2id

    fn verify_password_argon2id(password : String, hash : String) -> Bool

    verify_password_scrypt

    fn verify_password_scrypt(password : String, hash : String) -> Bool