Line data Source code
1 : //! Tools for client/server/stored key management.
2 :
3 : /// Faithfully taken from PostgreSQL.
4 : pub const SCRAM_KEY_LEN: usize = 32;
5 :
6 : /// One of the keys derived from the [password](super::password::SaltedPassword).
7 : /// We use the same structure for all keys, i.e.
8 : /// `ClientKey`, `StoredKey`, and `ServerKey`.
9 124 : #[derive(Clone, Default, PartialEq, Eq, Debug)]
10 : #[repr(transparent)]
11 : pub struct ScramKey {
12 : bytes: [u8; SCRAM_KEY_LEN],
13 : }
14 :
15 : impl ScramKey {
16 79 : pub fn sha256(&self) -> Self {
17 79 : super::sha256([self.as_ref()]).into()
18 79 : }
19 :
20 74 : pub fn as_bytes(&self) -> [u8; SCRAM_KEY_LEN] {
21 74 : self.bytes
22 74 : }
23 : }
24 :
25 : impl From<[u8; SCRAM_KEY_LEN]> for ScramKey {
26 : #[inline(always)]
27 430 : fn from(bytes: [u8; SCRAM_KEY_LEN]) -> Self {
28 430 : Self { bytes }
29 430 : }
30 : }
31 :
32 : impl AsRef<[u8]> for ScramKey {
33 : #[inline(always)]
34 192 : fn as_ref(&self) -> &[u8] {
35 192 : &self.bytes
36 192 : }
37 : }
|