blob: 37fae9eca294e8c26ad735fe8d2f7cf7e519bbc9 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
package internal
import (
"crypto/md5"
"fmt"
"hash/fnv"
)
// Hash is a simple wrapper around the MD5 algorithm implementation in the
// Go standard library. It takes in data and a salt and returns the hashed
// representation.
func Hash(data string, salt string) string {
output := md5.Sum([]byte(data + salt))
return fmt.Sprintf("%x", output)
}
// Fnv is a non-cryptographic hashing function based on non-cryptographic hash
// functions created by Glenn Fowler, Landon Curt Noll, and Phong Vo. This
// wraps the standard library FNV hash function by representing it as a 32 bit
// integer. Then it takes that number and "hashes" it by adding the rune's
// unicode value (as a uint 32) and then takes the modulus of 26, letting the number
// be represented as a letter of the alphabet. This is not a cryptographically
// secure operation, it is purely to replace numbers with a human-readable string
// to satisfy the requirement that any vhost with a "/" in it cannot end in a number
// (to avoid someone obtaining a vhost that is a cidr mask, it can cause issues).
func Fnv(data string) string {
h := fnv.New32()
h.Write([]byte(data))
hash := h.Sum32()
alphabet := "abcdefghijklmnopqrstuvwxyz"
res := ""
for _, char := range fmt.Sprintf("%d", hash) {
res = res + string(alphabet[(uint32(char)+hash)%26])
hash = (hash << 1) | (hash >> 31) // "rotate" the number for extra variance
}
return res
}
|