github.com/amazechain/amc@v0.1.3/utils/bytes.go (about)

     1  package utils
     2  
     3  // These methods use copy() to convert a byte slice to a fixed size array.
     4  // This approach is used for go1.19 and below.
     5  
     6  // ToBytes4 is a convenience method for converting a byte slice to a fix
     7  // sized 4 byte array. This method will truncate the input if it is larger
     8  // than 4 bytes.
     9  func ToBytes4(x []byte) [4]byte {
    10  	var y [4]byte
    11  	copy(y[:], x)
    12  	return y
    13  }
    14  
    15  // ToBytes20 is a convenience method for converting a byte slice to a fix
    16  // sized 20 byte array. This method will truncate the input if it is larger
    17  // than 20 bytes.
    18  func ToBytes20(x []byte) [20]byte {
    19  	var y [20]byte
    20  	copy(y[:], x)
    21  	return y
    22  }
    23  
    24  // ToBytes32 is a convenience method for converting a byte slice to a fix
    25  // sized 32 byte array. This method will truncate the input if it is larger
    26  // than 32 bytes.
    27  func ToBytes32(x []byte) [32]byte {
    28  	var y [32]byte
    29  	copy(y[:], x)
    30  	return y
    31  }
    32  
    33  // ToBytes48 is a convenience method for converting a byte slice to a fix
    34  // sized 48 byte array. This method will truncate the input if it is larger
    35  // than 48 bytes.
    36  func ToBytes48(x []byte) [48]byte {
    37  	var y [48]byte
    38  	copy(y[:], x)
    39  	return y
    40  }
    41  
    42  // ToBytes64 is a convenience method for converting a byte slice to a fix
    43  // sized 64 byte array. This method will truncate the input if it is larger
    44  // than 64 bytes.
    45  func ToBytes64(x []byte) [64]byte {
    46  	var y [64]byte
    47  	copy(y[:], x)
    48  	return y
    49  }
    50  
    51  // ToBytes96 is a convenience method for converting a byte slice to a fix
    52  // sized 96 byte array. This method will truncate the input if it is larger
    53  // than 96 bytes.
    54  func ToBytes96(x []byte) [96]byte {
    55  	var y [96]byte
    56  	copy(y[:], x)
    57  	return y
    58  }