A RSA library for vlang.
- vlang >= 0.5.2
Add the dependency to your project:
v install deatil.vrsaor
v install --git https://github.com/deatil/v-rsaThe v-rsa can be imported in your application with:
import deatil.vrsa.rsamodule main
import rand
import deatil.vrsa.rsa
fn main() {
mut rng := rand.new_default()
prikey := rsa.generate_key(mut rng, 2048)!
pubkey := prikey.public()
msg := '12345678abcde'.bytes()
hashed := rsa.hasher_sha256.hash_msg(msg)!
signed := rsa.sign_pkcs1v15(prikey, rsa.hasher_sha256, hashed)!
println('sign_pkcs1v15: ${signed.hex()}')
veri := if _ := rsa.verify_pkcs1v15(pubkey, rsa.hasher_sha256, hashed, signed) {
true
} else {
false
}
println('verify_pkcs1v15: ${veri}')
}Generate key:
generate_key(mut random rand.PRNG, bits int) !PrivateKeyPKCS1v15 sign:
sign_pkcs1v15(priv PrivateKey, hasher IHasher, hashed []u8) ![]u8verify_pkcs1v15(pubkey PublicKey, hasher IHasher, hashed []u8, sig []u8) !PKCS1v15 encrypt:
encrypt_pkcs1v15(mut random rand.PRNG, pubkey PublicKey, msg []u8) ![]u8decrypt_pkcs1v15(priv PrivateKey, ciphertext []u8) ![]u8OAEP encrypt:
encrypt_oaep(mut h hash.Hash, mut random rand.PRNG, pubkey PublicKey, msg []u8, label []u8) ![]u8decrypt_oaep(mut h hash.Hash, priv PrivateKey, ciphertext []u8, label []u8) ![]u8PSS sign:
sign_pss(mut random rand.PRNG, priv PrivateKey, mut h hash.Hash, digest []u8, opts PSSOptions)verify_pss(pubkey PublicKey, mut h hash.Hash, digest []u8, sig []u8, opts PSSOptions) !hasher_md5: rsa.hasher_md5hasher_sha1: rsa.hasher_sha1hasher_sha224: rsa.hasher_sha224hasher_sha256: rsa.hasher_sha256hasher_sha384: rsa.hasher_sha384hasher_sha512: rsa.hasher_sha512hasher_ripemd160: rsa.hasher_ripemd160
custom hasher (example sm3):
import deatil.sm3
pub const hasher_sm3 = Hasher{
prefixe: [u8(0x30), 0x30, 0x30, 0x0c, 0x06, 0x08, 0x2a, 0x81, 0x1c, 0xcf, 0x55, 0x01, 0x83, 0x78, 0x05, 0x00, 0x04, 0x20]
hash: fn () hash.Hash {
return sm3.new()
}
}PCKS1 prikey:
parse_prikey_pkcs1_der(bytes []u8) !PrivateKeymake_prikey_pkcs1_der(prikey PrivateKey) ![]u8PCKS1 pubkey:
parse_pubkey_pkcs1_der(bytes []u8) !PublicKeymake_pubkey_pkcs1_der(pubkey PublicKey) ![]u8PCKS8 prikey:
parse_prikey_pkcs8_der(bytes []u8) !PrivateKeymake_prikey_pkcs8_der(prikey PrivateKey) ![]u8PCKS8 pubkey:
parse_pubkey_pkcs8_der(bytes []u8) !PublicKeymake_pubkey_pkcs8_der(pubkey PublicKey) ![]u8- The library LICENSE is
Apache2, using the library need keep the LICENSE.
- Copyright deatil(https://github.com/deatil).