1- #[ cfg( any( feature = "alloc" , feature = "std" ) ) ]
2- use alloc:: string:: String ;
31use core:: fmt;
42
53#[ cfg( feature = "std" ) ]
64use std:: error;
7- #[ cfg( feature = "std" ) ]
8- use std:: io;
95
106/// Library generic result type.
117pub type BcryptResult < T > = Result < T , BcryptError > ;
@@ -14,22 +10,19 @@ pub type BcryptResult<T> = Result<T, BcryptError>;
1410/// All the errors we can encounter while hashing/verifying
1511/// passwords
1612pub enum BcryptError {
17- #[ cfg( feature = "std" ) ]
18- Io ( io:: Error ) ,
13+ /// Raised when the cost value is outside of the allowed 4-31 range.
14+ ///
15+ /// Cost is provided as an argument to hashing functions, and extracted from the hash in
16+ /// verification functions.
1917 CostNotAllowed ( u32 ) ,
18+ /// Raised when verifying against an incorrectly formatted hash.
2019 #[ cfg( any( feature = "alloc" , feature = "std" ) ) ]
21- InvalidCost ( String ) ,
22- #[ cfg( any( feature = "alloc" , feature = "std" ) ) ]
23- InvalidPrefix ( String ) ,
24- #[ cfg( any( feature = "alloc" , feature = "std" ) ) ]
25- InvalidHash ( String ) ,
26- InvalidSaltLen ( usize ) ,
27- InvalidBase64 ( base64:: DecodeError ) ,
20+ InvalidHash ( & ' static str ) ,
21+ /// Raised when an error occurs when generating a salt value.
2822 #[ cfg( any( feature = "alloc" , feature = "std" ) ) ]
2923 Rand ( getrandom:: Error ) ,
30- /// Return this error if the input contains more than 72 bytes. This variant contains the
31- /// length of the input in bytes.
32- /// Only returned when calling `non_truncating_*` functions
24+ /// Raised when the input to a `non_truncating_*` function contains more than 72 bytes.
25+ /// This variant contains the length of the input in bytes.
3326 Truncation ( usize ) ,
3427}
3528
@@ -43,19 +36,12 @@ macro_rules! impl_from_error {
4336 } ;
4437}
4538
46- impl_from_error ! ( base64:: DecodeError , BcryptError :: InvalidBase64 ) ;
47- #[ cfg( feature = "std" ) ]
48- impl_from_error ! ( io:: Error , BcryptError :: Io ) ;
4939#[ cfg( any( feature = "alloc" , feature = "std" ) ) ]
5040impl_from_error ! ( getrandom:: Error , BcryptError :: Rand ) ;
5141
5242impl fmt:: Display for BcryptError {
5343 fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
5444 match * self {
55- #[ cfg( feature = "std" ) ]
56- BcryptError :: Io ( ref err) => write ! ( f, "IO error: {}" , err) ,
57- #[ cfg( any( feature = "alloc" , feature = "std" ) ) ]
58- BcryptError :: InvalidCost ( ref cost) => write ! ( f, "Invalid Cost: {}" , cost) ,
5945 BcryptError :: CostNotAllowed ( ref cost) => write ! (
6046 f,
6147 "Cost needs to be between {} and {}, got {}" ,
@@ -64,13 +50,7 @@ impl fmt::Display for BcryptError {
6450 cost
6551 ) ,
6652 #[ cfg( any( feature = "alloc" , feature = "std" ) ) ]
67- BcryptError :: InvalidPrefix ( ref prefix) => write ! ( f, "Invalid Prefix: {}" , prefix) ,
68- #[ cfg( any( feature = "alloc" , feature = "std" ) ) ]
69- BcryptError :: InvalidHash ( ref hash) => write ! ( f, "Invalid hash: {}" , hash) ,
70- BcryptError :: InvalidBase64 ( ref err) => write ! ( f, "Base64 error: {}" , err) ,
71- BcryptError :: InvalidSaltLen ( len) => {
72- write ! ( f, "Invalid salt len: expected 16, received {}" , len)
73- }
53+ BcryptError :: InvalidHash ( ref reason) => write ! ( f, "Invalid hash: {}" , reason) ,
7454 #[ cfg( any( feature = "alloc" , feature = "std" ) ) ]
7555 BcryptError :: Rand ( ref err) => write ! ( f, "Rand error: {}" , err) ,
7656 BcryptError :: Truncation ( len) => {
@@ -84,14 +64,9 @@ impl fmt::Display for BcryptError {
8464impl error:: Error for BcryptError {
8565 fn source ( & self ) -> Option < & ( dyn error:: Error + ' static ) > {
8666 match * self {
87- BcryptError :: Io ( ref err) => Some ( err) ,
88- BcryptError :: InvalidCost ( _)
89- | BcryptError :: CostNotAllowed ( _)
90- | BcryptError :: InvalidPrefix ( _)
67+ BcryptError :: CostNotAllowed ( _)
9168 | BcryptError :: InvalidHash ( _)
92- | BcryptError :: InvalidSaltLen ( _)
9369 | BcryptError :: Truncation ( _) => None ,
94- BcryptError :: InvalidBase64 ( ref err) => Some ( err) ,
9570 BcryptError :: Rand ( ref err) => Some ( err) ,
9671 }
9772 }
0 commit comments