mirror of
https://github.com/mikf/gallery-dl.git
synced 2024-11-22 02:32:33 +01:00
add 'hash_md5' and 'hash_sha1' functions (#3679)
... to global eval namespace
This commit is contained in:
parent
e1df7f73b1
commit
56039d2456
@ -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,
|
||||
}
|
||||
|
||||
|
@ -394,6 +394,46 @@ class TestOther(unittest.TestCase):
|
||||
def test_noop(self):
|
||||
self.assertEqual(util.noop(), None)
|
||||
|
||||
def test_md5(self):
|
||||
self.assertEqual(util.md5(b""),
|
||||
"d41d8cd98f00b204e9800998ecf8427e")
|
||||
self.assertEqual(util.md5(b"hello"),
|
||||
"5d41402abc4b2a76b9719d911017c592")
|
||||
|
||||
self.assertEqual(util.md5(""),
|
||||
"d41d8cd98f00b204e9800998ecf8427e")
|
||||
self.assertEqual(util.md5("hello"),
|
||||
"5d41402abc4b2a76b9719d911017c592")
|
||||
self.assertEqual(util.md5("ワルド"),
|
||||
"051f29cd6c942cf110a0ccc5729871d2")
|
||||
|
||||
self.assertEqual(util.md5(0),
|
||||
"d41d8cd98f00b204e9800998ecf8427e")
|
||||
self.assertEqual(util.md5(()),
|
||||
"d41d8cd98f00b204e9800998ecf8427e")
|
||||
self.assertEqual(util.md5(None),
|
||||
"d41d8cd98f00b204e9800998ecf8427e")
|
||||
|
||||
def test_sha1(self):
|
||||
self.assertEqual(util.sha1(b""),
|
||||
"da39a3ee5e6b4b0d3255bfef95601890afd80709")
|
||||
self.assertEqual(util.sha1(b"hello"),
|
||||
"aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d")
|
||||
|
||||
self.assertEqual(util.sha1(""),
|
||||
"da39a3ee5e6b4b0d3255bfef95601890afd80709")
|
||||
self.assertEqual(util.sha1("hello"),
|
||||
"aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d")
|
||||
self.assertEqual(util.sha1("ワルド"),
|
||||
"0cbe319081aa0e9298448ec2bb16df8c494aa04e")
|
||||
|
||||
self.assertEqual(util.sha1(0),
|
||||
"da39a3ee5e6b4b0d3255bfef95601890afd80709")
|
||||
self.assertEqual(util.sha1(()),
|
||||
"da39a3ee5e6b4b0d3255bfef95601890afd80709")
|
||||
self.assertEqual(util.sha1(None),
|
||||
"da39a3ee5e6b4b0d3255bfef95601890afd80709")
|
||||
|
||||
def test_compile_expression(self):
|
||||
expr = util.compile_expression("1 + 2 * 3")
|
||||
self.assertEqual(expr(), 7)
|
||||
|
Loading…
Reference in New Issue
Block a user