diff options
-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) |