Advertisement
immibis

crypto test

Feb 22nd, 2013
6,083
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.35 KB | None | 0 0
  1. local p = peripheral.wrap("left")
  2.  
  3. local function testSymmetric(kalgo, calgo, plaintext, keysize)
  4.     if not calgo then calgo = kalgo end
  5.    
  6.     print("symmetric, ",calgo,", key size ",tostring(keysize))
  7.    
  8.     local start = plaintext
  9.    
  10.     local key = p.generateSymmetricKey(kalgo, keysize)
  11.    
  12.     local keyEnc = key.encode()
  13.     -- Key could be saved to a file, transmitted over rednet, etc, at this point.
  14.     -- Since we're not doing that it's redundant to encode and decode the key, but it
  15.     -- demonstrates how to do it.
  16.     key = p.decodeKey(kalgo, keyEnc)
  17.    
  18.     local enc = key.encrypt(calgo, plaintext)
  19.     plaintext = key.decrypt(calgo, enc)
  20.    
  21.     print("original plaintext: ",start)
  22.     print("key: ",keyEnc)
  23.     print("ciphertext: ",enc)
  24.     print("final plaintext: ", plaintext)
  25.    
  26.     assert(start == plaintext)
  27. end
  28.  
  29. local function testAsymmetric(kalgo, calgo, plaintext, keysize, testReverseKeys)
  30.     print("asymmetric, ",calgo,", key size ",tostring(keysize))
  31.    
  32.     local start = plaintext
  33.    
  34.     local pub, priv = p.generateKeyPair(kalgo, keysize)
  35.    
  36.     local pubEnc = pub.encode()
  37.     local privEnc = priv.encode()
  38.  
  39.     -- Keys could be saved to a file, transmitted over rednet, etc, at this point
  40.     -- Since we're not doing that it's redundant to encode and decode the keys, but it
  41.     -- demonstrates how to do it.
  42.  
  43.     pub = p.decodeKey(kalgo, pubEnc)
  44.     priv = p.decodeKey(kalgo, privEnc)
  45.    
  46.     -- Asymmetric encryption: anything encrypted with the public key must be decrypted
  47.     -- with the private key
  48.     local enc = pub.encrypt(calgo, plaintext)
  49.     print("Ciphertext: ",enc)
  50.     assert(priv.decrypt(calgo, enc) == start)
  51.    
  52.     -- and vice versa in the case of RSA
  53.     if testReverseKeys then
  54.         enc= priv.encrypt(calgo, plaintext)
  55.         assert(pub.decrypt(calgo, enc) == start)
  56.     end
  57. end
  58.  
  59. local function testHash(algo, expected)
  60.     local text = "The quick brown fox jumps over the lazy dog"
  61.    
  62.     local a = p.hash(algo, text)
  63.     local b = p.hash(algo, text)
  64.     print(algo,": ",a)
  65.     assert(a == b)
  66.     if expected then assert(a:lower() == expected) end
  67. end
  68.  
  69. -- Test a few symmetric ciphers
  70. testSymmetric("AES", "AES/CBC/PKCS7Padding", "hi asdf", nil)
  71. testSymmetric("ARCFOUR", nil, "hi asdf", nil)
  72. testSymmetric("Blowfish", nil, "hi asdf", nil)
  73. testSymmetric("DES", nil, "hi asdf", nil)
  74. testSymmetric("DESede", nil, "hi asdf", nil)
  75. testSymmetric("RC2", nil, "hi asdf", nil)
  76.  
  77. -- Test a few different block cipher modes, as well as NoPadding
  78. testSymmetric("DES", "DES/CBC/NoPadding", "hihihihi", 56)
  79. testSymmetric("DES", "DES/CTR/NoPadding", "hihihihi", nil)
  80. testSymmetric("DESede", "DESede/ECB/NoPadding", "asdfjkl;", 56*3)
  81. testSymmetric("DESede", "DESede", "asdfjkl;", nil)
  82. testSymmetric("AES", "AES/CFB/NoPadding", "0123456789ABCDEF", nil)
  83. testSymmetric("AES", "AES/OFB/NoPadding", "0123456789ABCDEF", 128)
  84.  
  85. -- Test asymmetric ciphers
  86. testAsymmetric("RSA", "RSA", "Hello world!", 512, true)
  87. testAsymmetric("RSA", "RSA", "Hello world!", 1024, true)
  88. --testAsymmetric("RSA", "RSA", "Hello world!", 2048, true) -- would error, 1024 is max allowed key size
  89.  
  90. -- Test a whole bunch of hash functions
  91. testHash("MD5", "9e107d9d372bb6826bd81d3542a419d6")
  92. testHash("SHA1")
  93. testHash("SHA256")
  94. testHash("SHA384")
  95. testHash("SHA512")
  96. testHash("MD4")
  97. testHash("MD2")
  98. testHash("RipeMD128")
  99. testHash("RipeMD160")
  100. testHash("RipeMD256")
  101. testHash("RipeMD320")
  102. testHash("SHA224")
  103. testHash("Tiger")
  104. testHash("GOST3411")
  105. testHash("Whirlpool")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement