-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsession.go
More file actions
62 lines (54 loc) · 1.69 KB
/
Copy pathsession.go
File metadata and controls
62 lines (54 loc) · 1.69 KB
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
57
58
59
60
61
62
package masterpassword
import (
"bytes"
"code.google.com/p/go.crypto/scrypt"
"crypto/hmac"
"crypto/sha256"
"encoding/binary"
)
const n int = 32768
const r int = 8
const p int = 2
const dkLen int = 64
// Session is an encryption session and holds the master key, as well as the user name.
// Note that the password is not stored.
type Session struct {
Key []byte
Name string
}
// NewSession creates a new session for given username and password.
// This involves deriving the master key, which can be a little time consuming.
func NewSession(name string, password string) *Session {
saltBuffer := bytes.NewBuffer(nil)
saltBuffer.WriteString(prefix)
binary.Write(saltBuffer, binary.BigEndian, uint32(len(name)))
saltBuffer.WriteString(name)
key, err := scrypt.Key([]byte(password), saltBuffer.Bytes(), n, r, p, dkLen)
if nil != err {
panic(err)
}
return &Session{
Key: key,
Name: name,
}
}
// NewSite initializes a new site in this session, with a counter value of 1
func (session *Session) NewSite(name string) *Site {
return session.NewSiteWithCounter(name, 1)
}
// NewSiteWithCounter initializes a new site in this session, with a custom counter value.
func (session *Session) NewSiteWithCounter(name string, counter int) *Site {
hash := hmac.New(sha256.New, session.Key)
seedBuffer := bytes.NewBuffer(nil)
seedBuffer.WriteString(prefix)
binary.Write(seedBuffer, binary.BigEndian, uint32(len(name)))
seedBuffer.WriteString(name)
binary.Write(seedBuffer, binary.BigEndian, uint32(counter))
hash.Write(seedBuffer.Bytes())
return &Site{
Seed: hash.Sum(nil),
Session: session,
Name: name,
Counter: counter,
}
}