diff options
author | Peter Dettman <peter.dettman@bouncycastle.org> | 2022-07-27 00:07:10 +0700 |
---|---|---|
committer | Peter Dettman <peter.dettman@bouncycastle.org> | 2022-07-27 00:07:10 +0700 |
commit | 8f249d0598d78b78fe099cc0c1e30300b0b1eacb (patch) | |
tree | 518c1ec1b8745f4109c0bb54934e1d32a0f938d9 | |
parent | Refactoring Threefish (diff) | |
download | BouncyCastle.NET-ed25519-8f249d0598d78b78fe099cc0c1e30300b0b1eacb.tar.xz |
Refactor RIPEMD digests
-rw-r--r-- | crypto/src/crypto/digests/RipeMD128Digest.cs | 33 | ||||
-rw-r--r-- | crypto/src/crypto/digests/RipeMD160Digest.cs | 37 | ||||
-rw-r--r-- | crypto/src/crypto/digests/RipeMD256Digest.cs | 36 | ||||
-rw-r--r-- | crypto/src/crypto/digests/RipeMD320Digest.cs | 40 |
4 files changed, 42 insertions, 104 deletions
diff --git a/crypto/src/crypto/digests/RipeMD128Digest.cs b/crypto/src/crypto/digests/RipeMD128Digest.cs index e8a0331ca..cba2c65d3 100644 --- a/crypto/src/crypto/digests/RipeMD128Digest.cs +++ b/crypto/src/crypto/digests/RipeMD128Digest.cs @@ -1,5 +1,6 @@ using System; +using Org.BouncyCastle.Crypto.Utilities; using Org.BouncyCastle.Utilities; namespace Org.BouncyCastle.Crypto.Digests @@ -57,12 +58,9 @@ namespace Org.BouncyCastle.Crypto.Digests return DigestLength; } - internal override void ProcessWord( - byte[] input, - int inOff) + internal override void ProcessWord(byte[] input, int inOff) { - X[xOff++] = (input[inOff] & 0xff) | ((input[inOff + 1] & 0xff) << 8) - | ((input[inOff + 2] & 0xff) << 16) | ((input[inOff + 3] & 0xff) << 24); + X[xOff++] = (int)Pack.LE_To_UInt32(input, inOff); if (xOff == 16) { @@ -82,27 +80,14 @@ namespace Org.BouncyCastle.Crypto.Digests X[15] = (int)((ulong) bitLength >> 32); } - private void UnpackWord( - int word, - byte[] outBytes, - int outOff) - { - outBytes[outOff] = (byte)word; - outBytes[outOff + 1] = (byte)((uint) word >> 8); - outBytes[outOff + 2] = (byte)((uint) word >> 16); - outBytes[outOff + 3] = (byte)((uint) word >> 24); - } - - public override int DoFinal( - byte[] output, - int outOff) + public override int DoFinal(byte[] output, int outOff) { Finish(); - UnpackWord(H0, output, outOff); - UnpackWord(H1, output, outOff + 4); - UnpackWord(H2, output, outOff + 8); - UnpackWord(H3, output, outOff + 12); + Pack.UInt32_To_LE((uint)H0, output, outOff); + Pack.UInt32_To_LE((uint)H1, output, outOff + 4); + Pack.UInt32_To_LE((uint)H2, output, outOff + 8); + Pack.UInt32_To_LE((uint)H3, output, outOff + 12); Reset(); @@ -478,7 +463,5 @@ namespace Org.BouncyCastle.Crypto.Digests CopyIn(d); } - } - } diff --git a/crypto/src/crypto/digests/RipeMD160Digest.cs b/crypto/src/crypto/digests/RipeMD160Digest.cs index af4aa44bb..0fc2a4a1c 100644 --- a/crypto/src/crypto/digests/RipeMD160Digest.cs +++ b/crypto/src/crypto/digests/RipeMD160Digest.cs @@ -1,5 +1,6 @@ using System; +using Org.BouncyCastle.Crypto.Utilities; using Org.BouncyCastle.Utilities; namespace Org.BouncyCastle.Crypto.Digests @@ -59,12 +60,9 @@ namespace Org.BouncyCastle.Crypto.Digests return DigestLength; } - internal override void ProcessWord( - byte[] input, - int inOff) + internal override void ProcessWord(byte[] input, int inOff) { - X[xOff++] = (input[inOff] & 0xff) | ((input[inOff + 1] & 0xff) << 8) - | ((input[inOff + 2] & 0xff) << 16) | ((input[inOff + 3] & 0xff) << 24); + X[xOff++] = (int)Pack.LE_To_UInt32(input, inOff); if (xOff == 16) { @@ -72,7 +70,7 @@ namespace Org.BouncyCastle.Crypto.Digests } } - internal override void ProcessLength( + internal override void ProcessLength( long bitLength) { if (xOff > 14) @@ -84,28 +82,15 @@ namespace Org.BouncyCastle.Crypto.Digests X[15] = (int)((ulong) bitLength >> 32); } - private void UnpackWord( - int word, - byte[] outBytes, - int outOff) - { - outBytes[outOff] = (byte)word; - outBytes[outOff + 1] = (byte)((uint) word >> 8); - outBytes[outOff + 2] = (byte)((uint) word >> 16); - outBytes[outOff + 3] = (byte)((uint) word >> 24); - } - - public override int DoFinal( - byte[] output, - int outOff) + public override int DoFinal(byte[] output, int outOff) { Finish(); - UnpackWord(H0, output, outOff); - UnpackWord(H1, output, outOff + 4); - UnpackWord(H2, output, outOff + 8); - UnpackWord(H3, output, outOff + 12); - UnpackWord(H4, output, outOff + 16); + Pack.UInt32_To_LE((uint)H0, output, outOff); + Pack.UInt32_To_LE((uint)H1, output, outOff + 4); + Pack.UInt32_To_LE((uint)H2, output, outOff + 8); + Pack.UInt32_To_LE((uint)H3, output, outOff + 12); + Pack.UInt32_To_LE((uint)H4, output, outOff + 16); Reset(); @@ -439,7 +424,5 @@ namespace Org.BouncyCastle.Crypto.Digests CopyIn(d); } - } - } diff --git a/crypto/src/crypto/digests/RipeMD256Digest.cs b/crypto/src/crypto/digests/RipeMD256Digest.cs index 306275767..621162a6f 100644 --- a/crypto/src/crypto/digests/RipeMD256Digest.cs +++ b/crypto/src/crypto/digests/RipeMD256Digest.cs @@ -1,5 +1,6 @@ using System; +using Org.BouncyCastle.Crypto.Utilities; using Org.BouncyCastle.Utilities; namespace Org.BouncyCastle.Crypto.Digests @@ -59,12 +60,9 @@ namespace Org.BouncyCastle.Crypto.Digests xOff = t.xOff; } - internal override void ProcessWord( - byte[] input, - int inOff) + internal override void ProcessWord(byte[] input, int inOff) { - X[xOff++] = (input[inOff] & 0xff) | ((input[inOff + 1] & 0xff) << 8) - | ((input[inOff + 2] & 0xff) << 16) | ((input[inOff + 3] & 0xff) << 24); + X[xOff++] = (int)Pack.LE_To_UInt32(input, inOff); if (xOff == 16) { @@ -84,29 +82,18 @@ namespace Org.BouncyCastle.Crypto.Digests X[15] = (int)((ulong)bitLength >> 32); } - private void UnpackWord( - int word, - byte[] outBytes, - int outOff) - { - outBytes[outOff] = (byte)(uint)word; - outBytes[outOff + 1] = (byte)((uint)word >> 8); - outBytes[outOff + 2] = (byte)((uint)word >> 16); - outBytes[outOff + 3] = (byte)((uint)word >> 24); - } - public override int DoFinal(byte[] output, int outOff) { Finish(); - UnpackWord(H0, output, outOff); - UnpackWord(H1, output, outOff + 4); - UnpackWord(H2, output, outOff + 8); - UnpackWord(H3, output, outOff + 12); - UnpackWord(H4, output, outOff + 16); - UnpackWord(H5, output, outOff + 20); - UnpackWord(H6, output, outOff + 24); - UnpackWord(H7, output, outOff + 28); + Pack.UInt32_To_LE((uint)H0, output, outOff); + Pack.UInt32_To_LE((uint)H1, output, outOff + 4); + Pack.UInt32_To_LE((uint)H2, output, outOff + 8); + Pack.UInt32_To_LE((uint)H3, output, outOff + 12); + Pack.UInt32_To_LE((uint)H4, output, outOff + 16); + Pack.UInt32_To_LE((uint)H5, output, outOff + 20); + Pack.UInt32_To_LE((uint)H6, output, outOff + 24); + Pack.UInt32_To_LE((uint)H7, output, outOff + 28); Reset(); @@ -425,6 +412,5 @@ namespace Org.BouncyCastle.Crypto.Digests CopyIn(d); } - } } diff --git a/crypto/src/crypto/digests/RipeMD320Digest.cs b/crypto/src/crypto/digests/RipeMD320Digest.cs index 767d74dba..c46bc4fea 100644 --- a/crypto/src/crypto/digests/RipeMD320Digest.cs +++ b/crypto/src/crypto/digests/RipeMD320Digest.cs @@ -1,5 +1,6 @@ using System; +using Org.BouncyCastle.Crypto.Utilities; using Org.BouncyCastle.Utilities; namespace Org.BouncyCastle.Crypto.Digests @@ -62,12 +63,9 @@ namespace Org.BouncyCastle.Crypto.Digests xOff = t.xOff; } - internal override void ProcessWord( - byte[] input, - int inOff) + internal override void ProcessWord(byte[] input, int inOff) { - X[xOff++] = (input[inOff] & 0xff) | ((input[inOff + 1] & 0xff) << 8) - | ((input[inOff + 2] & 0xff) << 16) | ((input[inOff + 3] & 0xff) << 24); + X[xOff++] = (int)Pack.LE_To_UInt32(input, inOff); if (xOff == 16) { @@ -87,31 +85,20 @@ namespace Org.BouncyCastle.Crypto.Digests X[15] = (int)((ulong)bitLength >> 32); } - private void UnpackWord( - int word, - byte[] outBytes, - int outOff) - { - outBytes[outOff] = (byte)word; - outBytes[outOff + 1] = (byte)((uint)word >> 8); - outBytes[outOff + 2] = (byte)((uint)word >> 16); - outBytes[outOff + 3] = (byte)((uint)word >> 24); - } - public override int DoFinal(byte[] output, int outOff) { Finish(); - UnpackWord(H0, output, outOff); - UnpackWord(H1, output, outOff + 4); - UnpackWord(H2, output, outOff + 8); - UnpackWord(H3, output, outOff + 12); - UnpackWord(H4, output, outOff + 16); - UnpackWord(H5, output, outOff + 20); - UnpackWord(H6, output, outOff + 24); - UnpackWord(H7, output, outOff + 28); - UnpackWord(H8, output, outOff + 32); - UnpackWord(H9, output, outOff + 36); + Pack.UInt32_To_LE((uint)H0, output, outOff); + Pack.UInt32_To_LE((uint)H1, output, outOff + 4); + Pack.UInt32_To_LE((uint)H2, output, outOff + 8); + Pack.UInt32_To_LE((uint)H3, output, outOff + 12); + Pack.UInt32_To_LE((uint)H4, output, outOff + 16); + Pack.UInt32_To_LE((uint)H5, output, outOff + 20); + Pack.UInt32_To_LE((uint)H6, output, outOff + 24); + Pack.UInt32_To_LE((uint)H7, output, outOff + 28); + Pack.UInt32_To_LE((uint)H8, output, outOff + 32); + Pack.UInt32_To_LE((uint)H9, output, outOff + 36); Reset(); @@ -454,6 +441,5 @@ namespace Org.BouncyCastle.Crypto.Digests CopyIn(d); } - } } |