...

Source file src/golang.org/x/crypto/nacl/sign/sign_test.go

Documentation: golang.org/x/crypto/nacl/sign

     1  // Copyright 2018 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package sign
     6  
     7  import (
     8  	"bytes"
     9  	"crypto/rand"
    10  	"encoding/hex"
    11  	"testing"
    12  )
    13  
    14  var testSignedMessage, _ = hex.DecodeString("26a0a47f733d02ddb74589b6cbd6f64a7dab1947db79395a1a9e00e4c902c0f185b119897b89b248d16bab4ea781b5a3798d25c2984aec833dddab57e0891e0d68656c6c6f20776f726c64")
    15  var testMessage = testSignedMessage[Overhead:]
    16  var testPublicKey [32]byte
    17  var testPrivateKey = [64]byte{
    18  	0x98, 0x3c, 0x6a, 0xa6, 0x21, 0xcc, 0xbb, 0xb2, 0xa7, 0xe8, 0x97, 0x94, 0xde, 0x5f, 0xf8, 0x11,
    19  	0x8a, 0xf3, 0x33, 0x1a, 0x03, 0x5c, 0x43, 0x99, 0x03, 0x13, 0x2d, 0xd7, 0xb4, 0xc4, 0x8b, 0xb0,
    20  	0xf6, 0x33, 0x20, 0xa3, 0x34, 0x8b, 0x7b, 0xe2, 0xfe, 0xb4, 0xe7, 0x3a, 0x54, 0x08, 0x2d, 0xd7,
    21  	0x0c, 0xb7, 0xc0, 0xe3, 0xbf, 0x62, 0x6c, 0x55, 0xf0, 0x33, 0x28, 0x52, 0xf8, 0x48, 0x7d, 0xfd,
    22  }
    23  
    24  func init() {
    25  	copy(testPublicKey[:], testPrivateKey[32:])
    26  }
    27  
    28  func TestSign(t *testing.T) {
    29  	signedMessage := Sign(nil, testMessage, &testPrivateKey)
    30  	if !bytes.Equal(signedMessage, testSignedMessage) {
    31  		t.Fatalf("signed message did not match, got\n%x\n, expected\n%x", signedMessage, testSignedMessage)
    32  	}
    33  }
    34  
    35  func TestOpen(t *testing.T) {
    36  	message, ok := Open(nil, testSignedMessage, &testPublicKey)
    37  	if !ok {
    38  		t.Fatalf("valid signed message not successfully verified")
    39  	}
    40  	if !bytes.Equal(message, testMessage) {
    41  		t.Fatalf("message did not match, got\n%x\n, expected\n%x", message, testMessage)
    42  	}
    43  	_, ok = Open(nil, testSignedMessage[1:], &testPublicKey)
    44  	if ok {
    45  		t.Fatalf("invalid signed message successfully verified")
    46  	}
    47  
    48  	badMessage := make([]byte, len(testSignedMessage))
    49  	copy(badMessage, testSignedMessage)
    50  	badMessage[5] ^= 1
    51  	if _, ok := Open(nil, badMessage, &testPublicKey); ok {
    52  		t.Fatalf("Open succeeded with a corrupt message")
    53  	}
    54  
    55  	var badPublicKey [32]byte
    56  	copy(badPublicKey[:], testPublicKey[:])
    57  	badPublicKey[5] ^= 1
    58  	if _, ok := Open(nil, testSignedMessage, &badPublicKey); ok {
    59  		t.Fatalf("Open succeeded with a corrupt public key")
    60  	}
    61  }
    62  
    63  func TestGenerateSignOpen(t *testing.T) {
    64  	publicKey, privateKey, _ := GenerateKey(rand.Reader)
    65  	signedMessage := Sign(nil, testMessage, privateKey)
    66  	message, ok := Open(nil, signedMessage, publicKey)
    67  	if !ok {
    68  		t.Fatalf("failed to verify signed message")
    69  	}
    70  
    71  	if !bytes.Equal(message, testMessage) {
    72  		t.Fatalf("verified message does not match signed messge, got\n%x\n, expected\n%x", message, testMessage)
    73  	}
    74  }
    75  

View as plain text