Skip to content

Commit

Permalink
v0.15.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Awn authored Apr 28, 2018
2 parents 2c77d7d + 0263957 commit f431f51
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
10 changes: 6 additions & 4 deletions internals.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,11 @@ func fillRandBytes(b []byte) {

// Wipes a byte slice with zeroes.
func wipeBytes(buf []byte) {
// Iterate over the slice...
for i := 0; i < len(buf); i++ {
// ... setting each element to zero.
buf[i] = byte(0)
if len(buf) == 0 {
return
}
buf[0] = 0
for bp := 1; bp < len(buf); bp *= 2 {
copy(buf[bp:], buf[:bp])
}
}
9 changes: 9 additions & 0 deletions memguard.go
Original file line number Diff line number Diff line change
Expand Up @@ -803,6 +803,15 @@ func Trim(b *LockedBuffer, offset, size int) (*LockedBuffer, error) {
return newBuf, nil
}

/*
WipeBytes zeroes out a given byte slice. It is recommeded that you call WipeBytes on slices after utilizing the Copy or CopyAt methods.
Due to the nature of memory allocated by the Go runtime, WipeBytes cannot guarantee that the data does not exist elsewhere in memory. Therefore, your program should aim to (when possible) store sensitive data only in LockedBuffers.
*/
func WipeBytes(b []byte) {
wipeBytes(b)
}

/*
DestroyAll calls Destroy on all LockedBuffers that have not already been destroyed.
Expand Down
21 changes: 21 additions & 0 deletions memguard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -693,6 +693,27 @@ func TestTrim(t *testing.T) {
}
}

func TestWipeBytes(t *testing.T) {
// Create random byte slice.
b := make([]byte, 32)
fillRandBytes(b)

// Wipe it.
WipeBytes(b)

// Check.
if !bytes.Equal(b, make([]byte, 32)) {
t.Error("unsuccessful wipe")
}

// Try with empty list.
ebuf := make([]byte, 0)
WipeBytes(ebuf)
if len(ebuf) != 0 || cap(ebuf) != 0 {
t.Error("changes made to zero-sized slice")
}
}

func TestCatchInterrupt(t *testing.T) {
CatchInterrupt(func() {})

Expand Down

0 comments on commit f431f51

Please sign in to comment.