LoginSignup
0
1

More than 5 years have passed since last update.

sakura.ioのシグネチャをGoで計算する

Last updated at Posted at 2017-10-16

sakura.ioのシグネチャはハッシュ関数にSHA1を使用したHMACで計算する。Go言語では crypto/hmac , crypto/sha1 を使用することで計算できる。

import (
    "crypto/hmac"
    "crypto/sha1"
    "encoding/hex"
)

const secret string := "secret-string"

// sakura.ioのシグネチャを検証する
func calculateSignature(body []byte, signature string) bool {
    // SHA1でHMACを生成してシークレットキーを渡す
    h := hmac.New(sha1.New, []byte(secret))

    // リクエストボディのバイト列をインスタンスに書き込む
    h.Write(body)

    // `Sum()` でハッシュ値を計算する
    // シグネチャとハッシュ値が同じだったらtrueを返す
    return signature == hex.EncodeToString(h.Sum(nil))
}
0
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
0
1