Skip to content

Latest commit

 

History

21 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

V-RSA

A RSA library for vlang.

Env

  • vlang >= 0.5.2

Adding v-rsa as a dependency

Add the dependency to your project:

v install deatil.vrsa

or

v install --git https://github.com/deatil/v-rsa

The v-rsa can be imported in your application with:

import deatil.vrsa.rsa

Get Starting

module 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}')
}

RSA functions

Generate key:

generate_key(mut random rand.PRNG, bits int) !PrivateKey

PKCS1v15 sign:

sign_pkcs1v15(priv PrivateKey, hasher IHasher, hashed []u8) ![]u8
verify_pkcs1v15(pubkey PublicKey, hasher IHasher, hashed []u8, sig []u8) !

PKCS1v15 encrypt:

encrypt_pkcs1v15(mut random rand.PRNG, pubkey PublicKey, msg []u8) ![]u8
decrypt_pkcs1v15(priv PrivateKey, ciphertext []u8) ![]u8

OAEP encrypt:

encrypt_oaep(mut h hash.Hash, mut random rand.PRNG, pubkey PublicKey, msg []u8, label []u8) ![]u8
decrypt_oaep(mut h hash.Hash, priv PrivateKey, ciphertext []u8, label []u8) ![]u8

PSS 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) !

PKCS1v15 Sign Hasher

  • hasher_md5: rsa.hasher_md5
  • hasher_sha1: rsa.hasher_sha1
  • hasher_sha224: rsa.hasher_sha224
  • hasher_sha256: rsa.hasher_sha256
  • hasher_sha384: rsa.hasher_sha384
  • hasher_sha512: rsa.hasher_sha512
  • hasher_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()
	}
}

Parse PublicKey

PCKS1 prikey:

parse_prikey_pkcs1_der(bytes []u8) !PrivateKey
make_prikey_pkcs1_der(prikey PrivateKey) ![]u8

PCKS1 pubkey:

parse_pubkey_pkcs1_der(bytes []u8) !PublicKey
make_pubkey_pkcs1_der(pubkey PublicKey) ![]u8

PCKS8 prikey:

parse_prikey_pkcs8_der(bytes []u8) !PrivateKey
make_prikey_pkcs8_der(prikey PrivateKey) ![]u8

PCKS8 pubkey:

parse_pubkey_pkcs8_der(bytes []u8) !PublicKey
make_pubkey_pkcs8_der(pubkey PublicKey) ![]u8

LICENSE

  • The library LICENSE is Apache2, using the library need keep the LICENSE.

Copyright

About

An RSA library for vlang.

Topics

Resources

Security policy

Stars

1 star

Watchers

1 watching

Forks

Releases

Contributors

Languages