|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 Oleg Yukhnevich. Use of this source code is governed by the Apache 2.0 license. |
| 3 | + */ |
| 4 | + |
| 5 | +package dev.whyoleg.cryptography.providers.base.operations |
| 6 | + |
| 7 | +import dev.whyoleg.cryptography.* |
| 8 | +import dev.whyoleg.cryptography.operations.* |
| 9 | +import dev.whyoleg.cryptography.providers.base.* |
| 10 | + |
| 11 | +@CryptographyProviderApi |
| 12 | +public class AccumulatingSignFunction( |
| 13 | + private val oneShot: (data: ByteArray) -> ByteArray, |
| 14 | +) : SignFunction { |
| 15 | + private var closed = false |
| 16 | + private val chunks = ArrayList<ByteArray>(4) |
| 17 | + |
| 18 | + private fun ensureOpen() { check(!closed) { "Already closed" } } |
| 19 | + |
| 20 | + override fun update(source: ByteArray, startIndex: Int, endIndex: Int) { |
| 21 | + ensureOpen() |
| 22 | + checkBounds(source.size, startIndex, endIndex) |
| 23 | + if (startIndex == 0 && endIndex == source.size) chunks += source |
| 24 | + else chunks += source.copyOfRange(startIndex, endIndex) |
| 25 | + } |
| 26 | + |
| 27 | + override fun signIntoByteArray(destination: ByteArray, destinationOffset: Int): Int { |
| 28 | + val sig = signToByteArray() |
| 29 | + sig.copyInto(destination, destinationOffset) |
| 30 | + return sig.size |
| 31 | + } |
| 32 | + |
| 33 | + override fun signToByteArray(): ByteArray { |
| 34 | + ensureOpen() |
| 35 | + val total = chunks.sumOf { it.size } |
| 36 | + val data = ByteArray(total) |
| 37 | + var off = 0 |
| 38 | + chunks.forEach { arr -> arr.copyInto(data, off); off += arr.size } |
| 39 | + val out = oneShot(data) |
| 40 | + reset() |
| 41 | + return out |
| 42 | + } |
| 43 | + |
| 44 | + override fun reset() { |
| 45 | + ensureOpen() |
| 46 | + chunks.clear() |
| 47 | + } |
| 48 | + |
| 49 | + override fun close() { |
| 50 | + closed = true |
| 51 | + chunks.clear() |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +@CryptographyProviderApi |
| 56 | +public class AccumulatingVerifyFunction( |
| 57 | + private val oneShot: (data: ByteArray, signature: ByteArray, startIndex: Int, endIndex: Int) -> Boolean, |
| 58 | +) : VerifyFunction { |
| 59 | + private var closed = false |
| 60 | + private val chunks = ArrayList<ByteArray>(4) |
| 61 | + |
| 62 | + private fun ensureOpen() { check(!closed) { "Already closed" } } |
| 63 | + |
| 64 | + override fun update(source: ByteArray, startIndex: Int, endIndex: Int) { |
| 65 | + ensureOpen() |
| 66 | + checkBounds(source.size, startIndex, endIndex) |
| 67 | + if (startIndex == 0 && endIndex == source.size) chunks += source |
| 68 | + else chunks += source.copyOfRange(startIndex, endIndex) |
| 69 | + } |
| 70 | + |
| 71 | + override fun tryVerify(signature: ByteArray, startIndex: Int, endIndex: Int): Boolean { |
| 72 | + ensureOpen() |
| 73 | + checkBounds(signature.size, startIndex, endIndex) |
| 74 | + val total = chunks.sumOf { it.size } |
| 75 | + val data = ByteArray(total) |
| 76 | + var off = 0 |
| 77 | + chunks.forEach { arr -> arr.copyInto(data, off); off += arr.size } |
| 78 | + val ok = oneShot(data, signature, startIndex, endIndex) |
| 79 | + reset() |
| 80 | + return ok |
| 81 | + } |
| 82 | + |
| 83 | + override fun verify(signature: ByteArray, startIndex: Int, endIndex: Int) { |
| 84 | + check(tryVerify(signature, startIndex, endIndex)) { "Invalid signature" } |
| 85 | + } |
| 86 | + |
| 87 | + override fun reset() { |
| 88 | + ensureOpen() |
| 89 | + chunks.clear() |
| 90 | + } |
| 91 | + |
| 92 | + override fun close() { |
| 93 | + closed = true |
| 94 | + chunks.clear() |
| 95 | + } |
| 96 | +} |
| 97 | + |
0 commit comments