/
util.go
56 lines (50 loc) · 1.1 KB
/
util.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 shared
import (
"crypto/subtle"
"encoding/base64"
"errors"
"github.com/dchest/siphash"
"gopkg.in/mgo.v2/bson"
"os"
)
var idHashKey []byte = []byte("semi-private-key")
func exists(name string) bool {
if _, err := os.Stat(name); err != nil {
if os.IsNotExist(err) {
return false
}
}
return true
}
func validateId(id []byte) error {
if len(id) != 16 {
return errors.New("valid ids are 16 bytes (126 bit) long.")
}
h := siphash.New(idHashKey)
h.Write(id[4:16])
if subtle.ConstantTimeCompare(h.Sum(nil)[0:4], id[0:4]) != 1 {
return errors.New("invalid id checksum")
}
return nil
}
func ObjectIdToPublicId(id bson.ObjectId) string {
if id == "" {
return ""
}
h := siphash.New(idHashKey)
h.Write([]byte(id))
return base64.URLEncoding.EncodeToString(append(h.Sum(nil)[0:4], []byte(id)...))
}
func PublicIdToObjectId(id string) (bson.ObjectId, error) {
if id == "" {
return "", nil
}
cypher, err := base64.URLEncoding.DecodeString(id)
if err != nil {
return "", err
}
if err := validateId(cypher); err != nil {
return "", err
}
return bson.ObjectId(cypher[4:16]), nil
}