-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Nagamani: Refactoring, and add
generate_nagamani19_int
- Loading branch information
Showing
4 changed files
with
84 additions
and
53 deletions.
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
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
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 |
---|---|---|
@@ -1,51 +1,70 @@ | ||
# -*- coding: utf-8 -*- | ||
# (c) 2019 Andreas Motl <[email protected]> | ||
# License: GNU Affero General Public License, Version 3 | ||
import os | ||
from binascii import hexlify | ||
from datetime import datetime | ||
from hashids import Hashids | ||
""" | ||
Nagamani - short, unique, non-sequential identifiers based on Hashids. | ||
""" | ||
|
||
precisions = { | ||
's': 1, | ||
'ms': 1000, | ||
'ns': 1000000, | ||
} | ||
|
||
|
||
def nagamani_id(year, salt, precision): | ||
class Nagamani: | ||
""" | ||
https://community.hiveeyes.org/t/gestaltung-der-anonymen-identifizierer-ids-fur-die-adressierung-von-imkern-messknoten-und-anderen-entitaten/1079/8 | ||
""" | ||
Generate unique ids based on Hashids. | ||
|
||
Hashids are short, unique, non-sequential ids generated from numbers | ||
and suitable to be used as unguessable and unpredictable short UIDs. | ||
precisions = { | ||
's': 1, | ||
'ms': 1000, | ||
'ns': 1000000, | ||
} | ||
|
||
Here, we are generating Hashids of the current timestamp in milliseconds. | ||
To keep the footprint low, a custom epoch is used which starts on Jan 1, 2019. | ||
def __init__(self, year: int, salt=None, precision: str = "ns"): | ||
self.year: str = year | ||
self.salt: str = salt or gensalt() | ||
self.precision: str = precision | ||
|
||
Examples:: | ||
def hash(self) -> str: | ||
""" | ||
Generate unique ids based on Hashids. | ||
1Zk5zBoQ | ||
Y4Mvj5Zx | ||
2b4NBvYe | ||
XaMvl962 | ||
yzgOlvap | ||
Hashids are short, unique, non-sequential ids generated from numbers | ||
and suitable to be used as unguessable and unpredictable short UIDs. | ||
""" | ||
Here, we are generating Hashids of the current timestamp in milliseconds. | ||
To keep the footprint low, a custom epoch is used which starts on Jan 1, 2019. | ||
Examples:: | ||
1Zk5zBoQ | ||
Y4Mvj5Zx | ||
2b4NBvYe | ||
XaMvl962 | ||
yzgOlvap | ||
""" | ||
return hashify(self.salt, self.duration()) | ||
|
||
def duration(self) -> int: | ||
""" | ||
Return current timestamp in milliseconds with a | ||
custom epoch which starts on Jan 1, 2019. | ||
""" | ||
|
||
assert type(year) is int, 'Year must be integer' | ||
assert salt is not None, 'Salt must be given' | ||
assert precision is not None, 'Precision must be one of s, ms, ns' | ||
assert type(self.year) is int, 'Year must be integer' | ||
assert self.salt is not None, 'Salt must be given' | ||
assert self.precision is not None, 'Precision must be one of s, ms, ns' | ||
|
||
scaling = precisions.get(precision) | ||
scaling = self.precisions.get(self.precision) | ||
|
||
duration = datetime.utcnow() - datetime(year, 1, 1) | ||
duration_scaled = int(duration.total_seconds() * scaling) | ||
return hashify(salt, duration_scaled) | ||
duration = datetime.utcnow() - datetime(self.year, 1, 1) | ||
duration_scaled = int(duration.total_seconds() * scaling) | ||
return duration_scaled | ||
|
||
|
||
def hashify(salt, *data): | ||
def hashify(salt, *data) -> str: | ||
""" | ||
Hashids are short, unique, non-sequential ids generated from numbers | ||
and suitable to be used as short UIDs. | ||
|
@@ -61,3 +80,20 @@ def hashify(salt, *data): | |
""" | ||
hashids = Hashids(salt=salt) | ||
return hashids.encode(*data) | ||
|
||
|
||
def gensalt(): | ||
""" | ||
This generates a salt from 24 random bytes from an OS-specific randomness source. | ||
The returned data should be unpredictable enough for cryptographic applications, | ||
though its exact quality depends on the OS implementation. | ||
https://docs.python.org/3/library/os.html#os.urandom | ||
Examples:: | ||
b5f95cead701f2488d5668decb0d63a30e7ddb4c21f26574 | ||
b4157e5459c88a6c454186492ee629ca097f8a60cbfb1a36 | ||
de1ba437524e540e3b0d55617afcad5677b982d9e1f45f9d | ||
""" | ||
return hexlify(os.urandom(24)).decode() |