11# -*- coding: utf-8 -*-
2- """Encryption for the authentication store."""
2+ """
3+ Encryption for the authentication store.
4+
5+ Passwords and cleartext are Unicode text strings. Ciphertext is a
6+ Unicode string containing base64-encoded data. The salt used for
7+ key derivation is an unencoded byte string representing raw byte
8+ data (the obsolete AES encryption method uses an unencoded or
9+ Unicode string containing base64 data directly).
10+ """
311
412import os
513import string
@@ -33,7 +41,7 @@ def __init__( self, password = None, salt = None ):
3341 salt = self .storage_key_salt ,
3442 iterations = 100000 ,
3543 backend = self .backend )
36- self .storage_key = base64 .urlsafe_b64encode ( kdf .derive ( password ) )
44+ self .storage_key = base64 .urlsafe_b64encode ( kdf .derive ( password . encode () ) )
3745
3846 @classmethod
3947 def GenerateSalt ( cls ):
@@ -62,7 +70,7 @@ def Encrypt( self, secret ):
6270 cleartext = padder .update ( secret .encode () )
6371 cleartext += padder .finalize ()
6472 ciphertext = f .encrypt ( cleartext .encode () )
65- return ciphertext
73+ return unicode ( ciphertext )
6674
6775 def Decrypt ( self , token ):
6876 """Decrypt a secret using the current key."""
@@ -95,7 +103,7 @@ def __init__( self, password = None, salt = None ):
95103 salt = self .storage_key_salt ,
96104 iterations = 10000 ,
97105 backend = self .backend )
98- self .storage_key = base64 . b64encode ( kdf .derive ( password ) )
106+ self .storage_key = kdf .derive ( password . encode ( ) )
99107
100108 @classmethod
101109 def GenerateSalt ( cls ):
@@ -126,7 +134,7 @@ def Decrypt( self, ciphertext ):
126134 b = base64 .standard_b64decode ( ciphertext )
127135 iv = b [0 :self .BLOCK_SIZE ]
128136 raw_ciphertext = b [self .BLOCK_SIZE :]
129- cipher = Cipher ( algorithms .AES ( base64 . b64decode ( self .storage_key ) ),
137+ cipher = Cipher ( algorithms .AES ( self .storage_key ),
130138 modes .CBC ( iv ), self .backend )
131139 decryptor = cipher .decryptor ()
132140 cleartext = decryptor .update ( raw_ciphertext ) + decryptor .finalize ()
@@ -150,23 +158,23 @@ def SetPassword( self, new_password, new_salt = None ):
150158 pass
151159
152160 def Encrypt ( self , cleartext ):
153- return cleartext
161+ raise NotImplementedError ( "Encryption is not supported by the cleartext algorithm." )
154162
155163 def Decrypt ( self , ciphertext ):
156164 return ciphertext
157165
158166def generate_salt ( algorithm_name ):
159167 if algorithm_name == 'FERNET-256' :
160- s = base64 . urlsafe_b64encode ( Fernet_256 .GenerateSalt () )
168+ salt = Fernet_256 .GenerateSalt ()
161169 else :
162170 raise NotImplementedError ( "Algorithm not implemented: " + algorithm_name )
163- return s
171+ return salt
164172
165173def create_encryption_object ( algorithm_name , password = None , salt = None ):
166174 if algorithm_name == 'FERNET-256' :
167- e = Fernet_256 ( password . encode () , salt )
175+ e = Fernet_256 ( password , salt )
168176 elif algorithm_name == 'AES' :
169- e = Old_AES ( password . encode () , salt )
177+ e = Old_AES ( password , salt )
170178 elif algorithm_name == 'cleartext' :
171179 e = Cleartext ()
172180 else :
0 commit comments