...

Source file src/golang.org/x/mod/sumdb/note/example_test.go

Documentation: golang.org/x/mod/sumdb/note

     1  // Copyright 2019 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 note_test
     6  
     7  import (
     8  	"fmt"
     9  	"io"
    10  	"os"
    11  
    12  	"golang.org/x/mod/sumdb/note"
    13  )
    14  
    15  func ExampleSign() {
    16  	skey := "PRIVATE+KEY+PeterNeumann+c74f20a3+AYEKFALVFGyNhPJEMzD1QIDr+Y7hfZx09iUvxdXHKDFz"
    17  	text := "If you think cryptography is the answer to your problem,\n" +
    18  		"then you don't know what your problem is.\n"
    19  
    20  	signer, err := note.NewSigner(skey)
    21  	if err != nil {
    22  		fmt.Println(err)
    23  		return
    24  	}
    25  
    26  	msg, err := note.Sign(&note.Note{Text: text}, signer)
    27  	if err != nil {
    28  		fmt.Println(err)
    29  		return
    30  	}
    31  	os.Stdout.Write(msg)
    32  
    33  	// Output:
    34  	// If you think cryptography is the answer to your problem,
    35  	// then you don't know what your problem is.
    36  	//
    37  	// — PeterNeumann x08go/ZJkuBS9UG/SffcvIAQxVBtiFupLLr8pAcElZInNIuGUgYN1FFYC2pZSNXgKvqfqdngotpRZb6KE6RyyBwJnAM=
    38  }
    39  
    40  func ExampleOpen() {
    41  	vkey := "PeterNeumann+c74f20a3+ARpc2QcUPDhMQegwxbzhKqiBfsVkmqq/LDE4izWy10TW"
    42  	msg := []byte("If you think cryptography is the answer to your problem,\n" +
    43  		"then you don't know what your problem is.\n" +
    44  		"\n" +
    45  		"— PeterNeumann x08go/ZJkuBS9UG/SffcvIAQxVBtiFupLLr8pAcElZInNIuGUgYN1FFYC2pZSNXgKvqfqdngotpRZb6KE6RyyBwJnAM=\n")
    46  
    47  	verifier, err := note.NewVerifier(vkey)
    48  	if err != nil {
    49  		fmt.Println(err)
    50  		return
    51  	}
    52  	verifiers := note.VerifierList(verifier)
    53  
    54  	n, err := note.Open(msg, verifiers)
    55  	if err != nil {
    56  		fmt.Println(err)
    57  		return
    58  	}
    59  	fmt.Printf("%s (%08x):\n%s", n.Sigs[0].Name, n.Sigs[0].Hash, n.Text)
    60  
    61  	// Output:
    62  	// PeterNeumann (c74f20a3):
    63  	// If you think cryptography is the answer to your problem,
    64  	// then you don't know what your problem is.
    65  }
    66  
    67  var rand = struct {
    68  	Reader io.Reader
    69  }{
    70  	zeroReader{},
    71  }
    72  
    73  type zeroReader struct{}
    74  
    75  func (zeroReader) Read(buf []byte) (int, error) {
    76  	for i := range buf {
    77  		buf[i] = 0
    78  	}
    79  	return len(buf), nil
    80  }
    81  
    82  func ExampleSign_add_signatures() {
    83  	vkey := "PeterNeumann+c74f20a3+ARpc2QcUPDhMQegwxbzhKqiBfsVkmqq/LDE4izWy10TW"
    84  	msg := []byte("If you think cryptography is the answer to your problem,\n" +
    85  		"then you don't know what your problem is.\n" +
    86  		"\n" +
    87  		"— PeterNeumann x08go/ZJkuBS9UG/SffcvIAQxVBtiFupLLr8pAcElZInNIuGUgYN1FFYC2pZSNXgKvqfqdngotpRZb6KE6RyyBwJnAM=\n")
    88  
    89  	verifier, err := note.NewVerifier(vkey)
    90  	if err != nil {
    91  		fmt.Println(err)
    92  		return
    93  	}
    94  	verifiers := note.VerifierList(verifier)
    95  
    96  	n, err := note.Open(msg, verifiers)
    97  	if err != nil {
    98  		fmt.Println(err)
    99  		return
   100  	}
   101  
   102  	skey, vkey, err := note.GenerateKey(rand.Reader, "EnochRoot")
   103  	if err != nil {
   104  		fmt.Println(err)
   105  		return
   106  	}
   107  	_ = vkey // give to verifiers
   108  
   109  	me, err := note.NewSigner(skey)
   110  	if err != nil {
   111  		fmt.Println(err)
   112  		return
   113  	}
   114  
   115  	msg, err = note.Sign(n, me)
   116  	if err != nil {
   117  		fmt.Println(err)
   118  		return
   119  	}
   120  	os.Stdout.Write(msg)
   121  
   122  	// Output:
   123  	// If you think cryptography is the answer to your problem,
   124  	// then you don't know what your problem is.
   125  	//
   126  	// — PeterNeumann x08go/ZJkuBS9UG/SffcvIAQxVBtiFupLLr8pAcElZInNIuGUgYN1FFYC2pZSNXgKvqfqdngotpRZb6KE6RyyBwJnAM=
   127  	// — EnochRoot rwz+eBzmZa0SO3NbfRGzPCpDckykFXSdeX+MNtCOXm2/5n2tiOHp+vAF1aGrQ5ovTG01oOTGwnWLox33WWd1RvMc+QQ=
   128  }
   129  

View as plain text