Golang implementation ANProto: the Authenticated and Non-networked protocol or ANother protocol
at main 47 lines 1.0 kB view raw
1package goan 2 3import ( 4 "crypto/sha256" 5 "encoding/base64" 6 "testing" 7) 8 9func TestGenHashSignOpen(t *testing.T) { 10 k, err := Gen() 11 if err != nil { 12 t.Fatalf("Gen error: %v", err) 13 } 14 if len(k) != 132 { 15 t.Fatalf("expected key length 132 got %d", len(k)) 16 } 17 18 // verify hash function still works 19 h := Hash("hello") 20 exph := sha256.Sum256([]byte("hello")) 21 expb64 := base64.StdEncoding.EncodeToString(exph[:]) 22 if h != expb64 { 23 t.Fatalf("hash mismatch") 24 } 25 26 // Sign the plain text message "hello" (not its hash) 27 signed, err := Sign("hello", k) 28 if err != nil { 29 t.Fatalf("sign error: %v", err) 30 } 31 if signed[:44] != k[:44] { 32 t.Fatalf("signed must start with pub key") 33 } 34 35 opened, err := Open(signed) 36 if err != nil { 37 t.Fatalf("open error: %v", err) 38 } 39 // For compatibility with example.js, the test strips the first 13 bytes (timestamp) 40 if len(opened) < 13 { 41 t.Fatalf("opened too short to strip timestamp") 42 } 43 payload := opened[13:] 44 if payload != "hello" { 45 t.Fatalf("opened payload must equal 'hello', got: %q", payload) 46 } 47}