main.lua 11 KB

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