Path: blob/main/tests/sys/geom/class/eli/gentestvect.py
39636 views
#!/usr/bin/env python12from hashlib import pbkdf2_hmac3import hashlib4import itertools5import string67#From: https://stackoverflow.com/questions/14945095/how-to-escape-string-for-generated-c8def cstring(s, encoding='ascii'):9if isinstance(s, unicode):10s = s.encode(encoding)1112result = ''13for c in s:14if not (32 <= ord(c) < 127) or c in ('\\', '"'):15result += '\\%03o' % ord(c)16else:17result += c1819return '"' + result + '"'2021intarr = lambda y: ', '.join(map(lambda x: str(ord(x)), y))2223_randfd = open('/dev/urandom', 'rb')24_maketrans = string.maketrans('', '')25def randgen(l, delchrs=None):26if delchrs is None:27return _randfd.read(l)2829s = ''30while len(s) < l:31s += string.translate(_randfd.read(l - len(s)), _maketrans,32delchrs)33return s3435def printhmacres(salt, passwd, itr, hmacout):36print '\t{ %s, %d, %s, %d, %s, %d },' % (cstring(salt), len(salt),37cstring(passwd), itr, cstring(hmacout), len(hmacout))3839if __name__ == '__main__':40import sys4142if len(sys.argv) == 1:43hashfun = 'sha512'44else:45hashfun = sys.argv[1]4647if hashfun not in hashlib.algorithms:48print 'Invalid hash function: %s' % `hashfun`49sys.exit(1)5051print '/* Test Vectors for PBKDF2-%s */' % hashfun.upper()52print '\t/* salt, saltlen, passwd, itr, hmacout, hmacoutlen */'53for saltl in xrange(8, 64, 8):54for itr in itertools.chain(xrange(100, 1000, 100), xrange(1000,5510000, 1000)):56for passlen in xrange(8, 80, 8):57salt = randgen(saltl)58passwd = randgen(passlen, '\x00')59hmacout = pbkdf2_hmac(hashfun, passwd, salt,60itr)61printhmacres(salt, passwd, itr, hmacout)626364