Browse Source

Use binary.BigEndian and attribute source

Signed-off-by: jolheiser <john.olheiser@gmail.com>
main
jolheiser 2 weeks ago
parent
commit
7e73fb5238
Signed by: jolheiser GPG Key ID: B853ADA5DA7BBF7A
  1. 4
      IMPLEMENTATION.md
  2. 17
      site_key.go
  3. 8
      spectre.go
  4. 9
      user_key.go

4
IMPLEMENTATION.md

@ -1,3 +1,7 @@
# Implementation
Adapted from [source](https://spectre.app/blog/2018-01-06-algorithm/).
## User Key Derivation
([code](user_key.go))

17
site_key.go

@ -8,24 +8,13 @@ import (
func siteKey(userKey []byte, scoper Scoper, siteName string, counter int, scope Scope) []byte {
nameBytes := []byte(siteName)
scopeBytes := []byte(scoper.Scope(scope))
nameBytesLen := len(nameBytes)
keySalt := append(scopeBytes,
byte(nameBytesLen>>24),
byte(nameBytesLen>>16),
byte(nameBytesLen>>8),
byte(nameBytesLen),
)
keySalt := append(scopeBytes, bigEndian(nameBytesLen)...)
keySalt = append(keySalt, nameBytes...)
keySalt = append(keySalt,
byte(counter>>24),
byte(counter>>16),
byte(counter>>8),
byte(counter),
)
keySalt = append(keySalt, bigEndian(counter)...)
sign := hmac.New(sha256.New, userKey)
sign.Write(keySalt)
return sign.Sum(nil)
}

8
spectre.go

@ -1,5 +1,7 @@
package spectre
import "encoding/binary"
// Spectre is a spectre client
type Spectre struct {
name string
@ -67,3 +69,9 @@ func WithScope(s Scope) SiteOption {
opts.scope = s
}
}
func bigEndian(num int) []byte {
buf := make([]byte, 4)
binary.BigEndian.PutUint32(buf, uint32(num))
return buf
}

9
user_key.go

@ -6,14 +6,9 @@ func userKey(name, secret string, scoper Scoper) ([]byte, error) {
nameBytes := []byte(name)
secretBytes := []byte(secret)
keyScope := []byte(scoper.Scope(Authentication))
nameBytesLen := len(nameBytes)
keySalt := append(keyScope,
byte(nameBytesLen>>24),
byte(nameBytesLen>>16),
byte(nameBytesLen>>8),
byte(nameBytesLen),
)
keySalt := append(keyScope, bigEndian(nameBytesLen)...)
keySalt = append(keySalt, nameBytes...)
return scrypt.Key(secretBytes, keySalt, 32768, 8, 2, 64)

Loading…
Cancel
Save