main.lua 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. -- LuaTools需要PROJECT和VERSION这两个信息
  2. PROJECT = "cryptodemo"
  3. VERSION = "1.0.0"
  4. log.info("main", PROJECT, VERSION)
  5. -- sys库是标配
  6. _G.sys = require("sys")
  7. if wdt then
  8. --添加硬狗防止程序卡死,在支持的设备上启用这个功能
  9. wdt.init(9000)--初始化watchdog设置为9s
  10. sys.timerLoopStart(wdt.feed, 3000)--3s喂一次狗
  11. end
  12. sys.taskInit(function()
  13. sys.wait(1000)
  14. -- MD5,输出结果已经hex编码
  15. log.info("md5", crypto.md5("abc"))
  16. log.info("hmac_md5", crypto.hmac_md5("abc", "1234567890"))
  17. -- SHA1,输出结果已经hex编码
  18. log.info("sha1", crypto.sha1("abc"))
  19. log.info("hmac_sha1", crypto.hmac_sha1("abc", "1234567890"))
  20. -- SHA256,输出结果已经hex编码
  21. log.info("sha256", crypto.sha256("abc"))
  22. log.info("hmac_sha256", crypto.hmac_sha256("abc", "1234567890"))
  23. -- SHA512,输出结果已经hex编码
  24. log.info("sha512", crypto.sha512("abc"))
  25. log.info("hmac_sha512", crypto.hmac_sha512("abc", "1234567890"))
  26. local data_encrypt = crypto.cipher_encrypt("AES-128-ECB", "ZERO", "023001", "HZBIT@WLW/YSBKEY")
  27. log.info("AES", "aes-128-ecb", data_encrypt:toHex())
  28. local data_decrypt = crypto.cipher_decrypt("AES-128-ECB", "ZERO", data_encrypt, "HZBIT@WLW/YSBKEY")
  29. log.info("AES", "aes-128-ecb", data_decrypt)
  30. -- AES加密, 未经Hex编码. AES-128-ECB 算法,待加密字符串如果超过32字节会报错,待查. by wendal 20200812
  31. local data_encrypt = crypto.cipher_encrypt("AES-128-ECB", "PKCS7", "12345678901234 > 123456", "1234567890123456")
  32. local data2_encrypt = crypto.cipher_encrypt("AES-128-CBC", "PKCS7", "12345678901234 > 123456", "1234567890123456", "1234567890666666")
  33. log.info("AES", "aes-128-ecb", data_encrypt:toHex())
  34. log.info("AES", "aes-128-cbc", data2_encrypt:toHex())
  35. -- AES解密, 未经Hex编码
  36. local data_decrypt = crypto.cipher_decrypt("AES-128-ECB", "PKCS7", data_encrypt, "1234567890123456")
  37. local data2_decrypt = crypto.cipher_decrypt("AES-128-CBC", "PKCS7", data2_encrypt, "1234567890123456", "1234567890666666")
  38. log.info("AES", "aes-128-ecb", data_decrypt)
  39. log.info("AES", "aes-128-cbc", data2_decrypt)
  40. log.info("mem", rtos.meminfo("sys"))
  41. -- DES-ECB 加解密
  42. local data1 = crypto.cipher_encrypt("DES-ECB", "PKCS7", "abcdefg", "12345678")
  43. if data1 then -- DES-ECB 在某些平台不支持的
  44. log.info("des", data1:toHex())
  45. local data2 = crypto.cipher_decrypt("DES-ECB", "PKCS7", data1, "12345678")
  46. log.info("des", data2)
  47. else
  48. log.info("des", "当前固件不支持DES/3DES")
  49. end
  50. -- 3DES-ECB 加解密
  51. local data1 = crypto.cipher_encrypt("DES-EDE3-ECB", "PKCS7", "abcdefg!!--ZZSS", "123456781234567812345678")
  52. if data1 then -- DES-ECB 在某些平台不支持的
  53. log.info("3des", data1:toHex())
  54. local data2 = crypto.cipher_decrypt("DES-EDE3-ECB", "PKCS7", data1, "123456781234567812345678")
  55. log.info("3des", data2)
  56. else
  57. log.info("3des", "当前固件不支持DES/3DES")
  58. end
  59. -- 打印所有支持的cipher
  60. if crypto.cipher_list then
  61. log.info("cipher", "list", json.encode(crypto.cipher_list()))
  62. else
  63. log.info("cipher", "当前固件不支持crypto.cipher_list")
  64. end
  65. -- 打印所有支持的cipher suites
  66. if crypto.cipher_suites then
  67. log.info("cipher", "suites", json.encode(crypto.cipher_suites()))
  68. else
  69. log.info("cipher", "当前固件不支持crypto.cipher_suites")
  70. end
  71. ---------------------------------------
  72. log.info("随机数测试")
  73. for i=1, 10 do
  74. sys.wait(100)
  75. log.info("crypto", "真随机数",string.unpack("I",crypto.trng(4)))
  76. -- log.info("crypto", "伪随机数",math.random()) -- 输出的是浮点数,不推荐
  77. -- log.info("crypto", "伪随机数",math.random(1, 65525)) -- 不推荐
  78. end
  79. -- totp的密钥
  80. log.info("totp的密钥")
  81. local secret = "VK54ZXPO74ISEM2E"
  82. --写死时间戳用来测试
  83. local ts = 1646796576
  84. --生成十分钟的动态码验证下
  85. for i=1,600,30 do
  86. local r = crypto.totp(secret,ts+i)
  87. local time = os.date("*t",ts+i + 8*3600)--东八区
  88. log.info("totp", string.format("%06d" ,r),time.hour,time.min,time.sec)
  89. end
  90. -- 文件测试
  91. log.info("文件hash值测试")
  92. if crypto.md_file then
  93. log.info("md5", crypto.md_file("MD5", "/luadb/logo.jpg"))
  94. log.info("sha1", crypto.md_file("SHA1", "/luadb/logo.jpg"))
  95. log.info("sha256", crypto.md_file("SHA256", "/luadb/logo.jpg"))
  96. log.info("hmac_md5", crypto.md_file("MD5", "/luadb/logo.jpg", "123456"))
  97. log.info("hmac_sha1", crypto.md_file("SHA1", "/luadb/logo.jpg", "123456"))
  98. log.info("hmac_sha256", crypto.md_file("SHA256", "/luadb/logo.jpg", "123456"))
  99. else
  100. log.info("文件hash值测试", "当前固件不支持crypto.md_file")
  101. end
  102. if crypto.checksum then
  103. log.info("checksum", "OK", string.char(crypto.checksum("OK")):toHex())
  104. log.info("checksum", "357E", string.char(crypto.checksum("357E", 1)):toHex())
  105. else
  106. log.info("checksum", "当前固件不支持crypto.checksum")
  107. end
  108. -- 流式hash测试
  109. log.info("流式hash测试")
  110. if crypto.hash_init then
  111. -- MD5
  112. local md5_obj = crypto.hash_init("MD5")
  113. crypto.hash_update(md5_obj, "1234567890")
  114. crypto.hash_update(md5_obj, "1234567890")
  115. crypto.hash_update(md5_obj, "1234567890")
  116. crypto.hash_update(md5_obj, "1234567890")
  117. local md5_result = crypto.hash_finish(md5_obj)
  118. log.info("md5_stream", md5_result)
  119. log.info("md5", crypto.md5("1234567890123456789012345678901234567890"))
  120. -- HMAC_MD5
  121. local hmac_md5_obj = crypto.hash_init("MD5", "1234567890")
  122. crypto.hash_update(hmac_md5_obj, "1234567890")
  123. crypto.hash_update(hmac_md5_obj, "1234567890")
  124. crypto.hash_update(hmac_md5_obj, "1234567890")
  125. crypto.hash_update(hmac_md5_obj, "1234567890")
  126. local hmac_md5_result = crypto.hash_finish(hmac_md5_obj)
  127. log.info("hmac_md5_stream", hmac_md5_result)
  128. log.info("hmac_md5", crypto.hmac_md5("1234567890123456789012345678901234567890", "1234567890"))
  129. -- SHA1
  130. local sha1_obj = crypto.hash_init("SHA1")
  131. crypto.hash_update(sha1_obj, "1234567890")
  132. crypto.hash_update(sha1_obj, "1234567890")
  133. crypto.hash_update(sha1_obj, "1234567890")
  134. crypto.hash_update(sha1_obj, "1234567890")
  135. local sha1_result = crypto.hash_finish(sha1_obj)
  136. log.info("sha1_stream", sha1_result)
  137. log.info("sha1", crypto.sha1("1234567890123456789012345678901234567890"))
  138. -- HMAC_SHA1
  139. local hmac_sha1_obj = crypto.hash_init("SHA1", "1234567890")
  140. crypto.hash_update(hmac_sha1_obj, "1234567890")
  141. crypto.hash_update(hmac_sha1_obj, "1234567890")
  142. crypto.hash_update(hmac_sha1_obj, "1234567890")
  143. crypto.hash_update(hmac_sha1_obj, "1234567890")
  144. local hmac_sha1_result = crypto.hash_finish(hmac_sha1_obj)
  145. log.info("hmac_sha1_stream", hmac_sha1_result)
  146. log.info("hmac_sha1", crypto.hmac_sha1("1234567890123456789012345678901234567890", "1234567890"))
  147. -- SHA256
  148. local sha256_obj = crypto.hash_init("SHA256")
  149. crypto.hash_update(sha256_obj, "1234567890")
  150. crypto.hash_update(sha256_obj, "1234567890")
  151. crypto.hash_update(sha256_obj, "1234567890")
  152. crypto.hash_update(sha256_obj, "1234567890")
  153. local sha256_result = crypto.hash_finish(sha256_obj)
  154. log.info("sha256_stream", sha256_result)
  155. log.info("sha256", crypto.sha256("1234567890123456789012345678901234567890"))
  156. -- HMAC_SHA256
  157. local hmac_sha256_obj = crypto.hash_init("SHA256", "1234567890")
  158. crypto.hash_update(hmac_sha256_obj, "1234567890")
  159. crypto.hash_update(hmac_sha256_obj, "1234567890")
  160. crypto.hash_update(hmac_sha256_obj, "1234567890")
  161. crypto.hash_update(hmac_sha256_obj, "1234567890")
  162. local hmac_sha256_result = crypto.hash_finish(hmac_sha256_obj)
  163. log.info("hmac_sha256_stream", hmac_sha256_result)
  164. log.info("hmac_sha256", crypto.hmac_sha256("1234567890123456789012345678901234567890", "1234567890"))
  165. -- SHA512
  166. local sha512_obj = crypto.hash_init("SHA512")
  167. if sha512_obj then
  168. crypto.hash_update(sha512_obj, "1234567890")
  169. crypto.hash_update(sha512_obj, "1234567890")
  170. crypto.hash_update(sha512_obj, "1234567890")
  171. crypto.hash_update(sha512_obj, "1234567890")
  172. local sha512_result = crypto.hash_finish(sha512_obj)
  173. log.info("sha512_stream", sha512_result)
  174. log.info("sha512", crypto.sha512("1234567890123456789012345678901234567890"))
  175. end
  176. -- HMAC_SHA512
  177. local hmac_sha512_obj = crypto.hash_init("SHA512", "1234567890")
  178. if hmac_sha512_obj then
  179. crypto.hash_update(hmac_sha512_obj, "1234567890")
  180. crypto.hash_update(hmac_sha512_obj, "1234567890")
  181. crypto.hash_update(hmac_sha512_obj, "1234567890")
  182. crypto.hash_update(hmac_sha512_obj, "1234567890")
  183. local hmac_sha512_result = crypto.hash_finish(hmac_sha512_obj)
  184. log.info("hmac_sha512_stream", hmac_sha512_result)
  185. log.info("hmac_sha512", crypto.hmac_sha512("1234567890123456789012345678901234567890", "1234567890"))
  186. end
  187. else
  188. log.info("crypto", "当前固件不支持crypto.hash_init")
  189. end
  190. log.info("crc7测试")
  191. if crypto.crc7 then
  192. local result = crypto.crc7(string.char(0xAA), 0xE5, 0x00)
  193. log.info("crc7测试", result, string.format("%02X", result))
  194. else
  195. log.info("crypto", "当前固件不支持crypto.crc7")
  196. end
  197. log.info("crypto", "ALL Done")
  198. sys.wait(100000)
  199. end)
  200. -- 用户代码已结束---------------------------------------------
  201. -- 结尾总是这一句
  202. sys.run()
  203. -- sys.run()之后后面不要加任何语句!!!!!