From 53de684b4ae209516a23325f60f14c375447263e Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Wed, 9 Nov 2016 17:02:54 -0800 Subject: [PATCH] crypto: add `cleanse` function. --- lib/crypto/crypto.js | 22 ++++++++++++++++++++++ package.json | 2 +- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/lib/crypto/crypto.js b/lib/crypto/crypto.js index 9918f1ca..01d3c710 100644 --- a/lib/crypto/crypto.js +++ b/lib/crypto/crypto.js @@ -514,6 +514,28 @@ crypto.ceq = function ceq(a, b) { return r === 1; }; +/** + * A maybe-secure memzero. + * @param {Buffer} data + */ + +crypto.cleanse = function cleanse(data) { + var ctr = crypto._counter; + var i; + + for (i = 0; i < data.length; i++) { + data[i] = ctr & 0xff; + ctr += i; + } + + crypto._counter = ctr >>> 0; +}; + +crypto._counter = 0; + +if (native) + crypto.cleanse = native.cleanse; + /** * Build a merkle tree from leaves. * @param {Buffer[]} leaves diff --git a/package.json b/package.json index fa1d905f..78def5c7 100644 --- a/package.json +++ b/package.json @@ -41,7 +41,7 @@ "elliptic": "6.3.2" }, "optionalDependencies": { - "bcoin-native": "0.0.7", + "bcoin-native": "0.0.8", "leveldown": "1.5.0", "secp256k1": "3.2.0", "socket.io": "1.4.8",