digitalmars.D.learn - Converting uint[] slice to string for the purpose of hashing?
- Enjoys Math (2/2) Jul 23 2015 1. Is the best way to hash a uint[] slice
- cym13 (13/15) Jul 23 2015 IIRC, std.digest functions take ubyte[] as input, so to hash a
- Enjoys Math (15/31) Jul 23 2015 Thanks. That worked. Here's my code:
- Enjoys Math (7/23) Jul 23 2015 Actually, uint[] seems to be hashable:
- Temtaime (2/2) Jul 23 2015 All types are hashable and for your own structs and classes you
- Jonathan M Davis via Digitalmars-d-learn (3/5) Jul 23 2015 It's toHash, actually, but yeah.
1. Is the best way to hash a uint[] slice 2. How do you do it?
Jul 23 2015
On Thursday, 23 July 2015 at 11:15:46 UTC, Enjoys Math wrote:1. Is the best way to hash a uint[] slice 2. How do you do it?IIRC, std.digest functions take ubyte[] as input, so to hash a uint[] I would do the following: void main(string[] args) { import std.conv; import std.digest.md; int[] a = [1, 2, 3, 4, 5]; auto md5 = new MD5Digest(); md5.put(a.to!(ubyte[])); auto hash = md5.finish(); writeln(hash); }
Jul 23 2015
On Thursday, 23 July 2015 at 11:49:05 UTC, cym13 wrote:On Thursday, 23 July 2015 at 11:15:46 UTC, Enjoys Math wrote:Thanks. That worked. Here's my code: module hashtools; import std.conv; import std.digest.md; string uintSliceToHash(const uint[] slice) { auto md5 = new MD5Digest(); md5.put(slice.to!(ubyte[])); return md5.finish().to!(string); } unittest { import std.stdio; uint[] slice = [1,2,3,4]; writeln(uintSliceToHash(slice)); }1. Is the best way to hash a uint[] slice 2. How do you do it?IIRC, std.digest functions take ubyte[] as input, so to hash a uint[] I would do the following: void main(string[] args) { import std.conv; import std.digest.md; int[] a = [1, 2, 3, 4, 5]; auto md5 = new MD5Digest(); md5.put(a.to!(ubyte[])); auto hash = md5.finish(); writeln(hash); }
Jul 23 2015
On Thursday, 23 July 2015 at 12:10:04 UTC, Enjoys Math wrote:On Thursday, 23 July 2015 at 11:49:05 UTC, cym13 wrote:Actually, uint[] seems to be hashable: import std.stdio; int[uint[]] aa; aa[[1,2,3]] = 5; writeln(aa[[1,2,3]]); WORKS[...]Thanks. That worked. Here's my code: module hashtools; import std.conv; import std.digest.md; string uintSliceToHash(const uint[] slice) { auto md5 = new MD5Digest(); md5.put(slice.to!(ubyte[])); return md5.finish().to!(string); } unittest { import std.stdio; uint[] slice = [1,2,3,4]; writeln(uintSliceToHash(slice)); }
Jul 23 2015
All types are hashable and for your own structs and classes you can redefine opHash
Jul 23 2015
On Thursday, July 23, 2015 12:56:13 Temtaime via Digitalmars-d-learn wrote:All types are hashable and for your own structs and classes you can redefine opHashIt's toHash, actually, but yeah. - Jonathan M Davis
Jul 23 2015