add 'hash_md5' and 'hash_sha1' functions (#3679)

... to global eval namespace
This commit is contained in:
Mike Fährmann
2023-02-20 22:29:26 +01:00
parent e1df7f73b1
commit 56039d2456
2 changed files with 61 additions and 0 deletions

View File

@@ -14,6 +14,7 @@ import sys
import json
import time
import random
import hashlib
import sqlite3
import binascii
import datetime
@@ -112,6 +113,24 @@ def noop():
"""Does nothing"""
def md5(s):
"""Generate MD5 hexdigest of 's'"""
if not s:
s = b""
elif isinstance(s, str):
s = s.encode()
return hashlib.md5(s).hexdigest()
def sha1(s):
"""Generate SHA1 hexdigest of 's'"""
if not s:
s = b""
elif isinstance(s, str):
s = s.encode()
return hashlib.sha1(s).hexdigest()
def generate_token(size=16):
"""Generate a random token with hexadecimal digits"""
data = random.getrandbits(size * 8).to_bytes(size, "big")
@@ -593,6 +612,8 @@ GLOBALS = {
"abort" : raises(exception.StopExtraction),
"terminate": raises(exception.TerminateExtraction),
"restart" : raises(exception.RestartExtraction),
"hash_sha1": sha1,
"hash_md5" : md5,
"re" : re,
}