paulmillr / noble-hashes

Audited & minimal JS implementation of hash functions, MACs and KDFs.
https://paulmillr.com/noble
MIT License
573 stars 46 forks source link

support sha512_256? #1

Closed aulneau closed 3 years ago

aulneau commented 3 years ago

This is great! Love your work @paulmillr!

I maintain the stacks library micro-stacks, and will likely use noble-hashes for all the hashing needs. We use sha512_256 for a few things, would it be possible for you to add this variant to noble-hashes?

🙏

paulmillr commented 3 years ago

Hey. Is it the same as sha512().slice(0, 256)?

paulmillr commented 3 years ago

I knew about this method for a while; but never really saw a usage. Do you have some specific use case?

aulneau commented 3 years ago

Hey. Is it the same as sha512().slice(0, 256)?

It is not, there are some bytes that change depending on if it's 512/224 or 512/256, here is another implementation: https://github.com/emn178/js-sha512/blob/master/src/sha512.js#L166-L182

I knew about this method for a while; but never really saw a usage. Do you have some specific use case?

Yeah, txIds, among other identifiers in Stacks are sha512/256. I was not present when this choice was made, but in asking someone who was:

sha512 truncated to 256 bits is just as secure (or more) as the "double sha256" used by bitcoin (it's why it's a standard hash function) but it's also much faster to compute on most computers

A number of things use it, actually. see SIP-005