[Issue 11757] toHexString doesn't support dynamic array
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Tue Dec 17 07:09:31 PST 2013
https://d.puremagic.com/issues/show_bug.cgi?id=11757
--- Comment #3 from bearophile_hugs at eml.cc 2013-12-17 07:09:23 PST ---
(In reply to comment #2)
> Fair enough. I'm just a beginner, with a C[++] background, so keeping in mind
> that string == immutable(char[]), chars are UTF-8, and my string is pure ASCII,
> I "just" expected it to work.
> I'll close this as invalid so, but I wouldn't be against an enlightment of
> what's going on (and how to convert a string to an ubyte safely).
In D fixed-sized arrays are values. ubyte[16] is a value, it's 16 unsigned
bytes.
A string is a struct that contains two words: a length and a pointer to a
series of immutable chars.
When you have similar doubts or questions I suggest you to ask them in the
D.learn newsgroup.
Your D code has several problems. This is a fist try at improving your code,
but some small problems still need to be fixed:
import std.digest.md, std.digest.sha, std.string;
enum size_t blockSize = 64;
enum size_t digestSize = 16;
ubyte[digestSize] hmacMD5(in ubyte[blockSize] key, in ubyte[] msg)
pure nothrow {
ubyte[key.length] okp = 0x5c;
okp[] ^= key[];
ubyte[key.length] ikp = 0x36;
ikp[] ^= key[];
// Heap allocations here:
return md5Of(okp ~ md5Of(ikp ~ msg));
}
ubyte[digestSize] hmacMD5(in string key, in string msg)
pure nothrow {
ubyte[blockSize] key2;
if (key.length > blockSize)
key2[0 .. digestSize] = key.md5Of;
else if (key.length < blockSize)
key2[0 .. key.length] = key[].representation;
return hmacMD5(key2, msg.representation);
}
void main() {
immutable r = hmacMD5("key", "The quick brown fox jumps over the lazy
dog");
assert(r.toHexString == "80070713463E7749B90C2DC24911E275");
}
--
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
More information about the Digitalmars-d-bugs
mailing list