-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencode.go
50 lines (38 loc) · 784 Bytes
/
encode.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package eip55
import (
"encoding/hex"
"golang.org/x/crypto/sha3"
)
func Encode(src [ArrayLength]byte) string {
var encoded [ArrayLength*2 + lenprefix]byte
{
copy(encoded[:lenprefix], prefix)
hex.Encode(encoded[lenprefix:], src[:])
}
var digest []byte
{
hashfunc := sha3.NewLegacyKeccak256()
hashfunc.Write(encoded[lenprefix:])
digest = hashfunc.Sum(nil)
}
{
var limit int = len(encoded)
for i:=lenprefix; i<limit; i++ {
var b byte = encoded[i]
if b < 'a' || 'z' < b {
continue
}
var bb byte = digest[(i-lenprefix)/2]
if 0 == (i%2) {
bb = bb >> 4
}
const mask byte = 0b0000_1000
bb &= mask
if mask == bb && 'a' <= b && b <= 'z' {
encoded[i] -= 'a'
encoded[i] += 'A'
}
}
}
return string(encoded[:])
}