1use argon2::{Argon2, Params};
2use bincode::{Decode, Encode};
3use ed25519_dalek::SigningKey;
4use ed25519_dalek::ed25519::Error;
5use ed25519_dalek::ed25519::signature::SignerMut;
6use ed25519_dalek::{Signature as DalekSignature, VerifyingKey};
7use num_bigint::BigUint;
8use serde::{Deserialize, Serialize};
9use std::fmt;
10use std::ops::Deref;
11
12use keys::{Private, Public};
13
14pub mod keys;
16
17pub const MAGIC_BYTES: [u8; 10] = [205, 198, 59, 175, 94, 82, 224, 9, 114, 173];
18
19pub fn argon2_hash(input: &[u8]) -> [u8; 32] {
20 let params = Params::new(8 * 1024, 1, 2, Some(32)).unwrap();
22 let argon2 = Argon2::new(argon2::Algorithm::Argon2id, argon2::Version::V0x13, params);
23 let mut hash = [0u8; 32];
24 argon2
25 .hash_password_into(input, &MAGIC_BYTES, &mut hash)
26 .unwrap();
27 hash
28}
29
30#[derive(Clone, Copy, PartialEq, Eq, Encode, Decode, std::hash::Hash)]
33pub struct Hash([u8; 32]);
34
35impl Hash {
36 pub fn new(data: &[u8]) -> Self {
38 Hash(argon2_hash(data))
39 }
40
41 pub const fn new_from_buf(hash_buf: [u8; 32]) -> Self {
43 Hash(hash_buf)
44 }
45
46 pub fn compare_with_data(&self, other_data: &[u8]) -> bool {
48 let computed = argon2_hash(other_data);
49 computed == self.0
50 }
51
52 pub fn new_from_base36(s: &str) -> Option<Self> {
54 let big_int = BigUint::parse_bytes(s.as_bytes(), 36)?;
56 let mut buf = big_int.to_bytes_be();
57
58 if buf.len() > 32 {
60 return None;
61 } else if buf.len() < 32 {
62 let mut padded = vec![0u8; 32 - buf.len()];
64 padded.extend(buf);
65 buf = padded;
66 }
67
68 let buf: [u8; 32] = buf.try_into().ok()?;
70
71 Some(Hash(buf))
72 }
73
74 pub fn dump_base36(&self) -> String {
75 let big_int = BigUint::from_bytes_be(&self.0);
76 big_int.to_str_radix(36)
77 }
78
79 pub fn dump_buf(&self) -> [u8; 32] {
80 self.0
81 }
82}
83
84impl Serialize for Hash {
85 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
86 where
87 S: serde::Serializer,
88 {
89 serializer.serialize_str(&self.dump_base36())
90 }
91}
92
93impl<'de> Deserialize<'de> for Hash {
94 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
95 where
96 D: serde::Deserializer<'de>,
97 {
98 let s = String::deserialize(deserializer)?;
99 Self::new_from_base36(&s).ok_or_else(|| serde::de::Error::custom("Invalid base36 hash"))
100 }
101}
102
103impl Deref for Hash {
104 type Target = [u8; 32];
105 fn deref(&self) -> &Self::Target {
106 &self.0
107 }
108}
109
110impl fmt::Debug for Hash {
111 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
112 write!(f, "Hash: {}", self.dump_base36())
113 }
114}
115
116#[derive(Clone, PartialEq, Eq, Encode, Decode, Copy, Hash)]
118pub struct Signature([u8; 64]);
119
120impl Signature {
121 pub fn new_signature(private: &mut Private, data: &[u8]) -> Self {
123 let mut key = SigningKey::from_bytes(private.dump_buf());
124 let signature = key.sign(data);
125 Signature(signature.to_bytes()) }
127
128 pub fn new_from_buf(signature: &[u8; 64]) -> Self {
130 Signature(signature.clone())
131 }
132
133 pub fn validate_with_public(&self, public: &Public, data: &[u8]) -> Result<bool, Error> {
135 let key = VerifyingKey::from_bytes(public.dump_buf())?;
136 Ok(key
137 .verify_strict(data, &DalekSignature::from_bytes(&self.0))
138 .is_ok())
139 }
140
141 pub fn validate_with_private(&self, private: &Private, data: &[u8]) -> Result<bool, Error> {
143 let key = SigningKey::from_bytes(private.dump_buf());
144 Ok(key
145 .verify_strict(data, &DalekSignature::from_bytes(&self.0))
146 .is_ok())
147 }
148
149 pub fn new_from_base36(s: &str) -> Option<Self> {
151 let big_int = BigUint::parse_bytes(s.as_bytes(), 36)?;
153 let mut buf = big_int.to_bytes_be();
154
155 if buf.len() > 64 {
157 return None;
158 } else if buf.len() < 64 {
159 let mut padded = vec![0u8; 64 - buf.len()];
161 padded.extend(buf);
162 buf = padded;
163 }
164
165 let buf: [u8; 64] = buf.try_into().ok()?;
167
168 Some(Self(buf))
169 }
170
171 pub fn dump_base36(&self) -> String {
172 let big_int = BigUint::from_bytes_be(&self.0);
173 big_int.to_str_radix(36)
174 }
175
176 pub fn dump_buf(&self) -> [u8; 64] {
177 self.0
178 }
179}
180
181impl Serialize for Signature {
182 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
183 where
184 S: serde::Serializer,
185 {
186 serializer.serialize_str(&self.dump_base36())
187 }
188}
189
190impl<'de> Deserialize<'de> for Signature {
191 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
192 where
193 D: serde::Deserializer<'de>,
194 {
195 let s = String::deserialize(deserializer)?;
196 Self::new_from_base36(&s).ok_or_else(|| serde::de::Error::custom("Invalid base36 signature"))
197 }
198}
199
200impl Deref for Signature {
201 type Target = [u8; 64]; fn deref(&self) -> &Self::Target {
203 &self.0
204 }
205}
206
207impl fmt::Debug for Signature {
208 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
209 write!(f, "Signature: {}", self.dump_base36())
210 }
211}