forked from sergeych/crypto2
fixed error in hashcode implementations of various objects so ring works as expected
This commit is contained in:
1 parent
f08653715b
commit
a1f984c4e6
10 files changed
+306
-5
No files matched your search
@@ -135,7 +135,7 @@ object Asymmetric {
|
||||
}
|
||||
|
||||
override fun hashCode(): Int {
|
||||
return keyBytes.hashCode()
|
||||
return keyBytes.contentHashCode()
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -234,7 +234,7 @@ object Asymmetric {
|
||||
}
|
||||
|
||||
override fun hashCode(): Int {
|
||||
return keyBytes.hashCode()
|
||||
return keyBytes.contentHashCode()
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
package net.sergeych.crypto2
|
||||
|
||||
import kotlinx.serialization.Serializable
|
||||
import net.sergeych.bipack.Unsigned
|
||||
|
||||
@Serializable
|
||||
class PBKDParams(
|
||||
val kdf: KDF,
|
||||
@Unsigned
|
||||
val offset: UInt,
|
||||
@Unsigned
|
||||
val length: UInt
|
||||
) {
|
||||
// val key by lazy {
|
||||
// SymmetricKey(derivedBytes)
|
||||
// }
|
||||
|
||||
// val derivedBytes by lazy {kdf.derivedBytes}
|
||||
}
|
||||
@@ -52,7 +52,7 @@ class SymmetricKey(
|
||||
}
|
||||
|
||||
override fun hashCode(): Int {
|
||||
return keyBytes.hashCode()
|
||||
return keyBytes.contentHashCode()
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -28,10 +28,13 @@ class UniversalRing(
|
||||
if( keyTag in byTag ) UniversalRing(keySet.filter { it.tag != keyTag }) else this
|
||||
|
||||
override fun equals(other: Any?): Boolean {
|
||||
println("compare1\r\n")
|
||||
if (this === other) return true
|
||||
println("compare2\r\n")
|
||||
if (other !is UniversalRing) return false
|
||||
|
||||
return size == other.size && keySet == other.keySet
|
||||
println("compare2 ${size == other.size}: $size : ${other.size} | ${keySet == other.keySet}\r\n")
|
||||
return size == other.size && keySet.containsAll(other.keySet)
|
||||
}
|
||||
|
||||
override fun toString(): String {
|
||||
|
||||
@@ -0,0 +1,157 @@
|
||||
package net.sergeych.crypto2
|
||||
|
||||
import com.ionspin.kotlin.crypto.pwhash.*
|
||||
import kotlinx.serialization.SerialName
|
||||
import kotlinx.serialization.Serializable
|
||||
import net.sergeych.bipack.Unsigned
|
||||
import net.sergeych.synctools.ProtectedOp
|
||||
|
||||
@Serializable
|
||||
sealed class KDF {
|
||||
|
||||
enum class Complexity {
|
||||
Interactive,
|
||||
Moderate,
|
||||
Sensitive,
|
||||
}
|
||||
|
||||
fun derivedCached(password: String): UByteArray =
|
||||
deriveCached(this, password)
|
||||
|
||||
|
||||
abstract protected fun derive(password: String): UByteArray
|
||||
|
||||
companion object {
|
||||
private val op = ProtectedOp()
|
||||
private val cache = HashMap<KDF, Entry>()
|
||||
|
||||
private class Entry(val kdf: KDF) {
|
||||
val op = ProtectedOp()
|
||||
var data: UByteArray? = null
|
||||
|
||||
// fun generateCached(): UByteArray = op {
|
||||
// if (data == null)
|
||||
// data = kdf.derive()
|
||||
// data!!
|
||||
// }
|
||||
|
||||
}
|
||||
|
||||
|
||||
// fun key(kdf: KDF,password: String): KDF {
|
||||
// return BipackEncoder.encode()
|
||||
// }
|
||||
fun deriveCached(kdf: KDF,password: String): UByteArray {
|
||||
TODO()
|
||||
// val entry = op {
|
||||
// cache.getOrPut(kdf) { Entry(kdf) }
|
||||
// }
|
||||
// return entry.generateCached()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Serializable
|
||||
@SerialName("argon")
|
||||
data class Argon(
|
||||
val algorithm: Alg,
|
||||
@Unsigned
|
||||
val instructionsComplexity: ULong,
|
||||
@Unsigned
|
||||
val memComplexity: Int,
|
||||
val salt: UByteArray,
|
||||
@Unsigned
|
||||
val keySize: Int,
|
||||
) : KDF(), Comparable<Argon> {
|
||||
|
||||
/**
|
||||
* Very abstract strength comparison. If a1 > a2 it generally means that its complexity, and, hopefully,
|
||||
* strength is higher.
|
||||
*/
|
||||
override fun compareTo(other: Argon): Int {
|
||||
var d = algorithm.ordinal.compareTo(other.algorithm.ordinal)
|
||||
if (d != 0) return d
|
||||
d = instructionsComplexity.compareTo(other.instructionsComplexity)
|
||||
if (d != 0) return d
|
||||
d = memComplexity.compareTo(other.memComplexity)
|
||||
if (d != 0) return d
|
||||
d = keySize
|
||||
if (d != 0) return d
|
||||
return 0
|
||||
}
|
||||
|
||||
enum class Alg(val code: Int) {
|
||||
V2i_13(crypto_pwhash_argon2i_ALG_ARGON2I13),
|
||||
V2id_13(crypto_pwhash_argon2id_ALG_ARGON2ID13),
|
||||
;
|
||||
|
||||
companion object {
|
||||
val default = V2id_13
|
||||
}
|
||||
}
|
||||
|
||||
override fun derive(password: String): UByteArray {
|
||||
TODO()
|
||||
//PasswordHash.pwhash(keySize, )
|
||||
}
|
||||
|
||||
override fun equals(other: Any?): Boolean {
|
||||
if (this === other) return true
|
||||
if (other !is Argon) return false
|
||||
|
||||
if (algorithm != other.algorithm) return false
|
||||
if (instructionsComplexity != other.instructionsComplexity) return false
|
||||
if (memComplexity != other.memComplexity) return false
|
||||
if (keySize != other.keySize) return false
|
||||
return salt contentEquals other.salt
|
||||
}
|
||||
|
||||
override fun hashCode(): Int {
|
||||
var result = algorithm.hashCode()
|
||||
result = 31 * result + instructionsComplexity.hashCode()
|
||||
result = 31 * result + memComplexity
|
||||
result = 31 * result + salt.contentHashCode()
|
||||
result = 31 * result + keySize
|
||||
return result
|
||||
}
|
||||
|
||||
|
||||
companion object {
|
||||
val saltSize: Int = crypto_pwhash_SALTBYTES
|
||||
val minKeySize: Int = crypto_pwhash_BYTES_MIN.toInt()
|
||||
|
||||
fun randomSalt() = randomUBytes(saltSize)
|
||||
|
||||
fun create(complexity: Complexity, salt: UByteArray, keySize: Int): Argon {
|
||||
require(salt.size == saltSize) { "The salt size should be $saltSize" }
|
||||
require(keySize > minKeySize) { "The key size should be at least $keySize bytes" }
|
||||
return when (complexity) {
|
||||
Complexity.Interactive -> Argon(
|
||||
Alg.default,
|
||||
crypto_pwhash_OPSLIMIT_INTERACTIVE.toULong(),
|
||||
crypto_pwhash_MEMLIMIT_INTERACTIVE,
|
||||
salt, keySize
|
||||
)
|
||||
|
||||
Complexity.Moderate -> Argon(
|
||||
Alg.default,
|
||||
crypto_pwhash_OPSLIMIT_MODERATE,
|
||||
crypto_pwhash_MEMLIMIT_MODERATE,
|
||||
salt, keySize
|
||||
)
|
||||
|
||||
Complexity.Sensitive -> Argon(
|
||||
Alg.default,
|
||||
crypto_pwhash_OPSLIMIT_SENSITIVE,
|
||||
crypto_pwhash_MEMLIMIT_SENSITIVE,
|
||||
salt, keySize
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
data class Instance(val kdf: KDF,val password: String)
|
||||
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user