www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - HMAC and toHexString

reply Nicholas Wilson <iamthewilsonator hotmail.com> writes:
...
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?
Jul 17 2018
next sibling parent Alex <sascha.orlov gmail.com> writes:
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
Jul 18 2018
prev sibling parent reply Nicholas Wilson <iamthewilsonator hotmail.com> writes:
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?
Ahh, the joys of memory corruption.
Jul 18 2018
next sibling parent reply baz dlang-community <b2.temp gmx.com> writes:
On Wednesday, 18 July 2018 at 11:22:36 UTC, Nicholas Wilson wrote:
 On Wednesday, 18 July 2018 at 05:54:48 UTC, Nicholas Wilson 
 wrote:
 [...]
Ahh, the joys of memory corruption.
You've reached https://issues.dlang.org/show_bug.cgi?id=16519 maybe ?
Jul 18 2018
parent Nicholas Wilson <iamthewilsonator hotmail.com> writes:
On Wednesday, 18 July 2018 at 11:29:42 UTC, baz dlang-community 
wrote:
 On Wednesday, 18 July 2018 at 11:22:36 UTC, Nicholas Wilson 
 wrote:
 On Wednesday, 18 July 2018 at 05:54:48 UTC, Nicholas Wilson 
 wrote:
 [...]
Ahh, the joys of memory corruption.
You've reached https://issues.dlang.org/show_bug.cgi?id=16519 maybe ?
Not sure if it was that or some of the other memory corruption I was experiencing.
Jul 18 2018
prev sibling parent Seb <seb wilzba.ch> writes:
On Wednesday, 18 July 2018 at 11:22:36 UTC, Nicholas Wilson wrote:
 On Wednesday, 18 July 2018 at 05:54:48 UTC, Nicholas Wilson 
 wrote:
 [...]
Ahh, the joys of memory corruption.
Yep, actually this one is a very common one. However, -dip1000 would warn you here ...
Jul 18 2018