Initial commit
This initial commit includes HUSH specific changes starting at this commit:
d14637012c
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
package cash.z.ecc.kotlin.mnemonic
|
||||
|
||||
import java.util.*
|
||||
|
||||
|
||||
/**
|
||||
* Clears out the given char array in memory, for security purposes.
|
||||
*/
|
||||
fun CharArray.clear() {
|
||||
Arrays.fill(this, '0')
|
||||
}
|
||||
|
||||
/**
|
||||
* Clears out the given byte array in memory, for security purposes.
|
||||
*/
|
||||
fun ByteArray.clear() {
|
||||
Arrays.fill(this, 0.toByte())
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package cash.z.ecc.kotlin.mnemonic
|
||||
|
||||
import cash.z.android.plugin.MnemonicPlugin
|
||||
import cash.z.ecc.android.bip39.Mnemonics
|
||||
import cash.z.ecc.android.bip39.Mnemonics.MnemonicCode
|
||||
import cash.z.ecc.android.bip39.Mnemonics.WordCount
|
||||
import cash.z.ecc.android.bip39.toEntropy
|
||||
import cash.z.ecc.android.bip39.toSeed
|
||||
import java.util.*
|
||||
import java.util.Locale.ENGLISH
|
||||
|
||||
class Mnemonics : MnemonicPlugin {
|
||||
override fun fullWordList(languageCode: String) = Mnemonics.getCachedWords(Locale.ENGLISH.language)
|
||||
override fun nextEntropy(): ByteArray = WordCount.COUNT_24.toEntropy()
|
||||
override fun nextMnemonic(): CharArray = MnemonicCode(WordCount.COUNT_24).chars
|
||||
override fun nextMnemonic(entropy: ByteArray): CharArray = MnemonicCode(entropy).chars
|
||||
override fun nextMnemonicList(): List<CharArray> = MnemonicCode(WordCount.COUNT_24).words
|
||||
override fun nextMnemonicList(entropy: ByteArray): List<CharArray> = MnemonicCode(entropy).words
|
||||
override fun toSeed(mnemonic: CharArray): ByteArray = MnemonicCode(mnemonic).toSeed()
|
||||
override fun toWordList(mnemonic: CharArray): List<CharArray> = MnemonicCode(mnemonic).words
|
||||
|
||||
fun validate(mnemonic: CharArray) {
|
||||
MnemonicCode(mnemonic).validate()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
package cash.z.ecc.android.util
|
||||
|
||||
import cash.z.android.plugin.MnemonicPlugin
|
||||
import cash.z.ecc.kotlin.mnemonic.Mnemonics
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import java.lang.Math.max
|
||||
import java.util.*
|
||||
|
||||
|
||||
class MnemonicTest {
|
||||
|
||||
lateinit var mnemonics: MnemonicPlugin
|
||||
|
||||
@Before
|
||||
fun start() {
|
||||
mnemonics = Mnemonics()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testSeed_fromMnemonic() {
|
||||
val seed = mnemonics.run {
|
||||
toSeed(nextMnemonic())
|
||||
}
|
||||
assertEquals(64, seed.size)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testMnemonic_create() {
|
||||
val words = String(mnemonics.nextMnemonic()).split(' ')
|
||||
assertEquals(24, words.size)
|
||||
validate(words)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testMnemonic_createList() {
|
||||
val words = mnemonics.nextMnemonicList()
|
||||
assertEquals(24, words.size)
|
||||
validate(words.map { String(it) })
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testMnemonic_toList() {
|
||||
val words = mnemonics.run {
|
||||
toWordList(nextMnemonic())
|
||||
}
|
||||
assertEquals(24, words.size)
|
||||
validate(words.map { String(it) })
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testMnemonic_longestWord() {
|
||||
var max = 0
|
||||
val englishWordList = mnemonics.fullWordList(Locale.ENGLISH.language)
|
||||
repeat(2048) {
|
||||
max = max(max, englishWordList[it].length)
|
||||
}
|
||||
assertEquals(8, max)
|
||||
}
|
||||
|
||||
private fun validate(words: List<String>) {
|
||||
val englishWordList = mnemonics.fullWordList(Locale.ENGLISH.language)
|
||||
// return or crash!
|
||||
words.forEach { word ->
|
||||
var i = 0
|
||||
while (true) {
|
||||
if (englishWordList[i++] == word) {
|
||||
println(word)
|
||||
break
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun CharSequence.toWords(): List<CharSequence> {
|
||||
return mutableListOf<CharSequence>().let { result ->
|
||||
var index = 0
|
||||
repeat(length) {
|
||||
if (this[it] == ' ') {
|
||||
result.add(subSequence(index, it))
|
||||
index = it + 1
|
||||
}
|
||||
}
|
||||
result.add(subSequence(index, length))
|
||||
result
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user