github.com/decred/dcrlnd@v0.7.6/internal/zero/slice.go (about)

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