Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 

37 rader
1.1 KiB

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