rename dtk -> dcd
This commit is contained in:
parent
e7a35d23e5
commit
139381e7fc
7 changed files with 56 additions and 26 deletions
79
dcd/aux.py
Normal file
79
dcd/aux.py
Normal file
|
@ -0,0 +1,79 @@
|
|||
#! /usr/bin/env python3
|
||||
# encoding: utf-8
|
||||
|
||||
# ----------------------------------------------------------------
|
||||
|
||||
def flatten_dict(root, glue=" ", prefix=[]):
|
||||
lines = []
|
||||
|
||||
for k, v in root.items():
|
||||
new_prefix = prefix + [k]
|
||||
|
||||
if type(v) == dict:
|
||||
lines.extend(flatten_dict(v, glue, new_prefix))
|
||||
else:
|
||||
lines.append((glue.join(new_prefix), v))
|
||||
|
||||
return lines
|
||||
|
||||
# ----------------------------------------------------------------
|
||||
|
||||
import random
|
||||
|
||||
PASSPHRASE_VOWELS = "aeiuAEU"
|
||||
PASSPHRASE_CONSONANTS = "bcdfghjkmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ23456789"
|
||||
|
||||
def random_bytes(length):
|
||||
return open("/dev/urandom", "rb").read(length)
|
||||
|
||||
def random_syllable():
|
||||
return random.choice(PASSPHRASE_CONSONANTS) + random.choice(PASSPHRASE_VOWELS) + random.choice(PASSPHRASE_CONSONANTS)
|
||||
|
||||
def random_word(syllables=3):
|
||||
parts = []
|
||||
|
||||
for _ in range(syllables):
|
||||
parts.append(random_syllable())
|
||||
|
||||
return "".join(parts)
|
||||
|
||||
# ----------------------------------------------------------------
|
||||
|
||||
import hashlib
|
||||
|
||||
def hash_password(p, hexdigest=True):
|
||||
h = hashlib.blake2b()
|
||||
|
||||
h.update(p.encode("utf-8"))
|
||||
|
||||
return h.hexdigest() if hexdigest else h.digest()
|
||||
|
||||
# ----------------------------------------------------------------
|
||||
|
||||
def raw_to_hex(raw, separator=" "):
|
||||
"""
|
||||
Converts a bytearray (or bytes) into its textual hexadecimal representation.
|
||||
"""
|
||||
|
||||
output = []
|
||||
|
||||
for o in raw:
|
||||
output.append(hex(o)[2:].zfill(2))
|
||||
|
||||
return separator.join(output)
|
||||
|
||||
def hex_to_raw(text, separator=" "):
|
||||
"""
|
||||
Converts a hexadecimal representation of a byte array into a bytearray.
|
||||
"""
|
||||
|
||||
output = []
|
||||
|
||||
text = text.replace(separator, "")
|
||||
|
||||
for i in range(len(text)//2):
|
||||
output.append(int(text[2*i:2*i+2], 16))
|
||||
|
||||
return bytearray(output)
|
||||
|
||||
# ----------------------------------------------------------------
|
Loading…
Add table
Add a link
Reference in a new issue