Common hash functions FNV and MD5

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Before introducing the hash function, say the hash result of the Golang. The /hash/ hash.go file under the package defines the interface for the hash function. All hash functions implement this interface.

// Hash is the common interface implemented by all hash functions.type Hash interface {// Write (via the embedded io.Writer interface) adds more data to the running hash.// It never returns an error.io.Writer// Sum appends the current hash to b and returns the resulting slice.// It does not change the underlying hash state.Sum(b []byte) []byte// Reset resets the Hash to its initial state.Reset()// Size returns the number of bytes Sum will return.Size() int// BlockSize returns the hash's underlying block size.// The Write method must be able to accept any amount// of data, but it may operate more efficiently if all writes// are a multiple of the block size.BlockSize() int}

This interface provides commonly used Sum functions, in addition, it inherits the io.Writer interface. The interface is defined in the io io.go file in the package. The interface defines the Write write function.

// Writer is the interface that wraps the basic Write method.//// Write writes len(p) bytes from p to the underlying data stream.// It returns the number of bytes written from p (0 <= n <= len(p))// and any error encountered that caused the write to stop early.// Write must return a non-nil error if it returns n < len(p).type Writer interface {Write(p []byte) (n int, err error)}

The common hash function call process is generally the case, initializing the hash. After the hash, the Write hash result is obtained through the function by writing the data Sum . where Sum(b []byte) []byte parameter B of the function, when passed nil in, will return the hash result directly, and B is not empty, the hash result will be appended to B.

MD5 encryption algorithm called the following, source:

package mainimport ("crypto/md5""encoding/hex")func main() {m := md5.New()m.Write([]byte("hello, world"))s := hex.EncodeToString(m.Sum(nil))println(s)}

FNV encryption call is also very simple, source code. fnv.New32()uses a hash of the 32-bit FNV-1 algorithm.

package mainimport "fmt"import "hash/fnv"import "encoding/hex"func main() {a := fnv.New32()a.Write([]byte("hello"))fmt.Println(hex.EncodeToString(a.Sum(nil)))}

FNV is a non-cryptographic hashing algorithm that supports 32-bit, 64-bit, 128-bit, 256-bit, 512-bit, and 1024-bit hashes. Collisions are relatively low, the specific impact of the comparison can be checked online.

The direct hash is all the integer type, the direct output of the words will be garbled, if you want to see the normal hash results, you need to convert the results to the corresponding representation of the character. Using packages encoding/hex can help implement hexadecimal encoding.

Add a little bit of ... Foundation Bad Pit yourself ...

The MD5 encryption result is 32 16 decimal digits, while a word energy-saving representation ranges from 0~255,256=16x16, which means that two hexadecimal digits represent a byte. So Sum the function gets the byte array length is 16, tangle me half a day ...

###### References + "1" package fnv-the Go Programming language+ "2" Fowler–noll–vo hash function-wikipedia+ "3" FNV hashing algorithm -Sisi into code

Original link: Common hash functions FNV and MD5, reproduced please specify the source!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.