Skip to content

Commit 538192b

Browse files
committed
Blake digest refactoring
1 parent f52eec9 commit 538192b

File tree

2 files changed

+12
-24
lines changed

2 files changed

+12
-24
lines changed

core/src/main/java/org/bouncycastle/crypto/digests/Blake2bDigest.java

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -402,19 +402,13 @@ public int doFinal(byte[] out, int outOffset)
402402
Arrays.fill(buffer, (byte)0);// Holds eventually the key if input is null
403403
Arrays.fill(internalState, 0L);
404404

405-
byte[] bytes = new byte[8];
406-
for (int i = 0; i < chainValue.length && (i * 8 < digestLength); i++)
405+
int full = digestLength >>> 3, partial = digestLength & 7;
406+
Pack.longToLittleEndian(chainValue, 0, full, out, outOffset);
407+
if (partial > 0)
407408
{
408-
Pack.longToLittleEndian(chainValue[i], bytes, 0);
409-
410-
if (i * 8 < digestLength - 8)
411-
{
412-
System.arraycopy(bytes, 0, out, outOffset + i * 8, 8);
413-
}
414-
else
415-
{
416-
System.arraycopy(bytes, 0, out, outOffset + i * 8, digestLength - (i * 8));
417-
}
409+
byte[] bytes = new byte[8];
410+
Pack.longToLittleEndian(chainValue[full], bytes, 0);
411+
System.arraycopy(bytes, 0, out, outOffset + digestLength - partial, partial);
418412
}
419413

420414
Arrays.fill(chainValue, 0L);

core/src/main/java/org/bouncycastle/crypto/digests/Blake2sDigest.java

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -433,19 +433,13 @@ public int doFinal(byte[] out, int outOffset)
433433
Arrays.fill(buffer, (byte)0);// Holds eventually the key if input is null
434434
Arrays.fill(internalState, 0);
435435

436-
byte[] bytes = new byte[4];
437-
for (int i = 0; i < chainValue.length && (i * 4 < digestLength); i++)
436+
int full = digestLength >>> 2, partial = digestLength & 3;
437+
Pack.intToLittleEndian(chainValue, 0, full, out, outOffset);
438+
if (partial > 0)
438439
{
439-
Pack.intToLittleEndian(chainValue[i], bytes, 0);
440-
441-
if (i * 4 < digestLength - 4)
442-
{
443-
System.arraycopy(bytes, 0, out, outOffset + i * 4, 4);
444-
}
445-
else
446-
{
447-
System.arraycopy(bytes, 0, out, outOffset + i * 4, digestLength - (i * 4));
448-
}
440+
byte[] bytes = new byte[4];
441+
Pack.intToLittleEndian(chainValue[full], bytes, 0);
442+
System.arraycopy(bytes, 0, out, outOffset + digestLength - partial, partial);
449443
}
450444

451445
Arrays.fill(chainValue, 0);

0 commit comments

Comments
 (0)