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
use std::num::TryFromIntError;
use base64::DecodeError;
use ctr::cipher::StreamCipherError;
use scrypt::errors::{InvalidOutputLen, InvalidParams};

#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) enum DerivedKeyError {
    Base64Decode(DecodeError),
    IntError(TryFromIntError),
    InvalidScryptParams(InvalidParams),
    InvalidOutputLen(InvalidOutputLen),
}

impl From<DecodeError> for DerivedKeyError {
    fn from(e: DecodeError) -> Self {
        Self::Base64Decode(e)
    }
}

impl From<TryFromIntError> for DerivedKeyError {
    fn from(e: TryFromIntError) -> Self {
        Self::IntError(e)
    }
}

impl From<InvalidParams> for DerivedKeyError {
    fn from(e: InvalidParams) -> Self {
        Self::InvalidScryptParams(e)
    }
}

impl From<InvalidOutputLen> for DerivedKeyError {
    fn from(e: InvalidOutputLen) -> Self {
        Self::InvalidOutputLen(e)
    }
}

#[derive(Clone, Debug)]
pub(crate) enum EncryptError {
    StreamCipher(StreamCipherError)
}

impl From<StreamCipherError> for EncryptError {
    fn from(e: StreamCipherError) -> Self {
        Self::StreamCipher(e)
    }
}

#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub enum VerifyPasswordError {
    GenerateDerivedKeyFailed,
    DecodingFailed,
    EncryptionFailed,
}

impl From<DecodeError> for VerifyPasswordError {
    fn from(_: DecodeError) -> Self {
        Self::DecodingFailed
    }
}

impl From<EncryptError> for VerifyPasswordError {
    fn from(_: EncryptError) -> Self {
        Self::EncryptionFailed
    }
}

impl From<DerivedKeyError> for VerifyPasswordError {
    fn from(_: DerivedKeyError) -> Self {
        Self::GenerateDerivedKeyFailed
    }
}