-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata.go
50 lines (40 loc) · 840 Bytes
/
data.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 main
import (
"encoding/json"
"os"
"golang.org/x/crypto/bcrypt"
)
var sd *StreamData
type App struct {
Name string `json:"name"`
EncryptedStreamKey []byte `json:"enc_stream_key"`
}
type StreamData struct {
Apps []App `json:"apps"`
}
func (sd *StreamData) App(sk string) (*App, bool) {
if sd == nil {
return nil, false
}
var err error
for _, app := range sd.Apps {
if err = bcrypt.CompareHashAndPassword(app.EncryptedStreamKey, []byte(sk)); err == nil {
return &app, true
}
}
return nil, false
}
func loadStreamData(configPath string) (*StreamData, error) {
f, err := os.Open(configPath)
if err != nil {
return nil, err
}
var sd StreamData
dec := json.NewDecoder(f)
dec.DisallowUnknownFields()
err = dec.Decode(&sd)
if err != nil {
return nil, err
}
return &sd, f.Close()
}