OSDN Git Service

merge tx signer
[bytom/bytom-java-sdk.git] / tx-signer / src / main / java / com / google / crypto / tink / subtle / Bytes.java
diff --git a/tx-signer/src/main/java/com/google/crypto/tink/subtle/Bytes.java b/tx-signer/src/main/java/com/google/crypto/tink/subtle/Bytes.java
new file mode 100755 (executable)
index 0000000..6cb43be
--- /dev/null
@@ -0,0 +1,183 @@
+// Copyright 2017 Google Inc.\r
+//\r
+// Licensed under the Apache License, Version 2.0 (the "License");\r
+// you may not use this file except in compliance with the License.\r
+// You may obtain a copy of the License at\r
+//\r
+//      http://www.apache.org/licenses/LICENSE-2.0\r
+//\r
+// Unless required by applicable law or agreed to in writing, software\r
+// distributed under the License is distributed on an "AS IS" BASIS,\r
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+// See the License for the specific language governing permissions and\r
+// limitations under the License.\r
+//\r
+////////////////////////////////////////////////////////////////////////////////\r
+\r
+package com.google.crypto.tink.subtle;\r
+\r
+import java.nio.ByteBuffer;\r
+import java.security.GeneralSecurityException;\r
+import java.util.Arrays;\r
+\r
+/**\r
+ * Helper methods that deal with byte arrays.\r
+ *\r
+ * @since 1.0.0\r
+ */\r
+public final class Bytes {\r
+    /**\r
+     * Best effort fix-timing array comparison.\r
+     *\r
+     * @return true if two arrays are equal.\r
+     */\r
+    public static final boolean equal(final byte[] x, final byte[] y) {\r
+        if (x == null || y == null) {\r
+            return false;\r
+        }\r
+        if (x.length != y.length) {\r
+            return false;\r
+        }\r
+        int res = 0;\r
+        for (int i = 0; i < x.length; i++) {\r
+            res |= x[i] ^ y[i];\r
+        }\r
+        return res == 0;\r
+    }\r
+\r
+    /**\r
+     * Returns the concatenation of the input arrays in a single array. For example, {@code concat(new\r
+     * byte[] {a, b}, new byte[] {}, new byte[] {c}} returns the array {@code {a, b, c}}.\r
+     *\r
+     * @return a single array containing all the values from the source arrays, in order\r
+     */\r
+    public static byte[] concat(byte[]... chunks) throws GeneralSecurityException {\r
+        int length = 0;\r
+        for (byte[] chunk : chunks) {\r
+            if (length > Integer.MAX_VALUE - chunk.length) {\r
+                throw new GeneralSecurityException("exceeded size limit");\r
+            }\r
+            length += chunk.length;\r
+        }\r
+        byte[] res = new byte[length];\r
+        int pos = 0;\r
+        for (byte[] chunk : chunks) {\r
+            System.arraycopy(chunk, 0, res, pos, chunk.length);\r
+            pos += chunk.length;\r
+        }\r
+        return res;\r
+    }\r
+\r
+    /**\r
+     * Computes the xor of two byte arrays, specifying offsets and the length to xor.\r
+     *\r
+     * @return a new byte[] of length len.\r
+     */\r
+    public static final byte[] xor(\r
+            final byte[] x, int offsetX, final byte[] y, int offsetY, int len) {\r
+        if (len < 0 || x.length - len < offsetX || y.length - len < offsetY) {\r
+            throw new IllegalArgumentException(\r
+                    "That combination of buffers, offsets and length to xor result in out-of-bond accesses.");\r
+        }\r
+        byte[] res = new byte[len];\r
+        for (int i = 0; i < len; i++) {\r
+            res[i] = (byte) (x[i + offsetX] ^ y[i + offsetY]);\r
+        }\r
+        return res;\r
+    }\r
+\r
+    /**\r
+     * Computes the xor of two byte buffers, specifying the length to xor, and\r
+     * stores the result to {@code output}.\r
+     *\r
+     * @return a new byte[] of length len.\r
+     */\r
+    public static final void xor(ByteBuffer output, ByteBuffer x, ByteBuffer y, int len) {\r
+        if (len < 0 || x.remaining() < len || y.remaining() < len || output.remaining() < len) {\r
+            throw new IllegalArgumentException(\r
+                    "That combination of buffers, offsets and length to xor result in out-of-bond accesses.");\r
+        }\r
+        for (int i = 0; i < len; i++) {\r
+            output.put((byte) (x.get() ^ y.get()));\r
+        }\r
+    }\r
+\r
+    /**\r
+     * Computes the xor of two byte arrays of equal size.\r
+     *\r
+     * @return a new byte[] of length x.length.\r
+     */\r
+    public static final byte[] xor(final byte[] x, final byte[] y) {\r
+        if (x.length != y.length) {\r
+            throw new IllegalArgumentException("The lengths of x and y should match.");\r
+        }\r
+        return xor(x, 0, y, 0, x.length);\r
+    }\r
+\r
+    /**\r
+     * xors b to the end of a.\r
+     *\r
+     * @return a new byte[] of length x.length.\r
+     */\r
+    public static final byte[] xorEnd(final byte[] a, final byte[] b) {\r
+        if (a.length < b.length) {\r
+            throw new IllegalArgumentException("xorEnd requires a.length >= b.length");\r
+        }\r
+        int paddingLength = a.length - b.length;\r
+        byte[] res = Arrays.copyOf(a, a.length);\r
+        for (int i = 0; i < b.length; i++) {\r
+            res[paddingLength + i] ^= b[i];\r
+        }\r
+        return res;\r
+    }\r
+\r
+    // TODO(thaidn): add checks for boundary conditions/overflows.\r
+\r
+    /**\r
+     * Transforms a passed value to a LSB first byte array with the size of the specified capacity\r
+     *\r
+     * @param capacity size of the resulting byte array\r
+     * @param value    that should be represented as a byte array\r
+     */\r
+    public static byte[] intToByteArray(int capacity, int value) {\r
+        final byte[] result = new byte[capacity];\r
+        for (int i = 0; i < capacity; i++) {\r
+            result[i] = (byte) ((value >> (8 * i)) & 0xFF);\r
+        }\r
+        return result;\r
+    }\r
+\r
+    /**\r
+     * Transforms a passed LSB first byte array to an int\r
+     *\r
+     * @param bytes that should be transformed to a byte array\r
+     */\r
+    public static int byteArrayToInt(byte[] bytes) {\r
+        return byteArrayToInt(bytes, bytes.length);\r
+    }\r
+\r
+    /**\r
+     * Transforms a passed LSB first byte array to an int\r
+     *\r
+     * @param bytes  that should be transformed to a byte array\r
+     * @param length amount of the passed {@code bytes} that should be transformed\r
+     */\r
+    public static int byteArrayToInt(byte[] bytes, int length) {\r
+        return byteArrayToInt(bytes, 0, length);\r
+    }\r
+\r
+    /**\r
+     * Transforms a passed LSB first byte array to an int\r
+     *\r
+     * @param bytes  that should be transformed to a byte array\r
+     * @param offset start index to start the transformation\r
+     * @param length amount of the passed {@code bytes} that should be transformed\r
+     */\r
+    public static int byteArrayToInt(byte[] bytes, int offset, int length) {\r
+        int value = 0;\r
+        for (int i = 0; i < length; i++) {\r
+            value += (bytes[i + offset] & 0xFF) << (i * 8);\r
+        }\r
+        return value;\r
+    }\r
+}\r