Skip to content

Commit f65ce06

Browse files
committed
Add Hash.base64.
1 parent 0f3a4e4 commit f65ce06

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

‎src/strif/strif.py‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
More information: https://github.com/jlevy/strif
66
"""
77

8+
import base64
89
import hashlib
910
import os
1011
import random
@@ -191,7 +192,11 @@ class Hash:
191192
Base 36 format is good for short, friendly identifiers. Length of a SHA1 hash
192193
in base36 is ~32 chars.
193194
194-
Example:
195+
Examples:
196+
hash_string("foo").value
197+
-> b'\n\n\x9f*gr\x94%W\xabSU\xd7j\xf4B\xf8\xf6^\x01'
198+
hash_string("foo").base64
199+
-> 'CgqfKmdylCVXq1NV12r0Qvj2XgE='
195200
hash_string("foo").base36
196201
-> '1e6gpc3ehk0mu2jqu8cg42g009s796b'
197202
"""
@@ -203,6 +208,10 @@ class Hash:
203208
def hex(self) -> str:
204209
return self.value.hex()
205210

211+
@property
212+
def base64(self) -> str:
213+
return base64.b64encode(self.value).decode("utf-8")
214+
206215
@property
207216
def base36(self) -> str:
208217
return base36_encode(int.from_bytes(self.value, byteorder="big"))

‎tests/test_hash.py‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import os
22

3-
from strif import hash_file
3+
from strif import hash_file, hash_string
44

55

66
def test_hash_file():
@@ -12,3 +12,7 @@ def test_hash_file():
1212

1313
result_hash = hash_file(file_path, "sha1").with_prefix
1414
assert result_hash == "sha1:0a0a9f2a6772942557ab5355d76af442f8f65e01"
15+
16+
assert hash_string("Hello, World!").with_prefix == result_hash
17+
18+
assert hash_string("Hello, World!").base64 == "CgqfKmdylCVXq1NV12r0Qvj2XgE="

0 commit comments

Comments
 (0)