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

hashlib 모듈 - blake2b 메서드(method)


/// 설명

RFC 7693 에 정의되어 있는 암호화 해시 함수입니다.

data: 해시를 적용할 메세지입니다.
digest_size: 해시함수를 통해 반환되는 해시의 크기를 지정합니다.
key: 해시함수에 적용할 비밀키입니다.
salt: 난수를 사용하여 해시를 생성합니다.
person: 다른 인자의 값이 같고, 해시의 목적이 다를 경우 사용합니다.(인자의 값이 같지 않아도 됩니다.)
fanout: 참고사이트
depth: 참고사이트
leaf_size: 참고사이트
node_offset: 참고사이트
node_depth: 참고사이트
inner_size: 함수 내부에서 사용되는 digest 크기
last_node: 참고사이트
usedforsecurity: 제한적인 상황에서 안전하지 않을 경우 False를 이용할 수 있습니다.

※ 형식
hashlib.blake2b(data=b'', *, digest_size=64, key=b'', salt=b'', person=b'', fanout=1, depth=1, leaf_size=0, node_offset=0, node_depth=0, inner_size=0, last_node=False, usedforsecurity=True)

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
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import os
from hashlib import blake2b
 
# RFC 7693
# digest_size     len(key)     len(salt)     len(person)
# 64              64           16            16
 
test_str = b'This is a string'
 
# Basic Hashing(Tree=sequential)
test = blake2b(digest_size=32)
test.update(test_str)
print(test.hexdigest())  # c07779197b2719fa7c6767a71385ebd018a4032558e6288d2ef58d4b6f42da30
 
# Keyed Hashing(Tree=sequential)
test = blake2b(key=b'use_your_key', digest_size=32)
test.update(test_str)
print(test.hexdigest())  # 574ca103b76fb0b60d4bbdc0a20b9adba20e0a3558694b76e5eaa3a686317bd7
 
# Randomized(salt) + Keyed Hashing(Tree=sequential)
salt = os.urandom(blake2b.SALT_SIZE)
test = blake2b(key=b'use_your_key', salt=salt, digest_size=32)
test.update(test_str)
print(test.hexdigest())  # 8b635ab4c91cf7cbd8ce9a347ccd3d29f71c76664948eb8864d298adaa416c63
 
# Keyed + Personalization Hashing(Tree=sequential)
test = blake2b(key=b'use_your_key', person=b'Task_1', digest_size=32)
test.update(test_str)
print(test.hexdigest())  # 5b85c7742db56d45b454d83989f26fc84f478a65008e818d657bdc87065d8a5f
 
# Keyed + Personalization Hashing
test = blake2b(key=b'use_your_key', person=b'Task_1', digest_size=32,
               fanout=2)
test.update(test_str)
print(test.hexdigest())  # 7bf287b04683f7dd55c584925a5fda689548b31f8470cc7408148dcd50071512
 
# Keyed + Personalization Hashing
test = blake2b(key=b'use_your_key', person=b'Task_1', digest_size=32,
               fanout=2, depth=2)
test.update(test_str)
print(test.hexdigest())  # 5399bd259a8c50f045c58d9513072912d14375adf9eaa39ba0cd93e8fc4415b2
 
# Keyed + Personalization Hashing
test = blake2b(key=b'use_your_key', person=b'Task_1', digest_size=32,
               fanout=2, depth=2, leaf_size=1024)
test.update(test_str)
print(test.hexdigest())  # 9dfa62ed6342fb4f641d04514064bf5ddc60df55d1e80645c74262628e6c7bdc
 
# Keyed + Personalization Hashing
test = blake2b(key=b'use_your_key', person=b'Task_1', digest_size=32,
               fanout=2, depth=2, leaf_size=1024, inner_size=32)
test.update(test_str)
print(test.hexdigest())  # c99880d7f4fd6ac46ef83c0204741c56ca5189c2480caac00828c306ea489492
 
# Keyed + Personalization Hashing
test = blake2b(key=b'use_your_key', person=b'Task_1', digest_size=32,
               fanout=2, depth=2, leaf_size=1024, inner_size=32, last_node=True)
test.update(test_str)
print(test.hexdigest())  # c6dca8e226ba72df25092db473457414f9835543f66f882c12109738242c4799
 
cs

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


– 당신을 응원합니다. –

댓글

이 블로그의 인기 게시물

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

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

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

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

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