Example #1
1
 def encrypt_key_file(self, data, passphrase):
     salt = get_random_bytes(32)
     iterations = 100000
     key = pbkdf2_sha256(passphrase.encode('utf-8'), salt, iterations, 32)
     hash = HMAC(key, data, sha256).digest()
     cdata = AES(key).encrypt(data)
     d = {
         'version': 1,
         'salt': salt,
         'iterations': iterations,
         'algorithm': 'sha256',
         'hash': hash,
         'data': cdata,
     }
     return msgpack.packb(d)
Example #2
0
 def test_pbkdf2_sha256(self):
     self.assert_equal(hexlify(pbkdf2_sha256(b'password', b'salt', 1, 32)),
                      b'120fb6cffcf8b32c43e7225256c4f837a86548c92ccc35480805987cb70be17b')
     self.assert_equal(hexlify(pbkdf2_sha256(b'password', b'salt', 2, 32)),
                      b'ae4d0c95af6b46d32d0adff928f06dd02a303f8ef3c251dfd6e2d85a95474c43')
     self.assert_equal(hexlify(pbkdf2_sha256(b'password', b'salt', 4096, 32)),
                      b'c5e478d59288c841aa530db6845c4c8d962893a001ce4e11a4963873aa98134a')
Example #3
-1
 def decrypt_key_file(self, data, passphrase):
     d = msgpack.unpackb(data)
     assert d[b'version'] == 1
     assert d[b'algorithm'] == b'sha256'
     key = pbkdf2_sha256(passphrase.encode('utf-8'), d[b'salt'], d[b'iterations'], 32)
     data = AES(key).decrypt(d[b'data'])
     if HMAC(key, data, sha256).digest() != d[b'hash']:
         return None
     return data
Example #4
-1
 def init(self, repository, passphrase):
     self.init_from_random_data(pbkdf2_sha256(passphrase.encode('utf-8'), repository.id, self.iterations, 100))
     self.init_ciphers()
 def test_pbkdf2_sha256(self):
     self.assert_equal(hexlify(pbkdf2_sha256(b'password', b'salt', 1, 32)),
                      b'120fb6cffcf8b32c43e7225256c4f837a86548c92ccc35480805987cb70be17b')
     self.assert_equal(hexlify(pbkdf2_sha256(b'password', b'salt', 2, 32)),
                      b'ae4d0c95af6b46d32d0adff928f06dd02a303f8ef3c251dfd6e2d85a95474c43')
     self.assert_equal(hexlify(pbkdf2_sha256(b'password', b'salt', 4096, 32)),
                      b'c5e478d59288c841aa530db6845c4c8d962893a001ce4e11a4963873aa98134a')