-
Notifications
You must be signed in to change notification settings - Fork 22
/
cipher.go
56 lines (44 loc) · 880 Bytes
/
cipher.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
51
52
53
54
55
56
package pokepaste
import (
"encoding/binary"
"encoding/hex"
"log"
"os"
"strconv"
"golang.org/x/crypto/blowfish"
)
var cipher *blowfish.Cipher
func init() {
var err error
cipher, err = blowfish.NewCipher([]byte(os.Getenv("POKEPASTE_KEY")))
if err != nil {
log.Fatal(err)
}
}
func encodeID(id uint64) string {
src := make([]byte, 8)
binary.BigEndian.PutUint64(src, id)
dst := make([]byte, 8)
cipher.Encrypt(dst, src)
return hex.EncodeToString(dst)
}
func decodeID(str string) (id uint64, err error) {
src, err := hex.DecodeString(str)
if err != nil {
return
}
dst := make([]byte, 8)
cipher.Decrypt(dst, src)
id = binary.BigEndian.Uint64(dst)
return
}
func decodeOldID(str string) (id uint64, err error) {
id, err = strconv.ParseUint(str, 10, 64)
if err != nil {
return
}
if id >= 256 {
id = (id * 0x7FFFFFFF) % 0x100000000
}
return
}