1+ use aes_gcm:: aead:: rand_core:: RngCore ;
12use aes_gcm:: aead:: { Aead , KeyInit , OsRng } ;
23use aes_gcm:: { AeadCore , Aes256Gcm , Key , Nonce } ;
3- use base64:: engine:: general_purpose:: STANDARD as BASE64 ;
44use base64:: Engine ;
5+ use base64:: engine:: general_purpose:: STANDARD as BASE64 ;
56use hkdf:: Hkdf ;
6- use aes_gcm:: aead:: rand_core:: RngCore ;
77use sha2:: Sha256 ;
88use zeroize:: Zeroize ;
99
10- use super :: account_repository:: { DecryptedPassword , EncryptedPassword , PasswordTransformer , PasswordTransformerError } ;
10+ use super :: account_repository:: {
11+ DecryptedPassword , EncryptedPassword , PasswordTransformer , PasswordTransformerError ,
12+ } ;
1113
1214const SALT_SIZE : usize = 32 ;
1315
@@ -85,19 +87,22 @@ impl AesGcmPasswordTransformer {
8587
8688#[ uniffi:: export]
8789impl PasswordTransformer for AesGcmPasswordTransformer {
88- fn encrypt ( & self , password : DecryptedPassword ) -> Result < EncryptedPassword , PasswordTransformerError > {
90+ fn encrypt (
91+ & self ,
92+ password : DecryptedPassword ,
93+ ) -> Result < EncryptedPassword , PasswordTransformerError > {
8994 let mut salt = [ 0u8 ; SALT_SIZE ] ;
9095 OsRng . fill_bytes ( & mut salt) ;
9196
9297 let mut key = self . derive_key ( & salt) ;
9398 let cipher = Aes256Gcm :: new ( & key) ;
9499 let nonce = Aes256Gcm :: generate_nonce ( & mut OsRng ) ;
95100
96- let result = cipher
97- . encrypt ( & nonce, password. 0 . as_bytes ( ) )
98- . map_err ( |e| PasswordTransformerError :: EncryptionFailed {
101+ let result = cipher. encrypt ( & nonce, password. 0 . as_bytes ( ) ) . map_err ( |e| {
102+ PasswordTransformerError :: EncryptionFailed {
99103 reason : e. to_string ( ) ,
100- } ) ;
104+ }
105+ } ) ;
101106 key. zeroize ( ) ;
102107 let ciphertext = result?;
103108
@@ -110,40 +115,60 @@ impl PasswordTransformer for AesGcmPasswordTransformer {
110115 Ok ( EncryptedPassword ( encoded) )
111116 }
112117
113- fn decrypt ( & self , password : EncryptedPassword ) -> Result < DecryptedPassword , PasswordTransformerError > {
118+ fn decrypt (
119+ & self ,
120+ password : EncryptedPassword ,
121+ ) -> Result < DecryptedPassword , PasswordTransformerError > {
114122 let parse_err = |msg : & str | PasswordTransformerError :: DecryptionFailed {
115123 reason : msg. to_string ( ) ,
116124 } ;
117125
118126 let mut parts = password. 0 . splitn ( 3 , ':' ) ;
119- let salt_b64 = parts. next ( ) . ok_or_else ( || parse_err ( "invalid encrypted password format" ) ) ?;
120- let nonce_b64 = parts. next ( ) . ok_or_else ( || parse_err ( "invalid encrypted password format" ) ) ?;
121- let ciphertext_b64 = parts. next ( ) . ok_or_else ( || parse_err ( "invalid encrypted password format" ) ) ?;
122-
123- let salt = BASE64 . decode ( salt_b64) . map_err ( |e| PasswordTransformerError :: DecryptionFailed {
124- reason : e. to_string ( ) ,
125- } ) ?;
126- let nonce_bytes = BASE64 . decode ( nonce_b64) . map_err ( |e| PasswordTransformerError :: DecryptionFailed {
127- reason : e. to_string ( ) ,
128- } ) ?;
127+ let salt_b64 = parts
128+ . next ( )
129+ . ok_or_else ( || parse_err ( "invalid encrypted password format" ) ) ?;
130+ let nonce_b64 = parts
131+ . next ( )
132+ . ok_or_else ( || parse_err ( "invalid encrypted password format" ) ) ?;
133+ let ciphertext_b64 = parts
134+ . next ( )
135+ . ok_or_else ( || parse_err ( "invalid encrypted password format" ) ) ?;
136+
137+ let salt =
138+ BASE64
139+ . decode ( salt_b64)
140+ . map_err ( |e| PasswordTransformerError :: DecryptionFailed {
141+ reason : e. to_string ( ) ,
142+ } ) ?;
143+ let nonce_bytes =
144+ BASE64
145+ . decode ( nonce_b64)
146+ . map_err ( |e| PasswordTransformerError :: DecryptionFailed {
147+ reason : e. to_string ( ) ,
148+ } ) ?;
129149 if nonce_bytes. len ( ) != 12 {
130150 return Err ( PasswordTransformerError :: DecryptionFailed {
131- reason : format ! ( "invalid nonce length: expected 12, got {}" , nonce_bytes. len( ) ) ,
151+ reason : format ! (
152+ "invalid nonce length: expected 12, got {}" ,
153+ nonce_bytes. len( )
154+ ) ,
132155 } ) ;
133156 }
134157 let nonce = Nonce :: from_slice ( & nonce_bytes) ;
135- let ciphertext = BASE64 . decode ( ciphertext_b64) . map_err ( |e| PasswordTransformerError :: DecryptionFailed {
136- reason : e. to_string ( ) ,
158+ let ciphertext = BASE64 . decode ( ciphertext_b64) . map_err ( |e| {
159+ PasswordTransformerError :: DecryptionFailed {
160+ reason : e. to_string ( ) ,
161+ }
137162 } ) ?;
138163
139164 let mut key = self . derive_key ( & salt) ;
140165 let cipher = Aes256Gcm :: new ( & key) ;
141166
142- let result = cipher
143- . decrypt ( & nonce, ciphertext. as_ref ( ) )
144- . map_err ( |e| PasswordTransformerError :: DecryptionFailed {
167+ let result = cipher. decrypt ( nonce, ciphertext. as_ref ( ) ) . map_err ( |e| {
168+ PasswordTransformerError :: DecryptionFailed {
145169 reason : e. to_string ( ) ,
146- } ) ;
170+ }
171+ } ) ;
147172 key. zeroize ( ) ;
148173 let plaintext = result?;
149174
0 commit comments