HMAC and toHexString

Alex sascha.orlov at gmail.com
Wed Jul 18 10:41:24 UTC 2018


On Wednesday, 18 July 2018 at 05:54:48 UTC, Nicholas Wilson wrote:
> ...
> string key = "blahblahblah";
> auto mac = hmac!SHA256(key.representation);
> string s = ...,t=...u=...,v=...;
> foreach(w;AliasSeq!(s,t,u,v))
>             mac.put(w.representation);
> ubyte[32] s = mac.finish;
> string sig = toHexString!(LetterCase.lower)(s);
>
> writeln(sig);
> // From what I understand Should print something like 
> 41771e2d0d6c1cf7f442aa8547783ea5b126bfc15a4b354e94d0eaea2ab0fa1a
> // Actually prints something like x"31 36 39 33 39 32 38 31 62 
> 38 31 62 36 36 62 63 63 34 63 36 36 61 62 32 34 37 64 32 64 34 
> 61 00 78 2A 55 5B FF 7F 00 00 78 2A 55 5B FF 7F 00 00 E0 2A 55 
> 5B FF 7F 00 00 08 2C 55 5B FF 7F 00 00"c
> Note the nuls and things like FF
>
> What am I doing wrong here?

Don't know, what you mean. Works for me:

´´´
import std.stdio;
import std.digest.hmac;
import std.digest.sha;
import std.string;
import std.meta;

void main()
{
	string key = "blahblahblah";
	auto mac = hmac!SHA256(key.representation);
	string s = "...",t="...", u="...",v="...";
	foreach(w;AliasSeq!(s,t,u,v))
				mac.put(w.representation);
	ubyte[32] r = mac.finish;
	r.toHexString!(LetterCase.lower).writeln;
}
´´´

output:
dc84632fc9b5d1f4b879d9aa021ae0d089e7f66a2fd2e824829cfceedbece729


More information about the Digitalmars-d-learn mailing list