Model argon2Matrix

This commit is contained in:
Ugljesa Jovanovic 2020-05-21 14:56:28 +02:00 committed by Ugljesa Jovanovic
parent f9ddd7bc20
commit f5b3eb6b92
No known key found for this signature in database
GPG Key ID: 178E6DFCECCB0E0F
2 changed files with 103 additions and 0 deletions

View File

@ -0,0 +1,48 @@
/*
* Copyright 2019 Ugljesa Jovanovic
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
@file:Suppress("EXPERIMENTAL_API_USAGE")
package com.ionspin.kotlin.crypto.keyderivation.argon2
/**
* Created by Ugljesa Jovanovic
* ugljesa.jovanovic@ionspin.com
* on 21-May-2020
*/
class Argon2Matrix(val columnCount: Int, val rowCount: Int) {
internal val storage: UByteArray = UByteArray(columnCount * rowCount * 1024)
operator fun get(rowPosition: Int, columnPosition: Int, inBlockPosition: Int) : UByte {
return storage[rowPosition * (columnCount - 1) * 1024 + columnPosition * 1024 + inBlockPosition]
}
operator fun get(rowPosition: Int, columnPosition: Int) : UByteArray {
println("Expensive.")
return storage.copyOfRange(
rowPosition * (columnCount - 1) * 1024 + columnPosition * 1024,
rowPosition * (columnCount - 1) * 1024 + columnPosition * 1024 + 1024
)
}
operator fun get(rowPosition: Int) : Array<UByteArray> {
return Array(columnCount) {
this.get(rowPosition, it)
}
}
}

View File

@ -0,0 +1,55 @@
/*
* Copyright 2019 Ugljesa Jovanovic
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
@file:Suppress("EXPERIMENTAL_API_USAGE", "EXPERIMENTAL_UNSIGNED_LITERALS")
package com.ionspin.kotlin.crypto.hash.argon
import com.ionspin.kotlin.crypto.keyderivation.argon2.Argon2Matrix
import com.ionspin.kotlin.crypto.util.hexColumsPrint
import kotlin.test.Test
import kotlin.test.assertTrue
/**
* Created by Ugljesa Jovanovic
* ugljesa.jovanovic@ionspin.com
* on 21-May-2020
*/
class Argon2MatrixTest {
val zeroesBlock = UByteArray(1024) { 0U }
val onesBlock = UByteArray(1024) { 1U }
val twosBlock = UByteArray(1024) { 2U }
val threesBlock = UByteArray(1024) { 3U }
@Test
fun debugTest() {
val argon2Matrix = Argon2Matrix(2, 2)
(zeroesBlock + onesBlock + twosBlock + threesBlock).copyInto(argon2Matrix.storage)
println(argon2Matrix[0, 0, 0])
println(argon2Matrix[1, 0, 0])
println(argon2Matrix[2, 0, 0])
println(argon2Matrix[3, 0, 0])
argon2Matrix.storage.hexColumsPrint(1024)
assertTrue {
argon2Matrix[0, 0, 0] == 0U.toUByte() &&
argon2Matrix[1, 0, 0] == 1U.toUByte() &&
argon2Matrix[2, 0, 0] == 2U.toUByte() &&
argon2Matrix[3, 0, 0] == 3U.toUByte()
}
}
}