파이썬[Python]: hashlib - scrypt 메서드

hashlib 모듈 - scrypt 메서드(method)


/// 설명

패스워드를 이용하여 키를 생성하는 scrypt 함수입니다.

※ 형식
hashlib.scrypt(password, *, salt, n, r, p)
hashlib.scrypt(password, *, salt, n, r, p, maxmem=0, dklen=64)

reference
https://docs.python.org/3/library/hashlib.html#module-hashlib

/// 예제

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import os
import hashlib
 
# RFC 7914
test_pass = b'password'  # password
test_salt = os.urandom(16)  # bytes
test_n = 8  # 1 < The CPU/Memory cost < 2^(128 * r / 8)(a power of 2 (e.g. 1024))
test_r = 8  # block_size
test_p = 1  # 0 < The parallelization(CPU parallelism) <= ((2^32-1) * 32) / (128 * r)
test_maxmem = 0  # limitation of memory(OpenSSL 1.1.0 default 32MiB)
test_dklen = 16  # 0 < length of key <= (2^32 - 1) * 32
 
# producing a derived key
test_key = hashlib.scrypt(test_pass, salt=test_salt, n=test_n, r=test_r, p=test_p)
print(test_key)
# b"R\xcdq\xb3\x99;\xa4\xe2\xe15\xe4\x02'\x90\xe5\xd2sH\xc2[\xa8\x04\xe7\x1f\xebB\x9fWP4[\xfdiW\xdd\xa3m\xe4@\x8b\xaae\x96\xbf\xeb\x0ft\tu;X\xa5W\xf97\x153a\x93\x97U\xb3e\xec"
print(test_key.hex())
# 52cd71b3993ba4e2e135e4022790e5d27348c25ba804e71feb429f5750345bfd6957dda36de4408baa6596bfeb0f7409753b58a557f937153361939755b365ec
 
# producing a derived key
test_key = hashlib.scrypt(test_pass, salt=test_salt, n=test_n, r=test_r, p=test_p, dklen=test_dklen)
print(test_key)  # b"R\xcdq\xb3\x99;\xa4\xe2\xe15\xe4\x02'\x90\xe5\xd2"
print(test_key.hex())  # 52cd71b3993ba4e2e135e4022790e5d2
 
cs

* 실행환경: Microsoft Windows 10 Homes
* 인터프리터: 파이썬(Python 3.9)


– 당신을 응원합니다. –

댓글

이 블로그의 인기 게시물

파이썬[Python]: 내장함수 - from_bytes 메서드

파이썬[Python]: 내장함수 - __len__ 메서드

파이썬[Python]: kivy - 한글 사용

파이썬[Python]: 내장함수 - bit_length 메서드

C 언어: sin 함수, cos 함수, tan 함수