0

I try to hash a double (1529480427715.5532) with SHA1 algorithm and i have this hash in c#:

  • use in string format: "3e8f41233f90a85f9963afaa571ba76afb8bb08d"
  • use in double format: "c880857c399c7b9cc9c6395197e700543c400b17"

but in fact i want to get this hash "da39a3ee5e6b4b0d3255bfef95601890afd80709" as result like when i'm using js-sha1 library.

Alex K.
  • 171,639
  • 30
  • 264
  • 288
Mehdi Payervand
  • 251
  • 3
  • 11
  • 1
    The hash "da39a3e..." in js-sha1 is the hash of empty strings/empty arrays. Are you sure that you want that hash? – Freggar Jun 20 '18 at 09:55
  • 1
    It seems to me that you are not correctly hashing your double from the javascript side. But why would you want to hash a double in the first place? Doubles *do* have precision errors, so I'm wondering what your use case is – Freggar Jun 20 '18 at 09:58
  • 1
    Some code i have generate above hash. I have seen mentioned js library generate it. You can re produce it by using that library (js-sha1) – Mehdi Payervand Jun 22 '18 at 07:04
  • Post the code you used. As mentioned by @xanatos js-sha1 does *not* support numbers, you would have to convert it to a string or byte array first.... – Freggar Jun 22 '18 at 07:16

1 Answers1

2

Given this code to convert from float64 (that is double in C#) to Uint8array, you can:

// c880857c399c7b9cc9c6395197e700543c400b17
var hash = sha1(convertTypedArray(new Float64Array([1529480427715.5532]), Uint8Array));

or even shorter, without using that link, because sha1 accepts ArrayBuffer as a parameter:

var hash = sha1(new Float64Array([1529480427715.5532]).buffer);

Note that sha1 accepts only some types of input, number isn't one of them.

From the examples of the library it seems strings, Array, Uint8Array, ArrayBuffer are supported.

As written by @Freggar,

// da39a3ee5e6b4b0d3255bfef95601890afd80709
var hash = sha1('');

And, using strings:

// 3e8f41233f90a85f9963afaa571ba76afb8bb08d
var hash = sha1('1529480427715.5532');
xanatos
  • 109,618
  • 12
  • 197
  • 280