diff options
author | Jack Lloyd <[email protected]> | 2018-08-11 17:06:52 -0400 |
---|---|---|
committer | Jack Lloyd <[email protected]> | 2018-08-11 17:06:52 -0400 |
commit | 7fe0cc6081c608d796cdaf0f45b195dbb42841e4 (patch) | |
tree | 6f588f0ae1ec0974a15879af11051aecd06732b3 /src/python | |
parent | 5cde8edd3ff9f79a9f568a803a5d5189e6993813 (diff) |
Add scrypt to Python
Diffstat (limited to 'src/python')
-rwxr-xr-x | src/python/botan2.py | 27 |
1 files changed, 23 insertions, 4 deletions
diff --git a/src/python/botan2.py b/src/python/botan2.py index 5b7c84847..87f1aec86 100755 --- a/src/python/botan2.py +++ b/src/python/botan2.py @@ -196,6 +196,11 @@ botan.botan_pbkdf_timed.argtypes = [c_char_p, POINTER(c_char), c_size_t, c_char_ c_void_p, c_size_t, c_size_t, POINTER(c_size_t)] botan.botan_pbkdf_timed.errcheck = errcheck_for('botan_pbkdf_timed') +# Scrypt +botan.botan_scrypt.argtypes = [POINTER(c_char), c_size_t, c_char_p, POINTER(c_char), c_size_t, + c_size_t, c_size_t, c_size_t] +botan.botan_scrypt.errcheck = errcheck_for('botan_scrypt') + # KDF botan.botan_kdf.argtypes = [c_char_p, POINTER(c_char), c_size_t, POINTER(c_char), c_size_t, POINTER(c_char), c_size_t, POINTER(c_char), c_size_t] @@ -383,14 +388,14 @@ def _ctype_bits(s): if isinstance(s, str): return s else: - raise Exception("Internal error - unexpected type provided to _ctype_bits") + raise Exception("Internal error - unexpected type %s provided to _ctype_bits" % (type(s).__name__)) else: if isinstance(s, bytes): return s elif isinstance(s, str): return s.encode('utf-8') else: - raise Exception("Internal error - unexpected type provided to _ctype_bits") + raise Exception("Internal error - unexpected type %s provided to _ctype_bits" % (type(s).__name__)) def _ctype_bufout(buf): if version_info[0] < 3: @@ -591,13 +596,17 @@ def check_bcrypt(passwd, passwd_hash): # # PBKDF # -def pbkdf(algo, password, out_len, iterations=10000, salt=rng().get(12)): +def pbkdf(algo, password, out_len, iterations=10000, salt=None): + if salt is None: + salt = rng().get(12) out_buf = create_string_buffer(out_len) botan.botan_pbkdf(_ctype_str(algo), out_buf, out_len, _ctype_str(password), salt, len(salt), iterations) return (salt, iterations, out_buf.raw) -def pbkdf_timed(algo, password, out_len, ms_to_run=300, salt=rng().get(12)): +def pbkdf_timed(algo, password, out_len, ms_to_run=300, salt=None): + if salt is None: + salt = rng().get(12) out_buf = create_string_buffer(out_len) iterations = c_size_t(0) botan.botan_pbkdf_timed(_ctype_str(algo), out_buf, out_len, _ctype_str(password), @@ -605,6 +614,16 @@ def pbkdf_timed(algo, password, out_len, ms_to_run=300, salt=rng().get(12)): return (salt, iterations.value, out_buf.raw) # +# Scrypt +# +def scrypt(out_len, password, salt, n=1024, r=8, p=8): + out_buf = create_string_buffer(out_len) + botan.botan_scrypt(out_buf, out_len, _ctype_str(password), + _ctype_bits(salt), len(salt), n, r, p) + + return out_buf.raw + +# # KDF # def kdf(algo, secret, out_len, salt, label): |