Skip to content

Use HMAC SHA-256 with configurable amount of rounds to generate seed #229

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -582,12 +582,27 @@ <h2>New HD Address <small>making bip32 even easier</small></h2>
</span>
</div>

<label>Seed (WIF)</label>
<div class="input-group">
<input id="newHDseed" type="text" class="form-control" value="" readonly>
<span class="input-group-btn">
<button class="deriveHDbtn btn btn-default" type="button"><span title="Derive from key" class="glyphicon glyphicon-chevron-right"></span></button>
</span>
</div>

<h3>Address Options</h3>
<p>You can use the advanced options below to generate different kinds of master addresses.</p>

<div class="checkbox">
<label><input type="checkbox" id="newHDBrainwallet" class="checkbox-inline"> Custom Seed or Brain Wallet</label>
<input type="text" class="form-control hidden" id="HDBrainwallet">
<div class="hidden" id="HDBrainwalletInput">
<input type="text" class="form-control" id="HDBrainwallet">
<span class="text-muted">
Number of HMAC SHA-256 iterations for seed generation, higher value mean's also longer calculation
(use value 0 to calculate just SHA-256, like in previous coinb.in versions):
<input type="text" class="form-control" id="HDBrainwalletIters" value="50000" size="10">
</span>
</div>
</div>

<input type="button" class="btn btn-primary" value="Generate" id="newHDKeysBtn">
Expand Down
23 changes: 21 additions & 2 deletions js/coin.js
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,7 @@
r.parent_fingerprint = bytes.slice(5, 9);
r.child_index = coinjs.uint(bytes.slice(9, 13), 4);
r.chain_code = bytes.slice(13, 45);
r.seed_wif = '';
r.key_bytes = bytes.slice(45, 78);

var c = coinjs.compressed; // get current default
Expand Down Expand Up @@ -742,8 +743,23 @@
}

// make a master hd xprv/xpub
r.master = function(pass) {
var seed = (pass) ? Crypto.SHA256(pass) : coinjs.newPrivkey();
r.master = function(pass, iters) {
if (pass) {
var seed_iters = (iters) ? Math.abs(iters * 1) : 0;
if (seed_iters == 0) {
var seed = Crypto.SHA256(pass);
} else {
var seed = Crypto.util.hexToBytes("0000000000000000000000000000000000000000000000000000000000000000");
for (var i = 0; i < seed_iters; i++) {
seed = Crypto.HMAC(Crypto.SHA256, seed, pass, { asBytes: true });
}
seed = Crypto.util.bytesToHex(seed);
}
} else {
var seed = coinjs.newPrivkey();
}

var seed_wif = coinjs.privkey2wif(seed);
var hasher = new jsSHA(seed, 'HEX');
var I = hasher.getHMAC("Bitcoin seed", "TEXT", "SHA-512", "HEX");

Expand All @@ -755,6 +771,7 @@
'parent_fingerprint':[0,0,0,0],
'child_index':0,
'chain_code':chain,
'seed_wif':seed_wif,
'privkey':I.slice(0, 64),
'pubkey':coinjs.newPubkey(I.slice(0, 64))});
}
Expand Down Expand Up @@ -799,6 +816,8 @@
var ret = pub.concat(checksum);
o.pubkey = coinjs.base58encode(ret);
}

o.seed_wif = data.seed_wif;
return o;
}

Expand Down
9 changes: 6 additions & 3 deletions js/coinbin.js
Original file line number Diff line number Diff line change
Expand Up @@ -570,18 +570,20 @@ $(document).ready(function() {
$("#newHDKeysBtn").click(function(){
coinjs.compressed = true;
var s = ($("#newHDBrainwallet").is(":checked")) ? $("#HDBrainwallet").val() : null;
var siters = ($("#newHDBrainwallet").is(":checked")) ? $("#HDBrainwalletIters").val()*1 : null;
var hd = coinjs.hd();
var pair = hd.master(s);
var pair = hd.master(s, siters);
$("#newHDxpub").val(pair.pubkey);
$("#newHDxprv").val(pair.privkey);
$("#newHDseed").val(pair.seed_wif);

});

$("#newHDBrainwallet").click(function(){
if($(this).is(":checked")){
$("#HDBrainwallet").removeClass("hidden");
$("#HDBrainwalletInput").removeClass("hidden");
} else {
$("#HDBrainwallet").addClass("hidden");
$("#HDBrainwalletInput").addClass("hidden");
}
});

Expand Down Expand Up @@ -1681,6 +1683,7 @@ $(document).ready(function() {
if(hex == hex_cmp_prv || hex == hex_cmp_pub){
var hd = coinjs.hd(s);
$("#verifyHDaddress .hdKey").html(s);
$("#verifyHDaddress .seed_wif").val(hd.seed_wif);
$("#verifyHDaddress .chain_code").val(Crypto.util.bytesToHex(hd.chain_code));
$("#verifyHDaddress .depth").val(hd.depth);
$("#verifyHDaddress .version").val('0x'+(hd.version).toString(16));
Expand Down