File tree Expand file tree Collapse file tree 4 files changed +108
-0
lines changed
main/scala/ky/korins/blake3
test/scala/ky/korins/blake3 Expand file tree Collapse file tree 4 files changed +108
-0
lines changed Original file line number Diff line number Diff line change 33All notable changes to this project will be documented in this file.
44
55## [ unreleased]
6+ - Introduced ` update(Short) ` , ` update(Int) ` and ` update(Long) ` .
67
78## [ 2.2.0] - 2021-01-05
89- Introduced ` doneShort() ` , ` doneInt() ` and ` doneLong() ` .
Original file line number Diff line number Diff line change @@ -29,6 +29,21 @@ trait Hasher {
2929 */
3030 def update (input : Byte ): Hasher
3131
32+ /**
33+ * Updates a hasher by specified short, returns the same hasher
34+ */
35+ def update (input : Short ): Hasher
36+
37+ /**
38+ * Updates a hasher by specified short, returns the same hasher
39+ */
40+ def update (input : Int ): Hasher
41+
42+ /**
43+ * Updates a hasher by specified short, returns the same hasher
44+ */
45+ def update (input : Long ): Hasher
46+
3247 /**
3348 * Updates a hasher by specified string, returns the same hasher
3449 */
Original file line number Diff line number Diff line change @@ -95,6 +95,45 @@ private[blake3] class HasherImpl(
9595 this
9696 }
9797
98+ // Simplified version of update(Array[Byte])
99+ def update (input : Short ): Hasher = synchronized {
100+ var v = input
101+ var i = 0
102+ while (i < 2 ) {
103+ finalizeWhenCompleted()
104+ chunkState.update(v.toByte)
105+ v = (v >> 8 ).toShort
106+ i += 1
107+ }
108+ this
109+ }
110+
111+ // Simplified version of update(Array[Byte])
112+ def update (input : Int ): Hasher = synchronized {
113+ var v = input
114+ var i = 0
115+ while (i < 4 ) {
116+ finalizeWhenCompleted()
117+ chunkState.update(v.toByte)
118+ v >>= 8
119+ i += 1
120+ }
121+ this
122+ }
123+
124+ // Simplified version of update(Array[Byte])
125+ def update (input : Long ): Hasher = synchronized {
126+ var v = input
127+ var i = 0
128+ while (i < 8 ) {
129+ finalizeWhenCompleted()
130+ chunkState.update(v.toByte)
131+ v >>= 8
132+ i += 1
133+ }
134+ this
135+ }
136+
98137 def update (input : InputStream ): Hasher = synchronized {
99138 val bytes = new Array [Byte ](CHUNK_LEN )
100139
Original file line number Diff line number Diff line change @@ -102,6 +102,59 @@ class Blake3Test extends AnyWordSpec with should.Matchers {
102102 Blake3 .hex(key, 42 ) shouldBe Blake3 .hex(key.getBytes, 42 )
103103 }
104104
105+ " Short works as Byte" in {
106+ val shortHash = Blake3
107+ .newHasher()
108+ .update(0x6af6 .toShort)
109+ .doneLong()
110+
111+ val byteHash = Blake3
112+ .newHasher()
113+ .update(0xf6 .toByte)
114+ .update(0x6a .toByte)
115+ .doneLong()
116+
117+ shortHash shouldBe byteHash
118+ }
119+
120+ " Int works as Byte" in {
121+ val shortHash = Blake3
122+ .newHasher()
123+ .update(0x7eaa6af6 )
124+ .doneLong()
125+
126+ val byteHash = Blake3
127+ .newHasher()
128+ .update(0xf6 .toByte)
129+ .update(0x6a .toByte)
130+ .update(0xaa .toByte)
131+ .update(0x7e .toByte)
132+ .doneLong()
133+
134+ shortHash shouldBe byteHash
135+ }
136+
137+ " Long works as Byte" in {
138+ val shortHash = Blake3
139+ .newHasher()
140+ .update(0x462d22e57eaa6af6L)
141+ .doneLong()
142+
143+ val byteHash = Blake3
144+ .newHasher()
145+ .update(0xf6 .toByte)
146+ .update(0x6a .toByte)
147+ .update(0xaa .toByte)
148+ .update(0x7e .toByte)
149+ .update(0xe5 .toByte)
150+ .update(0x22 .toByte)
151+ .update(0x2d .toByte)
152+ .update(0x46 .toByte)
153+ .doneLong()
154+
155+ shortHash shouldBe byteHash
156+ }
157+
105158 " Output encoding works" in {
106159 val input = " some text"
107160
You can’t perform that action at this time.
0 commit comments