useAndWipe

inline fun <T> useAndWipe(data: ByteArray, block: (ByteArray) -> T): T

Executes a block with a byte array and automatically wipes it afterward.

Use case: Ensures memory is wiped even if exceptions occur:

val keys = MemoryUtils.useAndWipe(seed) { seedBytes ->
deriveKeys(seedBytes)
}
// seed is wiped here, even if deriveKeys() throws

Guarantee:

  • The data is ALWAYS wiped after use, even on exceptions

  • Exceptions are re-thrown after wiping

Return

Result of the block

Parameters

data

Byte array to use and wipe

block

Lambda that uses the byte array

Throws

exception thrown by the block (after wiping data)