luat_crypto_rtt.c 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #include "luat_base.h"
  2. #include "luat_crypto.h"
  3. #include "rtthread.h"
  4. #ifdef RT_USING_HWCRYPTO
  5. #include "hwcrypto.h"
  6. #ifdef RT_HWCRYPTO_USING_MD5
  7. int luat_crypto_md5_simple(const char* str, size_t str_size, void* out_ptr) {
  8. struct rt_hwcrypto_ctx *ctx;
  9. ctx = rt_hwcrypto_hash_create(rt_hwcrypto_dev_default(), HWCRYPTO_TYPE_MD5);
  10. if (ctx == NULL) {
  11. return -1; // 内存爆了??
  12. }
  13. rt_hwcrypto_hash_update(ctx, str, str_size);
  14. rt_hwcrypto_hash_finish(ctx, out_ptr, 32);
  15. rt_hwcrypto_hash_destroy(ctx);
  16. return 0;
  17. }
  18. int luat_crypto_hmac_md5_simple(const char* str, size_t str_size, const char* mac, size_t mac_size, void* out_ptr) {
  19. return -1; // 未完成
  20. }
  21. #endif
  22. #ifdef RT_HWCRYPTO_USING_SHA1
  23. int luat_crypto_sha1_simple(const char* str, size_t str_size, void* out_ptr) {
  24. struct rt_hwcrypto_ctx *ctx;
  25. ctx = rt_hwcrypto_hash_create(rt_hwcrypto_dev_default(), HWCRYPTO_TYPE_SHA1);
  26. if (ctx == NULL) {
  27. return -1; // 内存爆了??
  28. }
  29. rt_hwcrypto_hash_update(ctx, str, str_size);
  30. rt_hwcrypto_hash_finish(ctx, out_ptr, 32);
  31. rt_hwcrypto_hash_destroy(ctx);
  32. return 0;
  33. }
  34. #endif
  35. #endif