github.com/mattn/go@v0.0.0-20171011075504-07f7db3ea99f/src/crypto/sha256/example_test.go (about)

     1  // Copyright 2016 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 sha256_test
     6  
     7  import (
     8  	"crypto/sha256"
     9  	"fmt"
    10  	"io"
    11  	"log"
    12  	"os"
    13  )
    14  
    15  func ExampleSum256() {
    16  	sum := sha256.Sum256([]byte("hello world\n"))
    17  	fmt.Printf("%x", sum)
    18  	// Output: a948904f2f0f479b8f8197694b30184b0d2ed1c1cd2a1ec0fb85d299a192a447
    19  }
    20  
    21  func ExampleNew() {
    22  	h := sha256.New()
    23  	h.Write([]byte("hello world\n"))
    24  	fmt.Printf("%x", h.Sum(nil))
    25  	// Output: a948904f2f0f479b8f8197694b30184b0d2ed1c1cd2a1ec0fb85d299a192a447
    26  }
    27  
    28  func ExampleNew_file() {
    29  	f, err := os.Open("file.txt")
    30  	if err != nil {
    31  		log.Fatal(err)
    32  	}
    33  	defer f.Close()
    34  
    35  	h := sha256.New()
    36  	if _, err := io.Copy(h, f); err != nil {
    37  		log.Fatal(err)
    38  	}
    39  
    40  	fmt.Printf("%x", h.Sum(nil))
    41  }