@@ -25,8 +25,6 @@ Low level core cryptographic components for Kotlin Multiplatform
25
25
NOTE: For Jvm, ` Digest ` extends ` java.security.MessageDigest ` and ` Mac ` extends ` javax.crypto.Mac `
26
26
for interoperability.
27
27
28
- Utilized by [ KotlinCrypto/hash] [ url-hash ] and [ KotlinCrypto/MACs] [ url-macs ]
29
-
30
28
### Library Authors
31
29
32
30
Modules in ` core ` are intentionally ** single purpose** and ** small** such that you
@@ -50,190 +48,9 @@ class FooFeatureA(digest: Digest) {
50
48
51
49
### Usage
52
50
53
- <details >
54
- <summary>Digest</summary>
55
-
56
- ``` kotlin
57
- // Using SHA256 from hash repo as an example
58
- import org.kotlincrypto.hash.sha2.SHA256
59
-
60
- fun main () {
61
- val digest = SHA256 ()
62
- val bytes = Random .Default .nextBytes(615 )
63
-
64
- // Digest implements Algorithm
65
- println (digest.algorithm())
66
-
67
- // Digest implements Updatable
68
- digest.update(5 .toByte())
69
- digest.update(bytes)
70
- digest.update(bytes, 10 , 88 )
71
-
72
- // Digest implements Resettable
73
- digest.reset()
74
-
75
- digest.update(bytes)
76
-
77
- // Digest implements Copyable
78
- val copy = digest.copy()
79
-
80
- val hash = digest.digest()
81
- val hash2 = copy.digest(bytes)
82
- }
83
- ```
84
-
85
- </details >
86
-
87
- <details >
88
- <summary>Mac</summary>
89
-
90
- ``` kotlin
91
- // Using SecureRandom from the secure-random repo as an example
92
- import org.kotlincrypto.SecureRandom
93
- // Using HmacSHA3_256 from the MACs repo as an example
94
- import org.kotlincrypto.macs.hmac.sha3.HmacSHA3_256
95
-
96
- fun main () {
97
- val key = SecureRandom ().nextBytesOf(100 )
98
- val mac = HmacSHA3_256 (key)
99
- val bytes = Random .Default .nextBytes(615 )
100
-
101
- // Mac implements Algorithm
102
- println (mac.algorithm())
103
-
104
- // Mac implements Updatable
105
- mac.update(5 .toByte())
106
- mac.update(bytes)
107
- mac.update(bytes, 10 , 88 )
108
-
109
- // Mac implements Resettable
110
- mac.reset()
111
-
112
- mac.update(bytes)
113
-
114
- // Mac implements Copyable
115
- val copy = mac.copy()
116
-
117
- val hash = mac.doFinal()
118
- val hash2 = copy.doFinal(bytes)
119
-
120
- // Reinitialize Mac instance with a new key
121
- // to use for something else.
122
- val newKey = SecureRandom ().nextBytesOf(100 )
123
- mac.reset(newKey = newKey)
124
-
125
- // Zero out key material before dereferencing
126
- copy.clearKey()
127
- }
128
- ```
129
-
130
- </details >
131
-
132
- <details >
133
- <summary>Xof</summary>
134
-
135
- ` XOF ` s (i.e. [ Extendable-Output Functions] [ url-pub-xof ] ) were introduced with ` SHA3 ` .
136
-
137
- ` XOF ` s are very similar to ` Digest ` and ` Mac ` except that instead of calling ` digest() `
138
- or ` doFinal() ` , which returns a fixed size ` ByteArray ` , their output size can be variable
139
- in length.
140
-
141
- As such, [ KotlinCrypto] [ url-kotlin-crypto ] takes the approach of making them distinctly
142
- different from those types, while implementing the same interfaces (` Algorithm ` , ` Copyable ` ,
143
- ` Resettable ` , ` Updatable ` ).
144
-
145
- Output for an ` Xof ` is done by reading, instead.
146
-
147
- ``` kotlin
148
- // Using SHAKE128 from hash repo as an example
149
- import org.kotlincrypto.hash.sha3.SHAKE128
150
-
151
- fun main () {
152
- val xof: Xof <SHAKE128 > = SHAKE128 .xOf()
153
- val bytes = Random .Default .nextBytes(615 )
154
-
155
- // Xof implements Algorithm
156
- println (xof.algorithm())
157
-
158
- // Xof implements Updatable
159
- xof.update(5 .toByte())
160
- xof.update(bytes)
161
- xof.update(bytes, 10 , 88 )
162
-
163
- // Xof implements Resettable
164
- xof.reset()
165
-
166
- xof.update(bytes)
167
-
168
- // Xof implements Copyable
169
- xof.copy()
170
-
171
- val out1 = ByteArray (100 )
172
- val out2 = ByteArray (12345 )
173
-
174
- // Use produces a Reader which auto-closes when your action finishes.
175
- // Reader is using a snapshot of the Xof state (thus the
176
- // optional argument to resetXof with a default of true).
177
- xof.use(resetXof = false ) { read(out1, 0 , out1.size); read(out2) }
178
-
179
- val out3 = ByteArray (out1.size)
180
- val out4 = ByteArray (out2.size)
181
-
182
- // Can also create a Reader that won't auto-close
183
- val reader = xof.reader(resetXof = false )
184
- reader.read(out3)
185
- reader.read(out4)
186
- reader.close()
187
-
188
- try {
189
- // The Reader has been closed and will throw
190
- // exception when trying to read from again.
191
- reader.use { read(out4) }
192
- } catch (e: IllegalStateException ) {
193
- e.printStackTrace()
194
- }
195
-
196
- // Contents are the same because Reader uses
197
- // a snapshot of Xof, which was not updated
198
- // between production of Readers.
199
- assertContentEquals(out1 + out2, out3 + out4)
200
-
201
- // Still able to update Xof, independent of the production
202
- // and usage of Readers.
203
- xof.update(10 .toByte())
204
- xof.use { read(out3); read(out4) }
205
-
206
- try {
207
- assertContentEquals(out1 + out2, out3 + out4)
208
- throw IllegalStateException ()
209
- } catch (_: AssertionError ) {
210
- // pass
211
- }
212
- }
213
- ```
214
-
215
- ``` kotlin
216
- // Using KMAC128 from MACs repo as an example
217
- import org.kotlincrypto.macs.kmac.KMAC128
218
- // Using SecureRandom from the secure-random repo as an example
219
- import org.kotlincrypto.SecureRandom
220
-
221
- fun main () {
222
- val key = SecureRandom ().nextBytesOf(100 )
223
- val kmacXof: Xof <KMAC128 > = KMAC128 .xOf(key)
224
-
225
- // If Xof is for a Mac that implements ReKeyableXofAlgorithm,
226
- // reinitialize the instance via the `Xof.Companion.reset`
227
- // extension function for reuse.
228
- val newKey = SecureRandom ().nextBytesOf(100 )
229
- kmacXof.reset(newKey = newKey)
230
-
231
- // Or zero out key material before dereferencing
232
- kmacXof.reset(newKey = ByteArray (1 ))
233
- }
234
- ```
235
-
236
- </details >
51
+ - See module [ digest] ( library/digest/README.md )
52
+ - See module [ mac] ( library/mac/README.md )
53
+ - See module [ xof] ( library/xof/README.md )
237
54
238
55
### Get Started
239
56
@@ -280,8 +97,4 @@ dependencies {
280
97
[ url-latest-release ] : https://github.com/KotlinCrypto/core/releases/latest
281
98
[ url-license ] : https://www.apache.org/licenses/LICENSE-2.0.txt
282
99
[ url-kotlin ] : https://kotlinlang.org
283
- [ url-kotlin-crypto ] : https://github.com/KotlinCrypto
284
- [ url-hash ] : https://github.com/KotlinCrypto/hash
285
- [ url-macs ] : https://github.com/KotlinCrypto/MACs
286
100
[ url-version-catalog ] : https://github.com/KotlinCrypto/version-catalog
287
- [ url-pub-xof ] : https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.202.pdf
0 commit comments