Skip to content

Commit 1ae5ddc

Browse files
committed
Introduced doneShort(), doneInt() and doneLong()
1 parent 4cced47 commit 1ae5ddc

File tree

4 files changed

+108
-0
lines changed

4 files changed

+108
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
All 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()`.

shared/src/main/scala/ky/korins/blake3/Hasher.scala

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff 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
*/

shared/src/main/scala/ky/korins/blake3/HasherImpl.scala

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff 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

shared/src/test/scala/ky/korins/blake3/Blake3Test.scala

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff 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

0 commit comments

Comments
 (0)