-
Notifications
You must be signed in to change notification settings - Fork 9
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
Separate Common and JVM-only code #158
Merged
ccjernigan
merged 9 commits into
Electric-Coin-Company:main
from
luca992:multiplatform-jvm
Feb 10, 2023
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
50845ca
Separate common code from java. Remove use of jvm only code in common
luca992 8b8da5c
remove explicit constructors from SecureRandom
luca992 9086fc5
make secureRandom private
luca992 e561f50
class SecureRandom -> class SecureRandom()
luca992 2b3a1de
run ktlintFormat
luca992 b8098b6
run detektAll
luca992 af7be37
make jvmSecretKeyFactory private
luca992 a82d5d9
make classes in crypto package internal
luca992 04b2ee5
make common package internal
luca992 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 [email protected] | ||
* Modified to for Kotlin - Kevin Gorham [email protected] | ||
*/ | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,7 +21,7 @@ import kotlin.math.ceil | |
* Modified to use SHA-512 - Ken Sedgwick [email protected] | ||
* Modified to for Kotlin - Kevin Gorham [email protected] | ||
*/ | ||
object Pbkdf2Sha512 { | ||
internal actual object Pbkdf2Sha512 { | ||
|
||
/** | ||
* Generate a derived key from the given parameters. | ||
|
@@ -31,7 +31,7 @@ object Pbkdf2Sha512 { | |
* @param c the iteration count | ||
* @param dkLen the key length in bits | ||
*/ | ||
fun derive(p: CharArray, s: ByteArray, c: Int, dkLen: Int): ByteArray { | ||
actual fun derive(p: CharArray, s: ByteArray, c: Int, dkLen: Int): ByteArray { | ||
ByteArrayOutputStream().use { baos -> | ||
val dkLenBytes = dkLen / 8 | ||
val pBytes = p.foldIndexed(ByteArray(p.size)) { i, acc, c -> | ||
|
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 | ||
} | ||
} | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this filename right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah the class matches the filename:
ReadmeExamplesTestJvm
I moved that test out because
use
is only available on jvm