main.lua 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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(3000)
  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. -- 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. print(data1:toHex())
  45. local data2 = crypto.cipher_decrypt("DES-ECB", "PKCS7", data1, "12345678")
  46. print(data2)
  47. end
  48. -- 打印所有支持的cipher
  49. if crypto.cipher_list then
  50. log.info("cipher", "list", json.encode(crypto.cipher_list()))
  51. end
  52. -- 打印所有支持的cipher suites
  53. if crypto.cipher_suites then
  54. log.info("cipher", "suites", json.encode(crypto.cipher_suites()))
  55. end
  56. for i=1, 10 do
  57. sys.wait(100)
  58. log.info("crypto", "真随机数",string.unpack("I",crypto.trng(4)))
  59. -- log.info("crypto", "伪随机数",math.random()) -- 输出的是浮点数,不建议使用
  60. end
  61. -- totp的密钥
  62. local secret = "VK54ZXPO74ISEM2E"
  63. --写死时间戳用来测试
  64. local ts = 1646796576
  65. --生成十分钟的动态码验证下
  66. for i=1,600,30 do
  67. local r = crypto.totp(secret,ts+i)
  68. local time = os.date("*t",ts+i + 8*3600)--东八区
  69. log.info("totp", string.format("%06d" ,r),time.hour,time.min,time.sec)
  70. end
  71. if crypto.md_file then
  72. log.info("md5", crypto.md_file("MD5", "/luadb/logo.jpg"))
  73. log.info("sha1", crypto.md_file("SHA1", "/luadb/logo.jpg"))
  74. log.info("sha256", crypto.md_file("SHA256", "/luadb/logo.jpg"))
  75. log.info("hmac_md5", crypto.md_file("MD5", "/luadb/logo.jpg", "123456"))
  76. log.info("hmac_sha1", crypto.md_file("SHA1", "/luadb/logo.jpg", "123456"))
  77. log.info("hmac_sha256", crypto.md_file("SHA256", "/luadb/logo.jpg", "123456"))
  78. end
  79. if crypto.checksum then
  80. log.info("checksum", "OK", string.char(crypto.checksum("OK")):toHex())
  81. log.info("checksum", "357E", string.char(crypto.checksum("357E", 1)):toHex())
  82. end
  83. log.info("crypto", "ALL Done")
  84. sys.wait(100000)
  85. end)
  86. -- 用户代码已结束---------------------------------------------
  87. -- 结尾总是这一句
  88. sys.run()
  89. -- sys.run()之后后面不要加任何语句!!!!!