-
Notifications
You must be signed in to change notification settings - Fork 0
/
nanobip39.cs
115 lines (101 loc) · 3.78 KB
/
nanobip39.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// nanobip39
// Copyright (C) 2021 Michael McMaster <[email protected]>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
namespace nanobip39
{
class Program
{
// Convert a LSB BitArray to MSB
// It's not fast, but it works
static void FlipBitOrder(BitArray bitArray)
{
for (var i = 0; i < bitArray.Length; i += 8)
{
for (var j = 0; j < 4; ++j)
{
var tmp = bitArray[i + j];
bitArray[i + j] = bitArray[i + (7 - j)];
bitArray[i + (7 - j)] = tmp;
}
}
}
static BitArray Checksum(byte[] entropy)
{
using (var sha256 = SHA256.Create())
{
var hash = sha256.ComputeHash(entropy);
var result = new BitArray(hash);
FlipBitOrder(result);
// Only need first few bits
result.Length = entropy.Length * 8 / 32;
return result;
}
}
static IEnumerable<int> Generate(int bits)
{
// Uses the Cryptographic random number generator
using (var rng = RandomNumberGenerator.Create())
{
// Grab some random numbers
var entropy = new byte[bits / 8];
rng.GetBytes(entropy);
// Append a few bits of checksum to the end
var checksum = Checksum(entropy);
var seedBits = new BitArray(entropy);
FlipBitOrder(seedBits);
seedBits.Length = seedBits.Length + checksum.Length;
for (var idx = 0; idx < checksum.Length; ++ idx)
{
seedBits.Set(idx + entropy.Length * 8, checksum.Get(idx));
}
// Use 11-bit indexes into the word table
for (var bitIndex = 0; bitIndex < seedBits.Length; bitIndex += 11)
{
var wordIdx = 0;
for (var i = 0; i < 11; i++)
{
wordIdx = wordIdx + ((seedBits.Get(bitIndex + i) ? 1 : 0) << (10 - i));
}
yield return wordIdx;
}
}
}
static void Main(string[] args)
{
var wordLength = 12;
if (args.Length != 2 ||
!int.TryParse(args[0], out wordLength) ||
(wordLength != 12 && wordLength != 24) ||
!File.Exists(args[1]))
{
Console.Error.WriteLine("Usage: dotnet run words /path/to/wordlist");
Console.Error.WriteLine("eg:");
Console.Error.WriteLine("\tdotnet run 24 english.txt");
System.Environment.Exit(1);
}
var wordList = File.ReadLines(args[1]).ToList();
foreach (var wordIndex in Generate(wordLength == 12 ? 128 : 256))
{
Console.WriteLine(wordList[wordIndex]);
}
}
}
}