slice.go raw

   1  package zero
   2  
   3  // This file implements range-based zeroing, which as of Go 1.5 is optimized using a Duff's device.
   4  import "math/big"
   5  
   6  // Bytes sets all bytes in the passed slice to zero. This is used to explicitly
   7  // clear private key material from memory.
   8  //
   9  // In general, prefer to use the fixed-sized zeroing functions (Bytea*) when
  10  // zeroing bytes as they are much more efficient than the variable sized zeroing
  11  // func Hash.
  12  func Bytes(b []byte) {
  13  	for i := range b {
  14  		b[i] = 0
  15  	}
  16  }
  17  
  18  // BigInt sets all bytes in the passed big int to zero and then sets the value to 0. This differs from simply setting
  19  // the value in that it specifically clears the underlying bytes whereas simply setting the value does not. This is
  20  // mostly useful to forcefully clear private keys.
  21  func BigInt(x *big.Int) {
  22  	b := x.Bits()
  23  	for i := range b {
  24  		b[i] = 0
  25  	}
  26  	x.SetInt64(0)
  27  }
  28