1 // Copyright 2011 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // Package fnv implements FNV-1 and FNV-1a, non-cryptographic hash functions
6 // created by Glenn Fowler, Landon Curt Noll, and Phong Vo.
8 // https://en.wikipedia.org/wiki/Fowler-Noll-Vo_hash_function.
24 offset64
= 14695981039346656037
26 prime64
= 1099511628211
29 // New32 returns a new 32-bit FNV-1 hash.Hash.
30 // Its Sum method will lay the value out in big-endian byte order.
31 func New32() hash
.Hash32
{
32 var s sum32
= offset32
36 // New32a returns a new 32-bit FNV-1a hash.Hash.
37 // Its Sum method will lay the value out in big-endian byte order.
38 func New32a() hash
.Hash32
{
39 var s sum32a
= offset32
43 // New64 returns a new 64-bit FNV-1 hash.Hash.
44 // Its Sum method will lay the value out in big-endian byte order.
45 func New64() hash
.Hash64
{
46 var s sum64
= offset64
50 // New64a returns a new 64-bit FNV-1a hash.Hash.
51 // Its Sum method will lay the value out in big-endian byte order.
52 func New64a() hash
.Hash64
{
53 var s sum64a
= offset64
57 func (s
*sum32
) Reset() { *s
= offset32
}
58 func (s
*sum32a
) Reset() { *s
= offset32
}
59 func (s
*sum64
) Reset() { *s
= offset64
}
60 func (s
*sum64a
) Reset() { *s
= offset64
}
62 func (s
*sum32
) Sum32() uint32 { return uint32(*s
) }
63 func (s
*sum32a
) Sum32() uint32 { return uint32(*s
) }
64 func (s
*sum64
) Sum64() uint64 { return uint64(*s
) }
65 func (s
*sum64a
) Sum64() uint64 { return uint64(*s
) }
67 func (s
*sum32
) Write(data
[]byte) (int, error
) {
69 for _
, c
:= range data
{
77 func (s
*sum32a
) Write(data
[]byte) (int, error
) {
79 for _
, c
:= range data
{
87 func (s
*sum64
) Write(data
[]byte) (int, error
) {
89 for _
, c
:= range data
{
97 func (s
*sum64a
) Write(data
[]byte) (int, error
) {
99 for _
, c
:= range data
{
104 return len(data
), nil
107 func (s
*sum32
) Size() int { return 4 }
108 func (s
*sum32a
) Size() int { return 4 }
109 func (s
*sum64
) Size() int { return 8 }
110 func (s
*sum64a
) Size() int { return 8 }
112 func (s
*sum32
) BlockSize() int { return 1 }
113 func (s
*sum32a
) BlockSize() int { return 1 }
114 func (s
*sum64
) BlockSize() int { return 1 }
115 func (s
*sum64a
) BlockSize() int { return 1 }
117 func (s
*sum32
) Sum(in
[]byte) []byte {
119 return append(in
, byte(v
>>24), byte(v
>>16), byte(v
>>8), byte(v
))
122 func (s
*sum32a
) Sum(in
[]byte) []byte {
124 return append(in
, byte(v
>>24), byte(v
>>16), byte(v
>>8), byte(v
))
127 func (s
*sum64
) Sum(in
[]byte) []byte {
129 return append(in
, byte(v
>>56), byte(v
>>48), byte(v
>>40), byte(v
>>32), byte(v
>>24), byte(v
>>16), byte(v
>>8), byte(v
))
132 func (s
*sum64a
) Sum(in
[]byte) []byte {
134 return append(in
, byte(v
>>56), byte(v
>>48), byte(v
>>40), byte(v
>>32), byte(v
>>24), byte(v
>>16), byte(v
>>8), byte(v
))