Scoop API

scoop:scoop.crypto:0.1.0

src/digest.scoop

Selected production source · src/digest.scoop

1package scoop.crypto
3import scoop.crypto.native.*
5/**
6 * Digest choices provided by the native backend. MD5 and SHA-1 are available for protocol
7 * compatibility, not collision-resistant signatures.
8 */
9public enum DigestAlgorithm {
10 /**
11 * Sha256 with a 256-bit output.
12 */
13 Sha256,
14 /**
15 * Sha384 with a 384-bit output.
16 */
17 Sha384,
18 /**
19 * Sha512 with a 512-bit output.
20 */
21 Sha512,
22 /**
23 * Sha1 with a 160-bit output. This legacy digest is retained for compatibility.
24 */
25 Sha1,
26 /**
27 * Md5 with a 128-bit output. This legacy digest is retained for compatibility.
28 */
29 Md5,
30 /**
31 * Sha512_256 with a 256-bit output.
32 */
33 Sha512_256 }
35internal fun digestCode(algorithm: DigestAlgorithm): Int = when (algorithm) {
36 case Sha256 -> 0
37 case Sha384 -> 1
38 case Sha512 -> 2
39 case Sha1 -> 3
40 case Md5 -> 4
41 case Sha512_256 -> 5
44/**
45 * An incremental native digest context. Feed data with update, finish once, and close the context.
46 * Finished or failed contexts reject further updates.
47 */
48public class Digest public constructor(algorithm: DigestAlgorithm) : Closeable {
49 private var handle: ULong = createDigest(algorithm)
50 private var finished: Boolean = false
52 private fun requireActive() {
53 if (handle == 0UL || finished) throw IllegalStateException(Some("digest is closed or finished"))
54 }
56 /**
57 * Adds bytes to the current digest. A backend failure makes this context unusable for further
58 * updates.
59 */
60 public fun update(data: List<Byte>) {
61 requireActive()
62 val bytes = data.toArray()
63 try {
64 @Unsafe { bytes.withDataPointer { pointer, count ->
65 @Unsafe { cryptoValue(digestUpdate(handle, pointer, count), "update digest") }
66 } }
67 } catch (error: Throwable) { finished = true; throw error }
68 }
70 /**
71 * Finalizes once and returns the digest bytes; the context still needs closing.
72 */
73 public fun finish(): Array<Byte> {
74 requireActive()
75 finished = true
76 @Unsafe { return cryptoBytes(digestFinish(handle), "finish digest") }
77 }
79 /**
80 * Releases the native context. Repeated closes are harmless.
81 */
82 public override fun close() {
83 val owned = handle
84 handle = 0UL
85 if (owned != 0UL) @Unsafe { digestFree(owned) }
86 }
89/**
90 * Computes a complete digest and closes the temporary context.
91 */
92public fun digest(algorithm: DigestAlgorithm, data: List<Byte>): Array<Byte> =
93 Digest(algorithm).use { context -> context.update(data); context.finish() }
95/**
96 * Returns the requested nonnegative number of bytes from the backend's cryptographic random
97 * generator; backend failures propagate.
98 */
99public fun secureRandomBytes(count: Long): Array<Byte> {
100 if (count < 0L) throw IllegalArgumentException(Some("negative random byte count"))
101 @Unsafe { return cryptoBytes(nativeRandom(count), "secure random") }
104private fun createDigest(algorithm: DigestAlgorithm): ULong {
105 @Unsafe { return cryptoValue(digestNew(digestCode(algorithm)), "create digest") }