-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Also make classes in crypto package internal
- Loading branch information
Showing
21 changed files
with
181 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
bip39-lib/src/commonMain/kotlin/cash/z/ecc/android/common/Closeable.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package cash.z.ecc.android.common | ||
|
||
internal expect interface Closeable { | ||
fun close() | ||
} |
3 changes: 3 additions & 0 deletions
3
bip39-lib/src/commonMain/kotlin/cash/z/ecc/android/crypto/FallbackProvider.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package cash.z.ecc.android.crypto | ||
|
||
internal expect class FallbackProvider() |
11 changes: 11 additions & 0 deletions
11
bip39-lib/src/commonMain/kotlin/cash/z/ecc/android/crypto/PBEKeySpecCommon.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package cash.z.ecc.android.crypto | ||
|
||
internal expect class PBEKeySpecCommon(password: CharArray?, salt: ByteArray?, iterationCount: Int, keyLength: Int) { | ||
|
||
var password: CharArray? | ||
var salt: ByteArray? | ||
var iterationCount: Int | ||
var keyLength: Int | ||
|
||
fun clearPassword() | ||
} |
29 changes: 29 additions & 0 deletions
29
bip39-lib/src/commonMain/kotlin/cash/z/ecc/android/crypto/Pbkdf2Sha512.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package cash.z.ecc.android.crypto | ||
|
||
/** | ||
* | ||
* This is a clean-room implementation of PBKDF2 using RFC 2898 as a reference. | ||
* | ||
* | ||
* RFC 2898: http://tools.ietf.org/html/rfc2898#section-5.2 | ||
* | ||
* | ||
* This code passes all RFC 6070 test vectors: http://tools.ietf.org/html/rfc6070 | ||
* | ||
* | ||
* http://cryptofreek.org/2012/11/29/pbkdf2-pure-java-implementation/<br></br> | ||
* Modified to use SHA-512 - Ken Sedgwick ken@bonsai.com | ||
* Modified to for Kotlin - Kevin Gorham anothergmale@gmail.com | ||
*/ | ||
internal expect object Pbkdf2Sha512 { | ||
|
||
/** | ||
* Generate a derived key from the given parameters. | ||
* | ||
* @param p the password | ||
* @param s the salt | ||
* @param c the iteration count | ||
* @param dkLen the key length in bits | ||
*/ | ||
fun derive(p: CharArray, s: ByteArray, c: Int, dkLen: Int): ByteArray | ||
} |
5 changes: 5 additions & 0 deletions
5
bip39-lib/src/commonMain/kotlin/cash/z/ecc/android/crypto/SecretKeyCommon.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package cash.z.ecc.android.crypto | ||
|
||
internal expect class SecretKeyCommon { | ||
val encoded: ByteArray | ||
} |
10 changes: 10 additions & 0 deletions
10
bip39-lib/src/commonMain/kotlin/cash/z/ecc/android/crypto/SecretKeyFactoryCommon.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package cash.z.ecc.android.crypto | ||
|
||
internal expect class SecretKeyFactoryCommon { | ||
fun generateSecret(pbeKeySpec: PBEKeySpecCommon): SecretKeyCommon | ||
|
||
companion object { | ||
fun getInstance(algorithm: String): SecretKeyFactoryCommon | ||
fun getInstance(algorithm: String, provider: FallbackProvider): SecretKeyFactoryCommon | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
bip39-lib/src/commonMain/kotlin/cash/z/ecc/android/random/SecureRandom.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package cash.z.ecc.android.random | ||
|
||
expect class SecureRandom() { | ||
fun nextBytes(bytes: ByteArray) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
bip39-lib/src/jvmMain/kotlin/cash/z/ecc/android/bip39/ToSha256.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package cash.z.ecc.android.bip39 | ||
|
||
import java.security.MessageDigest | ||
|
||
internal actual fun ByteArray.toSha256(): ByteArray = MessageDigest.getInstance("SHA-256").digest(this) |
3 changes: 3 additions & 0 deletions
3
bip39-lib/src/jvmMain/kotlin/cash/z/ecc/android/common/Closeable.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package cash.z.ecc.android.common | ||
|
||
internal actual interface Closeable : java.io.Closeable |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
bip39-lib/src/jvmMain/kotlin/cash/z/ecc/android/crypto/PBEKeySpecCommon.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package cash.z.ecc.android.crypto | ||
|
||
import javax.crypto.spec.PBEKeySpec | ||
|
||
internal actual class PBEKeySpecCommon actual constructor(password: CharArray?, salt: ByteArray?, iterationCount: Int, keyLength: Int) { | ||
val wrappedPbeKeySpec = PBEKeySpec(password, salt, iterationCount, keyLength) | ||
|
||
actual var password: CharArray? = null | ||
get() = wrappedPbeKeySpec.password | ||
private set | ||
actual var salt: ByteArray? = wrappedPbeKeySpec.salt | ||
get() = wrappedPbeKeySpec.salt | ||
private set | ||
actual var iterationCount: Int = wrappedPbeKeySpec.iterationCount | ||
get() = wrappedPbeKeySpec.iterationCount | ||
private set | ||
actual var keyLength: Int = wrappedPbeKeySpec.keyLength | ||
get() = wrappedPbeKeySpec.keyLength | ||
private set | ||
|
||
actual fun clearPassword() { | ||
wrappedPbeKeySpec.clearPassword() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
bip39-lib/src/jvmMain/kotlin/cash/z/ecc/android/crypto/SecretKeyCommon.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package cash.z.ecc.android.crypto | ||
|
||
import javax.crypto.SecretKey | ||
|
||
internal actual class SecretKeyCommon(generatedSecret: SecretKey) { | ||
|
||
actual val encoded = generatedSecret.encoded | ||
} |
15 changes: 15 additions & 0 deletions
15
bip39-lib/src/jvmMain/kotlin/cash/z/ecc/android/crypto/SecretKeyFactoryCommon.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package cash.z.ecc.android.crypto | ||
|
||
internal actual class SecretKeyFactoryCommon(private val jvmSecretKeyFactory: javax.crypto.SecretKeyFactory) { | ||
|
||
actual fun generateSecret(pbeKeySpec: PBEKeySpecCommon): SecretKeyCommon = | ||
SecretKeyCommon(jvmSecretKeyFactory.generateSecret(pbeKeySpec.wrappedPbeKeySpec)) | ||
|
||
actual companion object { | ||
actual fun getInstance(algorithm: String): SecretKeyFactoryCommon = | ||
SecretKeyFactoryCommon(javax.crypto.SecretKeyFactory.getInstance(algorithm)) | ||
|
||
actual fun getInstance(algorithm: String, provider: FallbackProvider): SecretKeyFactoryCommon = | ||
SecretKeyFactoryCommon(javax.crypto.SecretKeyFactory.getInstance(algorithm)) | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
bip39-lib/src/jvmMain/kotlin/cash/z/ecc/android/random/SecureRandom.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package cash.z.ecc.android.random | ||
|
||
actual class SecureRandom { | ||
private val jvmSecureRandom = java.security.SecureRandom() | ||
|
||
actual fun nextBytes(bytes: ByteArray) { | ||
jvmSecureRandom.nextBytes(bytes) | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
bip39-lib/src/jvmTest/kotlin/cash/z/ecc/android/bip39/ReadmeExamplesTestJvm.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package cash.z.ecc.android.bip39 | ||
|
||
import cash.z.ecc.android.bip39.Mnemonics.MnemonicCode | ||
import cash.z.ecc.android.bip39.Mnemonics.WordCount | ||
import io.kotest.core.spec.style.ShouldSpec | ||
import io.kotest.matchers.shouldBe | ||
|
||
class ReadmeExamplesTestJvm : ShouldSpec({ | ||
context("Example: auto-clear") { | ||
should("clear the mnemonic when done") { | ||
val mnemonicCode = MnemonicCode(WordCount.COUNT_24) | ||
mnemonicCode.use { | ||
mnemonicCode.wordCount shouldBe 24 | ||
} | ||
|
||
// content gets automatically cleared after use! | ||
mnemonicCode.wordCount shouldBe 0 | ||
} | ||
} | ||
}) |