Use binary.BigEndian and attribute source
continuous-integration/woodpecker the build was successful Details

Signed-off-by: jolheiser <john.olheiser@gmail.com>
main
jolheiser 2021-11-21 21:45:09 -06:00
parent 0e965a5dfa
commit 7e73fb5238
Signed by: jolheiser
GPG Key ID: B853ADA5DA7BBF7A
4 changed files with 17 additions and 21 deletions

View File

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

View File

@ -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)
}

View File

@ -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
}

View File

@ -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)