Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local p = peripheral.wrap("left")
- local function testSymmetric(kalgo, calgo, plaintext, keysize)
- if not calgo then calgo = kalgo end
- print("symmetric, ",calgo,", key size ",tostring(keysize))
- local start = plaintext
- local key = p.generateSymmetricKey(kalgo, keysize)
- local keyEnc = key.encode()
- -- Key could be saved to a file, transmitted over rednet, etc, at this point.
- -- Since we're not doing that it's redundant to encode and decode the key, but it
- -- demonstrates how to do it.
- key = p.decodeKey(kalgo, keyEnc)
- local enc = key.encrypt(calgo, plaintext)
- plaintext = key.decrypt(calgo, enc)
- print("original plaintext: ",start)
- print("key: ",keyEnc)
- print("ciphertext: ",enc)
- print("final plaintext: ", plaintext)
- assert(start == plaintext)
- end
- local function testAsymmetric(kalgo, calgo, plaintext, keysize, testReverseKeys)
- print("asymmetric, ",calgo,", key size ",tostring(keysize))
- local start = plaintext
- local pub, priv = p.generateKeyPair(kalgo, keysize)
- local pubEnc = pub.encode()
- local privEnc = priv.encode()
- -- Keys could be saved to a file, transmitted over rednet, etc, at this point
- -- Since we're not doing that it's redundant to encode and decode the keys, but it
- -- demonstrates how to do it.
- pub = p.decodeKey(kalgo, pubEnc)
- priv = p.decodeKey(kalgo, privEnc)
- -- Asymmetric encryption: anything encrypted with the public key must be decrypted
- -- with the private key
- local enc = pub.encrypt(calgo, plaintext)
- print("Ciphertext: ",enc)
- assert(priv.decrypt(calgo, enc) == start)
- -- and vice versa in the case of RSA
- if testReverseKeys then
- enc= priv.encrypt(calgo, plaintext)
- assert(pub.decrypt(calgo, enc) == start)
- end
- end
- local function testHash(algo, expected)
- local text = "The quick brown fox jumps over the lazy dog"
- local a = p.hash(algo, text)
- local b = p.hash(algo, text)
- print(algo,": ",a)
- assert(a == b)
- if expected then assert(a:lower() == expected) end
- end
- -- Test a few symmetric ciphers
- testSymmetric("AES", "AES/CBC/PKCS7Padding", "hi asdf", nil)
- testSymmetric("ARCFOUR", nil, "hi asdf", nil)
- testSymmetric("Blowfish", nil, "hi asdf", nil)
- testSymmetric("DES", nil, "hi asdf", nil)
- testSymmetric("DESede", nil, "hi asdf", nil)
- testSymmetric("RC2", nil, "hi asdf", nil)
- -- Test a few different block cipher modes, as well as NoPadding
- testSymmetric("DES", "DES/CBC/NoPadding", "hihihihi", 56)
- testSymmetric("DES", "DES/CTR/NoPadding", "hihihihi", nil)
- testSymmetric("DESede", "DESede/ECB/NoPadding", "asdfjkl;", 56*3)
- testSymmetric("DESede", "DESede", "asdfjkl;", nil)
- testSymmetric("AES", "AES/CFB/NoPadding", "0123456789ABCDEF", nil)
- testSymmetric("AES", "AES/OFB/NoPadding", "0123456789ABCDEF", 128)
- -- Test asymmetric ciphers
- testAsymmetric("RSA", "RSA", "Hello world!", 512, true)
- testAsymmetric("RSA", "RSA", "Hello world!", 1024, true)
- --testAsymmetric("RSA", "RSA", "Hello world!", 2048, true) -- would error, 1024 is max allowed key size
- -- Test a whole bunch of hash functions
- testHash("MD5", "9e107d9d372bb6826bd81d3542a419d6")
- testHash("SHA1")
- testHash("SHA256")
- testHash("SHA384")
- testHash("SHA512")
- testHash("MD4")
- testHash("MD2")
- testHash("RipeMD128")
- testHash("RipeMD160")
- testHash("RipeMD256")
- testHash("RipeMD320")
- testHash("SHA224")
- testHash("Tiger")
- testHash("GOST3411")
- testHash("Whirlpool")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement