github.com/emmansun/gmsm@v0.29.1/drbg/example_test.go (about)

     1  package drbg_test
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  
     7  	"github.com/emmansun/gmsm/drbg"
     8  )
     9  
    10  func ExampleNewGmCtrDrbgPrng() {
    11  	prng, err := drbg.NewGmCtrDrbgPrng(nil, 32, drbg.SECURITY_LEVEL_TEST, nil)
    12  	if err != nil {
    13  		panic(err)
    14  	}
    15  	c := 10
    16  	b := make([]byte, c)
    17  	_, err = prng.Read(b)
    18  	if err != nil {
    19  		fmt.Println("error:", err)
    20  		return
    21  	}
    22  	// The slice should now contain random bytes instead of only zeroes.
    23  	fmt.Println(bytes.Equal(b, make([]byte, c)))
    24  
    25  	// Output:
    26  	// false
    27  }
    28  
    29  func ExampleNewGmHashDrbgPrng() {
    30  	prng, err := drbg.NewGmHashDrbgPrng(nil, 32, drbg.SECURITY_LEVEL_TEST, nil)
    31  	if err != nil {
    32  		panic(err)
    33  	}
    34  	c := 10
    35  	b := make([]byte, c)
    36  	_, err = prng.Read(b)
    37  	if err != nil {
    38  		fmt.Println("error:", err)
    39  		return
    40  	}
    41  	// The slice should now contain random bytes instead of only zeroes.
    42  	fmt.Println(bytes.Equal(b, make([]byte, c)))
    43  
    44  	// Output:
    45  	// false
    46  }