-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
♻️ Move generic hashing and storage utilities from
lamindb
into `la…
…mindb-setup` (#661) * ♻️ Move generic hashing utilities into lamindb-setup * ♻️ Move generic storage utilities from lamindb into lamindb-setup * 🩹 Fix redundant import * ✅ Move hashing tests into lamindb-setup * 👷 Checkout main branch in laminapp-ui
- Loading branch information
Showing
3 changed files
with
161 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
"""Hashing. | ||
.. autosummary:: | ||
:toctree: . | ||
hash_set | ||
hash_file | ||
""" | ||
|
||
import base64 | ||
import hashlib | ||
from typing import List, Set, Tuple | ||
|
||
|
||
def to_b64_str(bstr: bytes): | ||
b64 = base64.urlsafe_b64encode(bstr).decode().strip("=") | ||
return b64 | ||
|
||
|
||
def b16_to_b64(s: str): | ||
return to_b64_str(base64.b16decode(s.strip('"'), casefold=True)) | ||
|
||
|
||
# a lot to read about this: lamin-notes/2022/hashing | ||
def hash_set(s: Set[str]) -> str: | ||
bstr = ":".join(sorted(s)).encode("utf-8") | ||
# as we're truncating at 20 b64, we choose md5 over sha512 | ||
return to_b64_str(hashlib.md5(bstr).digest())[:20] | ||
|
||
|
||
def hash_md5s_from_dir(etags: List[str]) -> Tuple[str, str]: | ||
# need to sort below because we don't want the order of parsing the dir to | ||
# affect the hash | ||
digests = b"".join( | ||
hashlib.md5(etag.encode("utf-8")).digest() for etag in sorted(etags) | ||
) | ||
digest = hashlib.md5(digests).digest() | ||
return to_b64_str(digest)[:22], "md5-d" | ||
|
||
|
||
def hash_file(file_path, chunk_size=50 * 1024 * 1024) -> Tuple[str, str]: | ||
chunks = [] | ||
with open(file_path, "rb") as fp: | ||
# read first chunk | ||
chunks = [fp.read(chunk_size)] | ||
# try reading the 2nd chunk | ||
data = fp.read(chunk_size) | ||
if data: | ||
# go to end of file | ||
fp.seek(-chunk_size, 2) | ||
# read last chunk | ||
data = fp.read(chunk_size) | ||
chunks.append(data) | ||
if len(chunks) == 1: | ||
digest = hashlib.md5(chunks[0]).digest() | ||
hash_type = "md5" | ||
else: | ||
digests = b"".join(hashlib.sha1(chunk).digest() for chunk in chunks) | ||
digest = hashlib.sha1(digests).digest() | ||
hash_type = "sha1-fl" # sha1 first last chunk | ||
return to_b64_str(digest)[:22], hash_type |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import base64 | ||
from pathlib import Path | ||
|
||
from lamindb_setup.core.hashing import b16_to_b64, hash_file, to_b64_str | ||
|
||
|
||
def test_compute_hash(): | ||
files = [ | ||
# file_content, hash, chunk_size, hash_type | ||
("a", "DMF1ucDxtqgxw5niaXcmYQ", None, "md5"), | ||
("b", "kutf_uauL-w61xx3dTFXjw", None, "md5"), | ||
("abc", "kAFQmDzST7DWlj99KOF_cg", None, "md5"), | ||
("a", "DMF1ucDxtqgxw5niaXcmYQ", 1, "md5"), | ||
("b", "kutf_uauL-w61xx3dTFXjw", 1, "md5"), | ||
# the last case here triggers multi-chunk compute with sha1 | ||
("abc", "p0EbDbQEP1wS-Tw6TuBjKS", 1, "sha1-fl"), | ||
] | ||
for content, hash, chunk_size, hash_type in files: | ||
filepath = Path("file_1") | ||
filepath.write_text(content) | ||
computed_hash, computed_hash_type = hash_file(filepath, chunk_size=chunk_size) | ||
assert computed_hash == hash | ||
assert computed_hash_type == hash_type | ||
filepath.unlink() | ||
|
||
|
||
def test_base64(): | ||
mytest = b"test" | ||
b64_str = to_b64_str(mytest) | ||
b64_str_padded = f"{b64_str}==" | ||
assert base64.urlsafe_b64decode(b64_str_padded.encode()).hex() == mytest.hex() | ||
|
||
|
||
def test_b16_to_b64(): | ||
assert b16_to_b64("9b89c8c1acf79dba5b5341d1fff9806f") == "m4nIwaz3nbpbU0HR__mAbw" |