github.com/Hnampk/fabric@v2.1.1+incompatible/common/util/utils.go (about)

     1  /*
     2  Copyright IBM Corp. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package util
     8  
     9  import (
    10  	"crypto/rand"
    11  	"fmt"
    12  	"io"
    13  	"time"
    14  
    15  	"github.com/golang/protobuf/ptypes/timestamp"
    16  	"github.com/hyperledger/fabric/bccsp"
    17  	"github.com/hyperledger/fabric/bccsp/factory"
    18  )
    19  
    20  // ComputeSHA256 returns SHA2-256 on data
    21  func ComputeSHA256(data []byte) (hash []byte) {
    22  	hash, err := factory.GetDefault().Hash(data, &bccsp.SHA256Opts{})
    23  	if err != nil {
    24  		panic(fmt.Errorf("Failed computing SHA256 on [% x]", data))
    25  	}
    26  	return
    27  }
    28  
    29  // ComputeSHA3256 returns SHA3-256 on data
    30  func ComputeSHA3256(data []byte) (hash []byte) {
    31  	hash, err := factory.GetDefault().Hash(data, &bccsp.SHA3_256Opts{})
    32  	if err != nil {
    33  		panic(fmt.Errorf("Failed computing SHA3_256 on [% x]", data))
    34  	}
    35  	return
    36  }
    37  
    38  // GenerateBytesUUID returns a UUID based on RFC 4122 returning the generated bytes
    39  func GenerateBytesUUID() []byte {
    40  	uuid := make([]byte, 16)
    41  	_, err := io.ReadFull(rand.Reader, uuid)
    42  	if err != nil {
    43  		panic(fmt.Sprintf("Error generating UUID: %s", err))
    44  	}
    45  
    46  	// variant bits; see section 4.1.1
    47  	uuid[8] = uuid[8]&^0xc0 | 0x80
    48  
    49  	// version 4 (pseudo-random); see section 4.1.3
    50  	uuid[6] = uuid[6]&^0xf0 | 0x40
    51  
    52  	return uuid
    53  }
    54  
    55  // GenerateUUID returns a UUID based on RFC 4122
    56  func GenerateUUID() string {
    57  	uuid := GenerateBytesUUID()
    58  	return idBytesToStr(uuid)
    59  }
    60  
    61  // CreateUtcTimestamp returns a google/protobuf/Timestamp in UTC
    62  func CreateUtcTimestamp() *timestamp.Timestamp {
    63  	now := time.Now().UTC()
    64  	secs := now.Unix()
    65  	nanos := int32(now.UnixNano() - (secs * 1000000000))
    66  	return &(timestamp.Timestamp{Seconds: secs, Nanos: nanos})
    67  }
    68  
    69  func idBytesToStr(id []byte) string {
    70  	return fmt.Sprintf("%x-%x-%x-%x-%x", id[0:4], id[4:6], id[6:8], id[8:10], id[10:])
    71  }
    72  
    73  // ToChaincodeArgs converts string args to []byte args
    74  func ToChaincodeArgs(args ...string) [][]byte {
    75  	bargs := make([][]byte, len(args))
    76  	for i, arg := range args {
    77  		bargs[i] = []byte(arg)
    78  	}
    79  	return bargs
    80  }
    81  
    82  // ConcatenateBytes is useful for combining multiple arrays of bytes, especially for
    83  // signatures or digests over multiple fields
    84  func ConcatenateBytes(data ...[]byte) []byte {
    85  	finalLength := 0
    86  	for _, slice := range data {
    87  		finalLength += len(slice)
    88  	}
    89  	result := make([]byte, finalLength)
    90  	last := 0
    91  	for _, slice := range data {
    92  		for i := range slice {
    93  			result[i+last] = slice[i]
    94  		}
    95  		last += len(slice)
    96  	}
    97  	return result
    98  }