def test_rfc6979(self):
text = urlopen('https://tools.ietf.org/rfc/rfc6979.txt').read().decode()
curve_tests = findall(r'curve: NIST P-192(.*)curve: NIST P-224', text, flags=DOTALL)[0]
q = int(findall(r'q = ([0-9A-F]*)', curve_tests)[0], 16)
x = int(findall(r'x = ([0-9A-F]*)', curve_tests)[0], 16)
test_regex = r'With SHA-(\d+), message = "([a-zA-Z]*)":\n' \
r'\s*k = ([0-9A-F]*)\n' \
r'\s*r = ([0-9A-F]*)\n' \
r'\s*s = ([0-9A-F]*)\n'
hash_lookup = {
'1': sha1,
'224': sha224,
'256': sha256,
'384': sha384,
'512': sha512
}
for test in findall(test_regex, curve_tests):
h = hash_lookup[test[0]]
msg = test[1]
k = int(test[2], 16)
r = int(test[3], 16)
s = int(test[4], 16)
self.assertEqual(k, RFC6979(msg, x, q, h).gen_nonce())
self.assertEqual((r, s), sign(msg, x, curve=P192, hashfunc=h))
评论列表
文章目录