...

Source file src/golang.org/x/crypto/nacl/auth/example_test.go

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

     1  // Copyright 2017 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 auth_test
     6  
     7  import (
     8  	"encoding/hex"
     9  	"fmt"
    10  
    11  	"golang.org/x/crypto/nacl/auth"
    12  )
    13  
    14  func Example() {
    15  	// Load your secret key from a safe place and reuse it across multiple
    16  	// Sum calls. (Obviously don't use this example key for anything
    17  	// real.) If you want to convert a passphrase to a key, use a suitable
    18  	// package like bcrypt or scrypt.
    19  	secretKeyBytes, err := hex.DecodeString("6368616e676520746869732070617373776f726420746f206120736563726574")
    20  	if err != nil {
    21  		panic(err)
    22  	}
    23  
    24  	var secretKey [32]byte
    25  	copy(secretKey[:], secretKeyBytes)
    26  
    27  	mac := auth.Sum([]byte("hello world"), &secretKey)
    28  	fmt.Printf("%x\n", *mac)
    29  	result := auth.Verify(mac[:], []byte("hello world"), &secretKey)
    30  	fmt.Println(result)
    31  	badResult := auth.Verify(mac[:], []byte("different message"), &secretKey)
    32  	fmt.Println(badResult)
    33  	// Output: eca5a521f3d77b63f567fb0cb6f5f2d200641bc8dada42f60c5f881260c30317
    34  	// true
    35  	// false
    36  }
    37  

View as plain text