有一个如何在 Python 中完成此操作的示例。您会注意到它使用binascii .a2b_hex()
.
这是 Ruby 中的另一个示例:
require('openssl')
secret = '488f40ff3d0b2c6188e272f8d86416b73b3cb4ef'
serial = '0123456789'
digest = OpenSSL::Digest::Digest.new('sha1')
puts OpenSSL::HMAC.hexdigest(digest, [secret].pack("H*"), serial)
这是 Arduino 的一个:
// First, download the library from https://github.com/Cathedrow/Cryptosuite
// and make sure it is installed properly, although until it supports Arduino 1.0,
// it's better to use this fork: https://github.com/jkiv/Cryptosuite
#include "sha1.h"
uint8_t secret[]={
0x48,0x8f,0x40,0xff,0x3d,0x0b,0x2c,0x61,0x88,0xe2,
0x72,0xf8,0xd8,0x64,0x16,0xb7,0x3b,0x3c,0xb4,0xef,
};
String serial = "0123456789";
String activation_code;
String convertHash(uint8_t* hash) {
String returnString;
int i;
for (i=0; i<20; i++) {
returnString += ("0123456789abcdef"[hash[i]>>4]);
returnString += ("0123456789abcdef"[hash[i]&0xf]);
}
return returnString;
}
void setup() {
// Generally you would compute the hash once on start up and store it in flash
// to avoid doing it each time as it can be a bit slow
Serial.begin(57600);
Serial.println("computing hmac sha: ");
Sha1.initHmac(secret, 20);
Sha1.print(serial);
activation_code = convertHash(Sha1.resultHmac());
Serial.println(activation_code);
}