diff options
author | equinox <equinox@diac24.net> | 2011-04-01 06:17:19 +0200 |
---|---|---|
committer | equinox <equinox@diac24.net> | 2011-04-01 06:17:19 +0200 |
commit | e53f11e801e0cce2741a192445d5bf7542e624bd (patch) | |
tree | dbba0f8cd7a9ec5e68e14d7982371464cb53677b | |
parent | 49856f36361a71aba71c43c77cebb22340010181 (diff) |
add crypto module
-rw-r--r-- | crypto.py | 33 |
1 files changed, 33 insertions, 0 deletions
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) |