forked from unixpickle/gobfuscate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hash.go
31 lines (26 loc) · 763 Bytes
/
hash.go
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
package main
import (
"crypto/sha256"
"encoding/hex"
"strings"
)
const hashedSymbolSize = 10
// A NameHasher is added to the input of a hash function
// to make it 'impossible' to find the input value
type NameHasher []byte
// Hash hashes the padding + token.
// The case of the first letter of the token is preserved.
func (n NameHasher) Hash(token string) string {
hashArray := sha256.Sum256(append(n, []byte(token)...))
hexStr := strings.ToLower(hex.EncodeToString(hashArray[:hashedSymbolSize]))
for i, x := range hexStr {
if x >= '0' && x <= '9' {
x = 'g' + (x - '0')
hexStr = hexStr[:i] + string(x) + hexStr[i+1:]
}
}
if strings.ToUpper(token[:1]) == token[:1] {
hexStr = strings.ToUpper(hexStr[:1]) + hexStr[1:]
}
return hexStr
}