diff options
author | Sergio Giro <sgiro@google.com> | 2016-02-01 18:52:42 +0000 |
---|---|---|
committer | Gerrit Code Review <noreply-gerritcodereview@google.com> | 2016-02-01 18:52:42 +0000 |
commit | 9218edabd1ef9852bc2f13115dcadc81b442dd6c (patch) | |
tree | 8229ff72c8cbb06f49dce3a8382930919fa6fc2b /bcprov/src/main/java/org/bouncycastle/pqc/math/linearalgebra/CharUtils.java | |
parent | 9b30eb05e5be69d51881a0d1b31e503e97acd784 (diff) | |
parent | 397d32894b89b506dc318e0f83446187c9b76ebe (diff) | |
download | android_external_bouncycastle-9218edabd1ef9852bc2f13115dcadc81b442dd6c.tar.gz android_external_bouncycastle-9218edabd1ef9852bc2f13115dcadc81b442dd6c.tar.bz2 android_external_bouncycastle-9218edabd1ef9852bc2f13115dcadc81b442dd6c.zip |
Merge "Merge remote-tracking branch 'aosp/upstream-master' into merge-152-from-upstream"
Diffstat (limited to 'bcprov/src/main/java/org/bouncycastle/pqc/math/linearalgebra/CharUtils.java')
-rw-r--r-- | bcprov/src/main/java/org/bouncycastle/pqc/math/linearalgebra/CharUtils.java | 98 |
1 files changed, 98 insertions, 0 deletions
diff --git a/bcprov/src/main/java/org/bouncycastle/pqc/math/linearalgebra/CharUtils.java b/bcprov/src/main/java/org/bouncycastle/pqc/math/linearalgebra/CharUtils.java new file mode 100644 index 0000000..1800685 --- /dev/null +++ b/bcprov/src/main/java/org/bouncycastle/pqc/math/linearalgebra/CharUtils.java @@ -0,0 +1,98 @@ +package org.bouncycastle.pqc.math.linearalgebra; + +public final class CharUtils +{ + + /** + * Default constructor (private) + */ + private CharUtils() + { + // empty + } + + /** + * Return a clone of the given char array. No null checks are performed. + * + * @param array the array to clone + * @return the clone of the given array + */ + public static char[] clone(char[] array) + { + char[] result = new char[array.length]; + System.arraycopy(array, 0, result, 0, array.length); + return result; + } + + /** + * Convert the given char array into a byte array. + * + * @param chars the char array + * @return the converted array + */ + public static byte[] toByteArray(char[] chars) + { + byte[] result = new byte[chars.length]; + for (int i = chars.length - 1; i >= 0; i--) + { + result[i] = (byte)chars[i]; + } + return result; + } + + /** + * Convert the given char array into a + * byte array for use with PBE encryption. + * + * @param chars the char array + * @return the converted array + */ + public static byte[] toByteArrayForPBE(char[] chars) + { + + byte[] out = new byte[chars.length]; + + for (int i = 0; i < chars.length; i++) + { + out[i] = (byte)chars[i]; + } + + int length = out.length * 2; + byte[] ret = new byte[length + 2]; + + int j = 0; + for (int i = 0; i < out.length; i++) + { + j = i * 2; + ret[j] = 0; + ret[j + 1] = out[i]; + } + + ret[length] = 0; + ret[length + 1] = 0; + + return ret; + } + + /** + * Compare two char arrays. No null checks are performed. + * + * @param left the char byte array + * @param right the second char array + * @return the result of the comparison + */ + public static boolean equals(char[] left, char[] right) + { + if (left.length != right.length) + { + return false; + } + boolean result = true; + for (int i = left.length - 1; i >= 0; i--) + { + result &= left[i] == right[i]; + } + return result; + } + +} |