Checkpoint 5
This commit is contained in:
40
hash/godoor_test.go
Normal file
40
hash/godoor_test.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package hash
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestHash(t *testing.T) {
|
||||
hash := HashCardUid(0x000000000F0AD2301)
|
||||
if hash != "a6d9ba36ecb5f8e6312f40ee260ad59e9cca3c6ce073bf072df3324c0072886196e6823a7c758ab567fc53e91fbbda297a4efe0072e41316c56446ef126a5180" {
|
||||
t.Errorf("WRONG Key generated: %q", hash)
|
||||
}
|
||||
|
||||
hash = HashCardUid(0x3CBB5AAD)
|
||||
if hash != "1e7bbc1aeeff4bfe58ea8d97df7cd0f97599fa31633d3a1f57d138549b3968c20affb186fe0b85570d34718b273d269bcd28b2be59faf8122451d91a2cdc5f18" {
|
||||
t.Errorf("WRONG Key generated: %q", hash)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBits(t *testing.T) {
|
||||
|
||||
bits := make([]bool, 64)
|
||||
trueBits := []int{0, 1, 2, 3, 8, 10, 12, 13, 15, 18, 22, 23, 31}
|
||||
for _, i := range trueBits {
|
||||
bits[i] = true
|
||||
}
|
||||
|
||||
var card uint64 = 0
|
||||
for i := 63; i >= 0; i-- {
|
||||
if bits[i] == true {
|
||||
card |= 1 << (63 - i)
|
||||
}
|
||||
}
|
||||
|
||||
cardId := fmt.Sprintf("%x", card)
|
||||
fmt.Println(cardId)
|
||||
if card != 0xF0AD230100000000 {
|
||||
panic(fmt.Errorf("WROOONG: %x", card))
|
||||
}
|
||||
}
|
21
hash/hash.go
Normal file
21
hash/hash.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package hash
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"encoding/hex"
|
||||
|
||||
"golang.org/x/crypto/scrypt"
|
||||
)
|
||||
|
||||
func HashCardUid(card uint64) string {
|
||||
b := make([]byte, 8)
|
||||
binary.LittleEndian.PutUint64(b, card)
|
||||
|
||||
hashed, err := scrypt.Key(b, []byte("hkRXwLlQKmCJoy5qaahp"), 16384, 8, 1, 64)
|
||||
if err != nil {
|
||||
panic(err) // can only happen when scrypt params are garbage
|
||||
}
|
||||
|
||||
hashedHex := hex.EncodeToString(hashed)
|
||||
return hashedHex
|
||||
}
|
Reference in New Issue
Block a user