Last active
January 10, 2024 20:54
-
-
Save jtagcat/3e1357560a104766eb0841416c51533f to your computer and use it in GitHub Desktop.
Jinja Stable UUID with home-brewn hash
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
{%- macro xor(a, b) %} {#- Macros can only return text #} | |
{%- if (a and not b) or (not a and b) -%} | |
1 | |
{%- else -%} | |
0 | |
{%- endif %} | |
{%- endmacro -%} | |
{%- macro bitsToHex(bools) %} | |
{%- for hex in (bools | batch(4)) %} | |
{%- set sum = 0 %} | |
{%- if hex[0] %}{%- set sum = sum + 8 %}{% endif %} | |
{%- if hex[1] %}{%- set sum = sum + 4 %}{% endif %} | |
{%- if hex[2] %}{%- set sum = sum + 2 %}{% endif %} | |
{%- if hex[3] %}{%- set sum = sum + 1 %}{% endif %} | |
{%- if sum < 10 %}{{ sum }} | |
{%- elif sum == 10 %}a | |
{%- elif sum == 11 %}b | |
{%- elif sum == 12 %}c | |
{%- elif sum == 13 %}d | |
{%- elif sum == 14 %}e | |
{%- elif sum == 15 %}f | |
{%- endif %} | |
{%- endfor %} | |
{%- endmacro %} | |
{%- macro badHash(input) %} {#- since there is no seedable randomness nor hashing available and cutting off parts of the input is just unacceptable as to allow self-inflicted pain #} | |
{%- set bitstream = [] %} | |
{%- for part in input.encode("utf-8") %} | |
{%- for bit in '{:b}'.format(part) %} | |
{%- do bitstream.append(bit == "1") %} | |
{%- endfor %} | |
{%- endfor -%} | |
{%- set result = [False]*12*4 %} {#- 12 last UUID digits, 4 bits in one hex #} | |
{%- for chunk in bitstream | batch(12*4) %} | |
{%- for bit in chunk %} | |
{%- do result.append(xor(result[0], bit) == "1"), result.pop(0) %} | |
{%- endfor %} | |
{%- endfor -%} | |
{{- bitsToHex(result) }} | |
{%- endmacro -%} | |
{%- set labUUID = {} %} | |
{%- for i in instance %} | |
{%- do labUUID.update({instance: '1ab00000-0000-0000-0000-'+badHash(i)}) %} | |
{%- endfor -%} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment