Use binary.BigEndian and attribute source
continuous-integration/woodpecker the build was successful
Details
continuous-integration/woodpecker the build was successful
Details
Signed-off-by: jolheiser <john.olheiser@gmail.com>main
parent
0e965a5dfa
commit
7e73fb5238
|
@ -1,3 +1,7 @@
|
||||||
|
# Implementation
|
||||||
|
|
||||||
|
Adapted from [source](https://spectre.app/blog/2018-01-06-algorithm/).
|
||||||
|
|
||||||
## User Key Derivation
|
## User Key Derivation
|
||||||
([code](user_key.go))
|
([code](user_key.go))
|
||||||
|
|
||||||
|
|
17
site_key.go
17
site_key.go
|
@ -8,24 +8,13 @@ import (
|
||||||
func siteKey(userKey []byte, scoper Scoper, siteName string, counter int, scope Scope) []byte {
|
func siteKey(userKey []byte, scoper Scoper, siteName string, counter int, scope Scope) []byte {
|
||||||
nameBytes := []byte(siteName)
|
nameBytes := []byte(siteName)
|
||||||
scopeBytes := []byte(scoper.Scope(scope))
|
scopeBytes := []byte(scoper.Scope(scope))
|
||||||
|
|
||||||
nameBytesLen := len(nameBytes)
|
nameBytesLen := len(nameBytes)
|
||||||
keySalt := append(scopeBytes,
|
|
||||||
byte(nameBytesLen>>24),
|
keySalt := append(scopeBytes, bigEndian(nameBytesLen)...)
|
||||||
byte(nameBytesLen>>16),
|
|
||||||
byte(nameBytesLen>>8),
|
|
||||||
byte(nameBytesLen),
|
|
||||||
)
|
|
||||||
keySalt = append(keySalt, nameBytes...)
|
keySalt = append(keySalt, nameBytes...)
|
||||||
keySalt = append(keySalt,
|
keySalt = append(keySalt, bigEndian(counter)...)
|
||||||
byte(counter>>24),
|
|
||||||
byte(counter>>16),
|
|
||||||
byte(counter>>8),
|
|
||||||
byte(counter),
|
|
||||||
)
|
|
||||||
|
|
||||||
sign := hmac.New(sha256.New, userKey)
|
sign := hmac.New(sha256.New, userKey)
|
||||||
sign.Write(keySalt)
|
sign.Write(keySalt)
|
||||||
|
|
||||||
return sign.Sum(nil)
|
return sign.Sum(nil)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
package spectre
|
package spectre
|
||||||
|
|
||||||
|
import "encoding/binary"
|
||||||
|
|
||||||
// Spectre is a spectre client
|
// Spectre is a spectre client
|
||||||
type Spectre struct {
|
type Spectre struct {
|
||||||
name string
|
name string
|
||||||
|
@ -67,3 +69,9 @@ func WithScope(s Scope) SiteOption {
|
||||||
opts.scope = s
|
opts.scope = s
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func bigEndian(num int) []byte {
|
||||||
|
buf := make([]byte, 4)
|
||||||
|
binary.BigEndian.PutUint32(buf, uint32(num))
|
||||||
|
return buf
|
||||||
|
}
|
||||||
|
|
|
@ -6,14 +6,9 @@ func userKey(name, secret string, scoper Scoper) ([]byte, error) {
|
||||||
nameBytes := []byte(name)
|
nameBytes := []byte(name)
|
||||||
secretBytes := []byte(secret)
|
secretBytes := []byte(secret)
|
||||||
keyScope := []byte(scoper.Scope(Authentication))
|
keyScope := []byte(scoper.Scope(Authentication))
|
||||||
|
|
||||||
nameBytesLen := len(nameBytes)
|
nameBytesLen := len(nameBytes)
|
||||||
keySalt := append(keyScope,
|
|
||||||
byte(nameBytesLen>>24),
|
keySalt := append(keyScope, bigEndian(nameBytesLen)...)
|
||||||
byte(nameBytesLen>>16),
|
|
||||||
byte(nameBytesLen>>8),
|
|
||||||
byte(nameBytesLen),
|
|
||||||
)
|
|
||||||
keySalt = append(keySalt, nameBytes...)
|
keySalt = append(keySalt, nameBytes...)
|
||||||
|
|
||||||
return scrypt.Key(secretBytes, keySalt, 32768, 8, 2, 64)
|
return scrypt.Key(secretBytes, keySalt, 32768, 8, 2, 64)
|
||||||
|
|
Loading…
Reference in New Issue