-
Notifications
You must be signed in to change notification settings - Fork 9
/
sign.v
103 lines (87 loc) · 2.42 KB
/
sign.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
module libsodium
const sign_len = 64
pub struct SigningKey {
secret_key [secret_key_size]u8
pub:
verify_key VerifyKey
}
pub struct VerifyKey {
pub:
public_key [public_key_size]u8
}
pub fn new_signing_key(public_key [public_key_size]u8, secret_key [secret_key_size]u8) SigningKey {
res := SigningKey{}
unsafe {
C.memcpy(&res.verify_key.public_key[0], &public_key[0], public_key.len)
C.memcpy(&res.secret_key[0], &secret_key[0], secret_key.len)
}
return res
}
pub fn generate_ed25519_signing_key() SigningKey {
mut pk := SigningKey{
secret_key: [secret_key_size]u8{}
verify_key: VerifyKey{
public_key: [public_key_size]u8{}
}
}
x := crypto_sign_ed25519_keypair(pk.verify_key.public_key[0], pk.secret_key[0])
if x != 0 {
// TODO handle errors
}
return pk
}
pub fn generate_signing_key() SigningKey {
res := SigningKey{}
C.crypto_sign_keypair(&res.verify_key.public_key[0], &res.secret_key[0])
return res
}
pub fn new_ed25519_signing_key_seed(seed []u8) SigningKey {
mut pk := SigningKey{
secret_key: [secret_key_size]u8{}
verify_key: VerifyKey{
public_key: [public_key_size]u8{}
}
}
x := crypto_sign_ed25519_seed_keypair(pk.verify_key.public_key[0], pk.secret_key[0],
seed.data)
if x != 0 {
// TODO handle errors
}
return pk
}
pub fn new_signing_key_seed(seed []u8) SigningKey {
res := SigningKey{}
C.crypto_sign_seed_keypair(&res.verify_key.public_key[0], &res.secret_key[0], seed.data)
return res
}
pub fn (key VerifyKey) verify_string(s string) bool {
len := s.len - libsodium.sign_len
buf := []u8{len: len}
mut buf_len := u64(0)
if C.crypto_sign_open(buf.data, C.ULLCAST(&buf_len), s.str, s.len, &key.public_key[0]) != 0 {
return false
}
return true
}
pub fn (key SigningKey) sign_string(s string) string {
buf_size := libsodium.sign_len + s.len
mut buf := unsafe { vcalloc(buf_size) }
mut buf_len := u64(0)
C.crypto_sign(buf, C.ULLCAST(&buf_len), s.str, s.len, &key.secret_key[0])
return unsafe { buf.vstring_with_len(int(buf_len)) }
}
pub fn (key VerifyKey) verify(b []u8) bool {
len := b.len - libsodium.sign_len
buf := []u8{len: len}
mut buf_len := u64(0)
if C.crypto_sign_open(buf.data, C.ULLCAST(&buf_len), b.data, b.len, &key.public_key[0]) != 0 {
return false
}
return true
}
pub fn (key SigningKey) sign(b []u8) []u8 {
buf := []u8{len: libsodium.sign_len + b.len}
mut buf_len := u64(0)
C.crypto_sign(buf.data, C.ULLCAST(&buf_len), b.data, b.len, &key.secret_key[0])
return buf
}