Created
May 29, 2016 15:22
-
-
Save ant31/893cb83df8d140ed13b59a8dc9de241c to your computer and use it in GitHub Desktop.
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
# requirements_packages_crypto = [ | |
# 'ecdsa', | |
# 'cryptography' | |
# ] | |
import string | |
import yaml | |
import json | |
import random | |
import hashlib | |
import _jsonnet | |
import sys | |
from cryptography.hazmat.primitives import serialization | |
from cryptography.hazmat.backends import default_backend | |
from cryptography.hazmat.primitives.asymmetric import rsa, dsa | |
def get_hash(data, hashtype='sha1'): | |
h = hashlib.new(hashtype) | |
h.update(data) | |
return h.hexdigest() | |
def rand_string(size=32, chars=(string.ascii_letters + string.digits), seed=None): | |
if seed == "": | |
seed = None | |
random.seed(seed) | |
size = int(size) | |
return ''.join(random.choice(chars) for _ in range(size)) | |
def rand_alphanum(size=32, seed=None): | |
size = int(size) | |
return rand_string(size=size, seed=seed) | |
def rand_alpha(size=32, seed=None): | |
size = int(size) | |
return rand_string(size=size, chars=string.ascii_letters, seed=seed) | |
def randint(size=32, seed=None): | |
size = int(size) | |
return rand_string(size=size, chars=string.digits, seed=seed) | |
def gen_private_ecdsa(): | |
from ecdsa import SigningKey | |
sk = SigningKey.generate() | |
return sk.to_pem() | |
def gen_private_rsa(): | |
private_key = rsa.generate_private_key( | |
public_exponent=65537, | |
key_size=2048, | |
backend=default_backend() | |
) | |
pem = private_key.private_bytes( | |
encoding=serialization.Encoding.PEM, | |
format=serialization.PrivateFormat.TraditionalOpenSSL, | |
encryption_algorithm=serialization.NoEncryption() | |
) | |
return pem | |
def gen_private_dsa(): | |
private_key = dsa.generate_private_key( | |
key_size=1024, | |
backend=default_backend() | |
) | |
pem = private_key.private_bytes( | |
encoding=serialization.Encoding.PEM, | |
format=serialization.PrivateFormat.TraditionalOpenSSL, | |
encryption_algorithm=serialization.NoEncryption() | |
) | |
return pem | |
def gen_privatekey(keytype='rsa', seed=None): | |
if keytype == "ecdsa": | |
return gen_private_ecdsa() | |
elif keytype == "rsa": | |
return gen_private_rsa() | |
elif keytype == "dsa": | |
return gen_private_dsa() | |
else: | |
raise ValueError("Unknow private key type: %s" % keytype) | |
def json_to_yaml(value): | |
""" | |
Serializes an object as YAML. Optionally given keyword arguments | |
are passed to yaml.dumps(), ensure_ascii however defaults to False. | |
""" | |
return yaml.safe_dump(json.loads(value)) | |
native_callbacks = { | |
'hash': (('data', 'hashtype'), get_hash), | |
'to_yaml': (('value',), json_to_yaml), | |
'rand_alphanum': (('size', 'seed'), rand_alphanum), | |
'rand_alpha': (('size', 'seed'), rand_alpha), | |
'randint': (('size', 'seed'), randint), | |
'privatekey': (('keytype', "seed"), gen_privatekey), | |
} | |
json_str = _jsonnet.evaluate_file( | |
'test.jsonnet', | |
native_callbacks=native_callbacks, | |
) | |
sys.stdout.write(json_str) |
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
local hash(data, hashtype='sha1') = | |
std.native("hash")(std.toString(data), hashtype); | |
local to_yaml(data) = | |
std.native("to_yaml")(std.toString(data)); | |
local randAlphaNum(size=32, seed="") = | |
std.native("rand_alphanum")(std.toString(size), seed=seed); | |
local randAlpha(size=32, seed="") = | |
std.native("rand_alpha")(std.toString(size), seed=seed); | |
local randInt(size=32, seed="") = | |
std.native("randint")(std.toString(size), seed=seed); | |
local genPrivateKey(keytype, seed="") = | |
std.native("privatekey")(keytype, seed=seed); | |
{ | |
hashsha1: hash("titi"), | |
hashmd5: hash("titi", 'md5'), | |
hashsha256: hash("titi", 'sha256'), | |
yaml: to_yaml({"a": "b", "t": [1,2,3,4]}), | |
rand_alphnum32: randAlphaNum(), | |
rand_alphnum8: randAlphaNum(8), | |
rand_alpha8: randAlpha(8), | |
rand_alpha32: randAlpha(), | |
rand_int32: randInt(seed="4"), | |
rand_int8: randInt(8), | |
rsa: genPrivateKey("rsa"), | |
dsa: genPrivateKey("dsa"), | |
ecdsa: genPrivateKey("ecdsa"), | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment