diff options
author | Peter Dettman <peter.dettman@bouncycastle.org> | 2013-09-27 13:49:02 +0400 |
---|---|---|
committer | Peter Dettman <peter.dettman@bouncycastle.org> | 2013-09-27 13:49:02 +0400 |
commit | c921fbecb4c8ef8dad70d278c1fed6e08cf66a61 (patch) | |
tree | c8ad0c72f50cf183d6fa985d0c3e890612770db1 /core/src/main/java/org/bouncycastle/util | |
parent | c7cedde18ac11dd718c47964825d5cf9c4d44c1a (diff) |
Simplify BigIntegers.asUnsignedByteArray implementation
Diffstat (limited to 'core/src/main/java/org/bouncycastle/util')
-rw-r--r-- | core/src/main/java/org/bouncycastle/util/BigIntegers.java | 42 |
1 files changed, 12 insertions, 30 deletions
diff --git a/core/src/main/java/org/bouncycastle/util/BigIntegers.java b/core/src/main/java/org/bouncycastle/util/BigIntegers.java index 6a8a620e..f7f7e68b 100644 --- a/core/src/main/java/org/bouncycastle/util/BigIntegers.java +++ b/core/src/main/java/org/bouncycastle/util/BigIntegers.java @@ -40,43 +40,25 @@ public final class BigIntegers * @param value value to be converted. * @return a byte array without a leading zero byte if present in the signed encoding. */ - public static byte[] asUnsignedByteArray( - int length, - BigInteger value) + public static byte[] asUnsignedByteArray(int length, BigInteger value) { byte[] bytes = value.toByteArray(); - - if (bytes[0] == 0) + if (bytes.length == length) { - if (bytes.length - 1 > length) - { - throw new IllegalArgumentException("standard length exceeded for value"); - } - - byte[] tmp = new byte[length]; - - System.arraycopy(bytes, 1, tmp, tmp.length - (bytes.length - 1), bytes.length - 1); - - return tmp; + return bytes; } - else - { - if (bytes.length == length) - { - return bytes; - } - - if (bytes.length > length) - { - throw new IllegalArgumentException("standard length exceeded for value"); - } - - byte[] tmp = new byte[length]; - System.arraycopy(bytes, 0, tmp, tmp.length - bytes.length, bytes.length); + int start = bytes[0] == 0 ? 1 : 0; + int count = bytes.length - start; - return tmp; + if (count > length) + { + throw new IllegalArgumentException("standard length exceeded for value"); } + + byte[] tmp = new byte[length]; + System.arraycopy(bytes, start, tmp, tmp.length - count, count); + return tmp; } /** |