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
use sodiumoxide::crypto::sign::ed25519;
use error::{Error, Result};
use crypto::{PublicKey, SecretKey};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Signature {
sig: ed25519::Signature,
}
impl Signature {
pub fn sign(secret: &SecretKey, data: &[u8]) -> Signature {
let sig = ed25519::sign_detached(data, &secret.inner());
Signature { sig }
}
pub fn from_slice(sb: &[u8]) -> Result<Signature> {
let sig = ed25519::Signature::from_slice(sb).ok_or(Error::InvalidSignature)?;
Ok(Signature { sig })
}
pub fn len(&self) -> usize {
self.sig.0.len()
}
pub fn to_vec(&self) -> Vec<u8> {
self.sig.0.to_vec()
}
pub fn buf(&self) -> &[u8] {
&self.sig.0
}
pub fn verify(&self, public: &PublicKey, data: &[u8]) -> bool {
ed25519::verify_detached(&self.sig, data, &public.inner())
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SignatureHint(pub [u8; 4]);
impl SignatureHint {
pub fn from_public_key(pk: &PublicKey) -> SignatureHint {
let mut hint: [u8; 4] = Default::default();
let buf = pk.buf();
let len = buf.len();
hint.copy_from_slice(&buf[len - 4..len]);
SignatureHint(hint)
}
pub fn to_vec(&self) -> Vec<u8> {
self.0.to_vec()
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DecoratedSignature {
hint: SignatureHint,
signature: Signature,
}
impl DecoratedSignature {
pub fn new(hint: SignatureHint, signature: Signature) -> DecoratedSignature {
DecoratedSignature { hint, signature }
}
pub fn hint(&self) -> &SignatureHint {
&self.hint
}
pub fn signature(&self) -> &Signature {
&self.signature
}
}