-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Full cross-platform crypto implementation
- Loading branch information
1 parent
b4e3375
commit 1dbf1a3
Showing
38 changed files
with
2,992 additions
and
2,407 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
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
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
This file was deleted.
Oops, something went wrong.
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,28 @@ | ||
package org.ton.crypto.digest | ||
|
||
public interface Digest { | ||
public val algorithmName: String | ||
public val digestSize: Int | ||
|
||
public fun update(input: ByteArray): Unit = update(input, 0, input.size) | ||
public fun update(input: ByteArray, offset: Int, length: Int) | ||
|
||
public operator fun plusAssign(input: ByteArray): Unit = update(input) | ||
|
||
public fun build(): ByteArray = build(ByteArray(digestSize)) | ||
public fun build(output: ByteArray): ByteArray = build(output, 0) | ||
public fun build(output: ByteArray, offset: Int): ByteArray | ||
|
||
public fun reset() | ||
|
||
public companion object { | ||
public fun sha256(): Digest = Digest("SHA-256") | ||
public fun sha512(): Digest = Digest("SHA-512") | ||
} | ||
} | ||
|
||
public expect fun Digest(algorithm: String): Digest | ||
|
||
public expect fun sha256(bytes: ByteArray): ByteArray | ||
|
||
public expect fun sha512(bytes: ByteArray): 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package org.ton.crypto.digest.md4 | ||
|
||
import org.ton.crypto.digest.Digest | ||
|
||
/** | ||
* base implementation of MD4 family style digest as outlined in "Handbook of Applied Cryptography", | ||
* pages 344 - 347. | ||
*/ | ||
public abstract class GeneralDigest : Digest { | ||
private val xBuf = ByteArray(4) | ||
private var xBuffOff = 0 | ||
private var byteCount = 0 | ||
|
||
public fun update(input: Byte) { | ||
xBuf[xBuffOff++] = input | ||
if (xBuffOff == xBuf.size) { | ||
processWord(xBuf, 0) | ||
xBuffOff = 0 | ||
} | ||
byteCount++ | ||
} | ||
|
||
public override fun update(input: ByteArray, offset: Int, length: Int) { | ||
// fill the current word | ||
var i = 0 | ||
if (xBuffOff != 0) { | ||
while (i < length) { | ||
xBuf[xBuffOff++] = input[offset + i++] | ||
if (xBuffOff == 4) { | ||
processWord(xBuf, 0) | ||
xBuffOff = 0 | ||
break | ||
} | ||
} | ||
} | ||
|
||
// process whole words. | ||
val limit = ((length - i) and 3.inv()) + i | ||
while (i < limit) { | ||
processWord(input, offset + i) | ||
i += 4 | ||
} | ||
|
||
// load in the remainder. | ||
while (i < length) { | ||
xBuf[xBuffOff++] = input[offset + i++] | ||
} | ||
|
||
byteCount += length | ||
} | ||
|
||
public fun finish() { | ||
val bitLength = byteCount.toLong() shl 3 | ||
|
||
// add the pad bytes. | ||
update(128.toByte()) | ||
|
||
while (xBuffOff != 0) { | ||
update(0.toByte()) | ||
} | ||
processLength(bitLength) | ||
processBlock() | ||
} | ||
|
||
override fun reset() { | ||
byteCount = 0 | ||
xBuffOff = 0 | ||
xBuf.fill(0) | ||
} | ||
|
||
protected abstract fun processWord(input: ByteArray, offset: Int) | ||
protected abstract fun processLength(bitLength: Long) | ||
protected abstract fun processBlock() | ||
} |
Oops, something went wrong.