-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1433: Create user hashing for Koblenz
- Loading branch information
Showing
13 changed files
with
384 additions
and
94 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
8 changes: 8 additions & 0 deletions
8
backend/src/main/kotlin/app/ehrenamtskarte/backend/common/utils/Environment.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 app.ehrenamtskarte.backend.common.utils | ||
|
||
// This helper class was created to enable mocking getenv in Tests | ||
class Environment { | ||
companion object { | ||
fun getVariable(name: String): String? = System.getenv(name) | ||
} | ||
} |
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
85 changes: 85 additions & 0 deletions
85
backend/src/main/kotlin/app/ehrenamtskarte/backend/verification/Argon2IdHasher.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,85 @@ | ||
import app.ehrenamtskarte.backend.common.utils.Environment | ||
import app.ehrenamtskarte.backend.common.webservice.KOBLENZ_PEPPER_SYS_ENV | ||
import app.ehrenamtskarte.backend.verification.CanonicalJson | ||
import org.bouncycastle.crypto.generators.Argon2BytesGenerator | ||
import org.bouncycastle.crypto.params.Argon2Parameters | ||
import java.nio.charset.StandardCharsets | ||
import java.util.Base64 | ||
|
||
class Argon2IdHasher { | ||
companion object { | ||
/** | ||
* Copied from spring-security Argon2EncodingUtils.java licenced under Apache 2.0 | ||
* | ||
* Encodes a raw Argon2-hash and its parameters into the standard Argon2-hash-string | ||
* as specified in the reference implementation | ||
* (https://github.com/P-H-C/phc-winner-argon2/blob/master/src/encoding.c#L244): | ||
* | ||
* {@code $argon2<T>[$v=<num>]$m=<num>,t=<num>,p=<num>$<bin>$<bin>} | ||
**/ | ||
@Throws(IllegalArgumentException::class) | ||
fun encode( | ||
hash: ByteArray?, | ||
parameters: Argon2Parameters | ||
): String? { | ||
val b64encoder: Base64.Encoder = Base64.getEncoder().withoutPadding() | ||
val stringBuilder = StringBuilder() | ||
val type = | ||
when (parameters.type) { | ||
Argon2Parameters.ARGON2_d -> "\$argon2d" | ||
Argon2Parameters.ARGON2_i -> "\$argon2i" | ||
Argon2Parameters.ARGON2_id -> "\$argon2id" | ||
else -> throw IllegalArgumentException("Invalid algorithm type: " + parameters.type) | ||
} | ||
stringBuilder.append(type) | ||
stringBuilder | ||
.append("\$v=") | ||
.append(parameters.version) | ||
.append("\$m=") | ||
.append(parameters.memory) | ||
.append(",t=") | ||
.append(parameters.iterations) | ||
.append(",p=") | ||
.append(parameters.lanes) | ||
if (parameters.salt != null) { | ||
stringBuilder.append("$").append(b64encoder.encodeToString(parameters.salt)) | ||
} | ||
stringBuilder.append("$").append(b64encoder.encodeToString(hash)) | ||
return stringBuilder.toString() | ||
} | ||
|
||
fun hashUserData(cardInfo: Card.CardInfo): String? { | ||
val canonicalJson = CanonicalJson.messageToMap(cardInfo) | ||
val hashLength = 32 | ||
if (!isCanonicalJsonValid(canonicalJson)) { | ||
throw Exception("Invalid Json input for hashing") | ||
} | ||
|
||
val pepper = Environment.getVariable(KOBLENZ_PEPPER_SYS_ENV) // TODO handle if Null | ||
val pepperByteArray = pepper?.toByteArray(StandardCharsets.UTF_8) | ||
val params = | ||
Argon2Parameters | ||
.Builder(Argon2Parameters.ARGON2_id) | ||
.withVersion(19) | ||
.withIterations(2) | ||
.withSalt(pepperByteArray) | ||
.withParallelism(1) | ||
.withMemoryAsKB(16) | ||
.build() | ||
|
||
val generator = Argon2BytesGenerator() | ||
generator.init(params) | ||
val result = ByteArray(hashLength) | ||
generator.generateBytes(CanonicalJson.serializeToString(canonicalJson).toCharArray(), result) | ||
return encode(result, params) | ||
} | ||
|
||
private fun isCanonicalJsonValid(canonicalJson: Map<String, Any>): Boolean { | ||
val hasName = canonicalJson.get("1") != null | ||
val hasExtensions = canonicalJson.get("3") as? Map<String, Any> | ||
val hasKoblenzPassExtension = hasExtensions?.get("6") as? Map<String, String> | ||
val hasKoblenzPassId = hasKoblenzPassExtension?.get("1") != null | ||
return hasName && hasKoblenzPassId | ||
} | ||
} | ||
} |
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
Oops, something went wrong.