forked from gebi/scryptauth
/
scryptauth_parse.go
44 lines (40 loc) · 1.05 KB
/
scryptauth_parse.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
package scryptauth
import (
"encoding/base64"
"errors"
"fmt"
"strconv"
"strings"
)
// DecodeBase64 parses "ctxID:base64(hash):base64(salt)"
func DecodeBase64(str string) (ctxID uint, hash, salt []byte, err error) {
tmp := strings.SplitN(str, ":", 3)
if len(tmp) != 3 {
err = errors.New("Error: invalid hash string")
return
}
tmpCtxID, err := strconv.ParseUint(tmp[0], 10, 0)
if err != nil {
err = errors.New("Error: parsing ctxID parameter")
return
}
ctxID = uint(tmpCtxID)
hash, err = base64.URLEncoding.DecodeString(tmp[1])
if err != nil {
err = errors.New("Error: decoding base64 hash")
return
}
salt, err = base64.URLEncoding.DecodeString(tmp[2])
if err != nil {
err = errors.New("Error: decoding base64 salt")
return
}
return
}
// EncodeBase64 encodes into "ctxID:base64(hash):base64(salt)"
func EncodeBase64(ctxID uint, hash, salt []byte) (str string) {
b64_salt := base64.URLEncoding.EncodeToString(salt)
b64_hash := base64.URLEncoding.EncodeToString(hash)
str = fmt.Sprintf("%d:%s:%s", ctxID, b64_hash, b64_salt)
return
}