Having looked at the go sources for hash, crypto/md5, crypto/sha256 I must admit that I am a bit puzzled on how to start writing my own hashing algorithm which will return a string type as opposed to current ones which return longs/ints. Any pointers greatly appreciated
- Don’t write your own crypto. This will lead to disaster.
- The existing libraries return
hash.Hash
. Which methods that return numeric types do you refer to?
I do not want to write my own crypto, I want to implement my own hashing function which will use the exact same implementation than the standard md5/sha256 but will return a string/[]byte value which will represent my own hashing of the input. CRC32, md5 … compute int/long/… which are in turn converted to []byte as returned value from hash therefore my statement above
You can just fmt.Sprintf("%x", h.Sum(nil))
to get a hexadecimal hashsum:
package main
import (
"crypto/sha256"
"fmt"
)
func main() {
input := "The tunneling gopher digs downwards unaware of what he will find.\n"
h := sha256.New()
h.Write([]byte(input))
s := fmt.Sprintf("%x", h.Sum(nil))
fmt.Println(s)
}
Output:
b464e60d2682d09081510940d39496463b54e54f412156f39e44231fd53d7c0e
Yes I already know about this and use it for computing md5/sha256 of some data
However I’d like to write another hashing function of my own which will implement the same interface than md5/sha256 but do other computation I will write however I do not know how to proceed to achieve this
I am still not quite sure what you are looking for. But maybe the source of crypto/sha256
can help you. New
could be a starting point.
As mentioned in my first post here above, I already looked at these codes but can not figure out which function is consuming the input data to produce the hash
I think I am making some progress here. Write function seems to be the key but name was confusing to me
I now have a fully working new hashing algorithm. I was not that complicated after all but required a lot of generate and test on my side
Would you share the code you wrote, please?
This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.