-
Notifications
You must be signed in to change notification settings - Fork 2
/
rsa.c.v
54 lines (40 loc) · 793 Bytes
/
rsa.c.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
module tomcrypt
struct C.Rsa_key {}
struct RsaKey {
key C.Rsa_key
}
fn C.rsa_import() int
pub fn load_rsa_key(k []byte) RsaKey {
r := RsaKey {}
res := C.rsa_import(k.data, k.len, &r.key)
if res != 0 {
panic('unable to import key $res')
}
return r
}
fn C.rsa_encrypt_key() int
pub fn (key RsaKey) sha1_encrypt_key_into(@in []byte, mut out []byte) {
hash_idx := C.find_hash('sha1')
prng_idx := C.find_prng("sprng")
size := out.len
res := C.rsa_encrypt_key(
@in.data,
@in.len,
out.data,
&size,
C.NULL,
0,
C.NULL,
prng_idx,
hash_idx,
&key.key
)
if res != 0 {
panic('unable to encrypt @in $res')
}
}
pub fn (key RsaKey) sha1_encrypt_key(@in []byte) []byte {
mut out := []byte{len:@in.len*16}
key.sha1_encrypt_key_into(@in, mut out)
return out
}