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 // http://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function.
24 offset64
= 14695981039346656037
26 prime64
= 1099511628211
29 // New32 returns a new 32-bit FNV-1 hash.Hash.
30 func New32() hash
.Hash32
{
31 var s sum32
= offset32
35 // New32a returns a new 32-bit FNV-1a hash.Hash.
36 func New32a() hash
.Hash32
{
37 var s sum32a
= offset32
41 // New64 returns a new 64-bit FNV-1 hash.Hash.
42 func New64() hash
.Hash64
{
43 var s sum64
= offset64
47 // New64a returns a new 64-bit FNV-1a hash.Hash.
48 func New64a() hash
.Hash64
{
49 var s sum64a
= offset64
53 func (s
*sum32
) Reset() { *s
= offset32
}
54 func (s
*sum32a
) Reset() { *s
= offset32
}
55 func (s
*sum64
) Reset() { *s
= offset64
}
56 func (s
*sum64a
) Reset() { *s
= offset64
}
58 func (s
*sum32
) Sum32() uint32 { return uint32(*s
) }
59 func (s
*sum32a
) Sum32() uint32 { return uint32(*s
) }
60 func (s
*sum64
) Sum64() uint64 { return uint64(*s
) }
61 func (s
*sum64a
) Sum64() uint64 { return uint64(*s
) }
63 func (s
*sum32
) Write(data
[]byte) (int, error
) {
65 for _
, c
:= range data
{
73 func (s
*sum32a
) Write(data
[]byte) (int, error
) {
75 for _
, c
:= range data
{
83 func (s
*sum64
) Write(data
[]byte) (int, error
) {
85 for _
, c
:= range data
{
93 func (s
*sum64a
) Write(data
[]byte) (int, error
) {
95 for _
, c
:= range data
{
100 return len(data
), nil
103 func (s
*sum32
) Size() int { return 4 }
104 func (s
*sum32a
) Size() int { return 4 }
105 func (s
*sum64
) Size() int { return 8 }
106 func (s
*sum64a
) Size() int { return 8 }
108 func (s
*sum32
) BlockSize() int { return 1 }
109 func (s
*sum32a
) BlockSize() int { return 1 }
110 func (s
*sum64
) BlockSize() int { return 1 }
111 func (s
*sum64a
) BlockSize() int { return 1 }
113 func (s
*sum32
) Sum(in
[]byte) []byte {
115 return append(in
, byte(v
>>24), byte(v
>>16), byte(v
>>8), byte(v
))
118 func (s
*sum32a
) Sum(in
[]byte) []byte {
120 return append(in
, byte(v
>>24), byte(v
>>16), byte(v
>>8), byte(v
))
123 func (s
*sum64
) Sum(in
[]byte) []byte {
125 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
))
128 func (s
*sum64a
) Sum(in
[]byte) []byte {
130 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
))