aboutsummaryrefslogtreecommitdiffstats
path: root/bsfs/utils/uuid.py
diff options
context:
space:
mode:
Diffstat (limited to 'bsfs/utils/uuid.py')
-rw-r--r--bsfs/utils/uuid.py24
1 files changed, 19 insertions, 5 deletions
diff --git a/bsfs/utils/uuid.py b/bsfs/utils/uuid.py
index 6366b18..ad7fc1c 100644
--- a/bsfs/utils/uuid.py
+++ b/bsfs/utils/uuid.py
@@ -1,12 +1,9 @@
-"""
-Part of the BlackStar filesystem (bsfs) module.
-A copy of the license is provided with the project.
-Author: Matthias Baumgartner, 2022
-"""
# imports
from collections import abc
import hashlib
+import io
+import json
import os
import platform
import random
@@ -105,4 +102,21 @@ class UCID():
with open(path, 'rb') as ifile:
return HASH(ifile.read()).hexdigest()
+ @staticmethod
+ def from_buffer(buffer: io.IOBase) -> str:
+ """Read the content from a buffer."""
+ if isinstance(buffer, io.TextIOBase):
+ return HASH(buffer.read().encode('utf-8', errors='ignore')).hexdigest()
+ return HASH(buffer.read()).hexdigest()
+
+ @staticmethod
+ def from_bytes(content: bytes) -> str:
+ """Get the content from as bytes."""
+ return HASH(content).hexdigest()
+
+ @staticmethod
+ def from_dict(content: dict) -> str:
+ """Get the content from a dict."""
+ return HASH(json.dumps(content).encode('ascii', 'ignore')).hexdigest()
+
## EOF ##