Android Byte Array Equal constantTimeAreEqual(byte[] a, byte[] b)

Here you can find the source of constantTimeAreEqual(byte[] a, byte[] b)

Description

A constant time equals comparison - does not terminate early if test will fail.

License

Open Source License

Parameter

Parameter Description
a first array
b second array

Return

true if arrays equal, false otherwise.

Declaration

public static boolean constantTimeAreEqual(byte[] a, byte[] b) 

Method Source Code

//package com.java2s;

public class Main {
    /**/*w  w  w  . jav a 2  s.c o m*/
     * A constant time equals comparison - does not terminate early if
     * test will fail.
     *
     * @param a first array
     * @param b second array
     * @return true if arrays equal, false otherwise.
     */
    public static boolean constantTimeAreEqual(byte[] a, byte[] b) {
        if (a == b) {
            return true;
        }

        if (a == null || b == null) {
            return false;
        }

        if (a.length != b.length) {
            return false;
        }

        int nonEqual = 0;

        for (int i = 0; i != a.length; i++) {
            nonEqual |= (a[i] ^ b[i]);
        }

        return nonEqual == 0;
    }
}

Related

  1. equals(byte[] array1, byte[] array2)
  2. equals(byte[] array1, byte[] array2, int length)
  3. equals(byte[] array1, byte[] array2, int length)
  4. equals(byte[] array1, byte[] array2, int length)