Skip to content

Instantly share code, notes, and snippets.

@ziwon
Created April 23, 2018 23:37
Show Gist options
  • Save ziwon/84e928a75e5e5a3abff0f28a3e7e1668 to your computer and use it in GitHub Desktop.
Save ziwon/84e928a75e5e5a3abff0f28a3e7e1668 to your computer and use it in GitHub Desktop.
Spring Security 모듈의 StandardPasswordEncoder encode() 함수를 구현
def sha256_encode(secret, password):
"""
Spring Security 모듈의 StandardPasswordEncoder encode() 함수를 구현
:param secret:
:param password:
:return:
"""
salt_key_length = 8
max_hash_rounds = 1024
def gen_salt(n): return bytearray(map(random.getrandbits, (8,) * n))
def concatenate(list_of_array):
length = 0
for arr in list_of_array:
length += len(arr)
new_array = bytearray(length)
pos = 0
for arr in list_of_array:
new_array[pos:] = arr
pos += len(arr)
return new_array
salt = gen_salt(salt_key_length)
summed = concatenate([salt,
secret.encode('utf-8'),
password.encode('utf-8')])
digested = summed
for _ in range(max_hash_rounds):
m = hashlib.sha256()
m.update(digested)
digested = m.digest()
ret = concatenate([salt, digested])
return binascii.hexlify(ret)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment