From e53f11e801e0cce2741a192445d5bf7542e624bd Mon Sep 17 00:00:00 2001 From: equinox Date: Fri, 1 Apr 2011 06:17:19 +0200 Subject: add crypto module --- crypto.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 crypto.py diff --git a/crypto.py b/crypto.py new file mode 100644 index 0000000..0274ec1 --- /dev/null +++ b/crypto.py @@ -0,0 +1,33 @@ +import M2Crypto.RSA +import M2Crypto.EVP +import time, sys +import base64 + +default_alg = 'sha256' + +def sign(keyfile, data, alg = default_alg): + digest = M2Crypto.EVP.MessageDigest(alg) + digest.update(data) + digval = digest.final() + + key = M2Crypto.RSA.load_key(keyfile) + signature = key.sign(digval, algo = alg) + return base64.urlsafe_b64encode(signature) + +def verify(keyfile, data, sig, alg = default_alg): + digest = M2Crypto.EVP.MessageDigest(alg) + digest.update(data) + digval = digest.final() + + key = M2Crypto.RSA.load_pub_key(keyfile) + + rawsig = base64.urlsafe_b64decode(sig) + return bool(key.verify(digval, rawsig, algo = alg)) + +if __name__ == '__main__': + if len(sys.argv) != 3: + print 'pls give private + public key as arg.' + sys.exit(1) + sig = sign(sys.argv[1], 'test') + vfy = verify(sys.argv[2], 'test', sig) + print '(%d) %s %s' % (len(sig), vfy, sig) -- cgit v1.2.1