1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

ESP32でHMAC(SHA-256)を生成する

Last updated at Posted at 2022-09-09

ESP32で、HMAC(SHA-256)を生成する機会があったので、備忘録として残しておきます。
ESP32のArduinoでは、mbedtlsを利用できるのでそれを使います。
p_hmacResultは、32バイト以上の配列である必要があります。

M5StickCだろうが、M5StampC3だろうが、ESP32であればPlatformIOを使ってコンパイルできてちゃんと動作します。追加のplatformio.iniのlib_deps指定は不要です。

#include <mbedtls/md.h>

long hmac_sha256(const char *p_key, const char *p_payload, unsigned char *p_hmacResult)
{
  mbedtls_md_context_t ctx;
  mbedtls_md_type_t md_type = MBEDTLS_MD_SHA256;
  mbedtls_md_init(&ctx);
  mbedtls_md_setup(&ctx, mbedtls_md_info_from_type(md_type), 1);
  mbedtls_md_hmac_starts(&ctx, (const unsigned char*)p_key, strlen(p_key));
  mbedtls_md_hmac_update(&ctx, (const unsigned char*)p_payload, strlen(p_payload));
  mbedtls_md_hmac_finish(&ctx, p_hmacResult); // 32 bytes
  mbedtls_md_free(&ctx);

  return 0;
}

以上

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?