Example usage for java.util Arrays equals

List of usage examples for java.util Arrays equals

Introduction

In this page you can find the example usage for java.util Arrays equals.

Prototype

public static boolean equals(Object[] a, Object[] a2) 

Source Link

Document

Returns true if the two specified arrays of Objects are equal to one another.

Usage

From source file:com.yahoo.ads.pb.mttf.PistachiosMTTFTest.java

public static void main(String[] args) {
    PistachiosClient client;/*from  w  ww . ja va 2 s.  c o  m*/
    try {
        client = new PistachiosClient();
    } catch (Exception e) {
        logger.info("error creating client", e);
        return;
    }
    Random rand = new Random();

    while (true) {
        try {
            long id = rand.nextLong();
            String value = InetAddress.getLocalHost().getHostName() + rand.nextInt();
            client.store(0, id, value.getBytes());
            for (int i = 0; i < 30; i++) {
                byte[] clientValue = client.lookup(0, id);
                String remoteValue = new String(clientValue);
                if (Arrays.equals(value.getBytes(), clientValue)
                        || !remoteValue.contains(InetAddress.getLocalHost().getHostName())) {
                    logger.debug("succeeded checking id {} value {}", id, value);
                } else {
                    logger.error("failed checking id {} value {} != {}", id, value, new String(clientValue));
                    System.exit(0);
                }
                Thread.sleep(100);
            }
        } catch (Exception e) {
            System.out.println("error testing" + e);
            System.exit(0);
        }
    }
}

From source file:Password.java

public static void main(String args[]) throws IOException {

    Console c = System.console();
    if (c == null) {
        System.err.println("No console.");
        System.exit(1);/*w w w .  ja  va 2  s .  co m*/
    }

    String login = c.readLine("Enter your login: ");
    char[] oldPassword = c.readPassword("Enter your old password: ");

    if (verify(login, oldPassword)) {
        boolean noMatch;
        do {
            char[] newPassword1 = c.readPassword("Enter your new password: ");
            char[] newPassword2 = c.readPassword("Enter new password again: ");
            noMatch = !Arrays.equals(newPassword1, newPassword2);
            if (noMatch) {
                c.format("Passwords don't match. Try again.%n");
            } else {
                change(login, newPassword1);
                c.format("Password for %s changed.%n", login);
            }
            Arrays.fill(newPassword1, ' ');
            Arrays.fill(newPassword2, ' ');
        } while (noMatch);
    }

    Arrays.fill(oldPassword, ' ');

}

From source file:com.yxy.chukonu.java.aws.sdk.s3.kms.managed.cmk.testKMSkeyUploadObject.java

public static void main(String[] args) throws Exception {
    String bucketName = "***bucket name***";
    String objectKey = "ExampleKMSEncryptedObject"; //The key in the specified bucket under which the object is stored.
    String kms_cmk_id = "***AWS KMS customer master key ID***";

    KMSEncryptionMaterialsProvider materialProvider = new KMSEncryptionMaterialsProvider(kms_cmk_id);

    encryptionClient = new AmazonS3EncryptionClient(new ProfileCredentialsProvider(), materialProvider,
            new CryptoConfiguration());

    // Upload object using the encryption client.
    byte[] plaintext = "Hello World, S3 Client-side Encryption Using Asymmetric Master Key!".getBytes();
    System.out.println("plaintext's length: " + plaintext.length);
    encryptionClient.putObject(new PutObjectRequest(bucketName, objectKey, new ByteArrayInputStream(plaintext),
            new ObjectMetadata()));

    // Download the object.
    S3Object downloadedObject = encryptionClient.getObject(bucketName, objectKey);
    byte[] decrypted = IOUtils.toByteArray(downloadedObject.getObjectContent());

    // Verify same data.
    Assert.assertTrue(Arrays.equals(plaintext, decrypted));
}

From source file:S3ClientSideEncryptionAsymmetricMasterKey.java

public static void main(String[] args) throws Exception {

    // 1. Load keys from files
    byte[] bytes = FileUtils.readFileToByteArray(new File(keyDir + "/private.key"));
    KeyFactory kf = KeyFactory.getInstance("RSA");
    PKCS8EncodedKeySpec ks = new PKCS8EncodedKeySpec(bytes);
    PrivateKey pk = kf.generatePrivate(ks);

    bytes = FileUtils.readFileToByteArray(new File(keyDir + "/public.key"));
    PublicKey publicKey = KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(bytes));

    KeyPair loadedKeyPair = new KeyPair(publicKey, pk);

    // 2. Construct an instance of AmazonS3EncryptionClient.
    EncryptionMaterials encryptionMaterials = new EncryptionMaterials(loadedKeyPair);
    AWSCredentials credentials = new BasicAWSCredentials("Q3AM3UQ867SPQQA43P2F",
            "zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG");
    AmazonS3EncryptionClient encryptionClient = new AmazonS3EncryptionClient(credentials,
            new StaticEncryptionMaterialsProvider(encryptionMaterials));
    Region usEast1 = Region.getRegion(Regions.US_EAST_1);
    encryptionClient.setRegion(usEast1);
    encryptionClient.setEndpoint("https://play.minio.io:9000");

    final S3ClientOptions clientOptions = S3ClientOptions.builder().setPathStyleAccess(true).build();
    encryptionClient.setS3ClientOptions(clientOptions);

    // Create the bucket
    encryptionClient.createBucket(bucketName);
    // 3. Upload the object.
    byte[] plaintext = "Hello World, S3 Client-side Encryption Using Asymmetric Master Key!".getBytes();
    System.out.println("plaintext's length: " + plaintext.length);
    encryptionClient.putObject(new PutObjectRequest(bucketName, objectKey, new ByteArrayInputStream(plaintext),
            new ObjectMetadata()));

    // 4. Download the object.
    S3Object downloadedObject = encryptionClient.getObject(bucketName, objectKey);
    byte[] decrypted = IOUtils.toByteArray(downloadedObject.getObjectContent());
    Assert.assertTrue(Arrays.equals(plaintext, decrypted));
    System.out.println("decrypted length: " + decrypted.length);
    //deleteBucketAndAllContents(encryptionClient);
}

From source file:S3ClientSideEncryptionWithSymmetricMasterKey.java

public static void main(String[] args) throws Exception {
    SecretKey mySymmetricKey = loadSymmetricAESKey(masterKeyDir, "AES");

    EncryptionMaterials encryptionMaterials = new EncryptionMaterials(mySymmetricKey);

    AWSCredentials credentials = new BasicAWSCredentials("Q3AM3UQ867SPQQA43P2F",
            "zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG");
    AmazonS3EncryptionClient encryptionClient = new AmazonS3EncryptionClient(credentials,
            new StaticEncryptionMaterialsProvider(encryptionMaterials));
    Region usEast1 = Region.getRegion(Regions.US_EAST_1);
    encryptionClient.setRegion(usEast1);
    encryptionClient.setEndpoint("https://play.minio.io:9000");

    final S3ClientOptions clientOptions = S3ClientOptions.builder().setPathStyleAccess(true).build();
    encryptionClient.setS3ClientOptions(clientOptions);

    // Create the bucket
    encryptionClient.createBucket(bucketName);

    // Upload object using the encryption client.
    byte[] plaintext = "Hello World, S3 Client-side Encryption Using Asymmetric Master Key!".getBytes();
    System.out.println("plaintext's length: " + plaintext.length);
    encryptionClient.putObject(new PutObjectRequest(bucketName, objectKey, new ByteArrayInputStream(plaintext),
            new ObjectMetadata()));

    // Download the object.
    S3Object downloadedObject = encryptionClient.getObject(bucketName, objectKey);
    byte[] decrypted = IOUtils.toByteArray(downloadedObject.getObjectContent());

    // Verify same data.
    Assert.assertTrue(Arrays.equals(plaintext, decrypted));
    //deleteBucketAndAllContents(encryptionClient);
}

From source file:ch.bfh.evoting.verifier.Verifier.java

/**
 * This program is a verifier for MobiVote application with HKRS12 protocol
 * You have to indicate the XML file exported from the application as argument for this program.
 * You can also indicate the files of different participants. In this case, there will be checked if all files contain the same values.
 * @param args the file(s) containing the values of the protocol
 *//*  w  w  w .java2  s .  c  om*/
public static void main(String[] args) {

    /*
     * Reading files given as parameters
     */
    if (args.length < 1) {
        //Not enough arguments
        System.out.println(
                "You must indicate the location of the XML file containing the data to verify! You can also indicate the files of different participants.");
        return;
    } else if (args.length > 1) {
        //Make a digest of all files and compare them
        List<byte[]> digests = new ArrayList<byte[]>();
        for (String s : args) {
            try {
                MessageDigest md = MessageDigest.getInstance("SHA-256");

                InputStream is = Files.newInputStream(Paths.get(s));
                DigestInputStream dis = new DigestInputStream(is, md);
                @SuppressWarnings("unused")
                int numBytes = -1;
                while ((numBytes = dis.read()) != -1) {
                    dis.read();
                }

                byte[] digest = md.digest();
                digests.add(digest);

            } catch (IOException e) {
                System.out.println("One of the given files does not exists!");
                return;
            } catch (NoSuchAlgorithmException e1) {
                System.out.println("Unable to compute the digest of the file!");
                return;
            }
        }
        System.out.print("Checking the similarity of the files...");
        for (byte[] b : digests) {
            for (byte[] b2 : digests) {
                if (!Arrays.equals(b, b2)) {
                    System.out.print("\t\t\t\t\t\t\t\t\t\tWRONG\n");
                    System.out.println(
                            "The given files are not all identical! This means the content received by the participants was not the same.");
                    return;
                }
            }
        }
        System.out.print("\t\t\t\t\t\t\t\t\t\tOK\n\n");
    } else if (args.length == 1) {
        //Print a warning
        System.out.println(
                "BE CAREFUL: checking only one file ensure that the cryptographic values of the protocol are correct.\n"
                        + "But, it does NOT ensure that all users have the same result. This could happen when a participant missed some messages.\n"
                        + "This case is only covered by this verifier by giving multiple files as argument.\n");
    }

    Serializer serializer = new Persister();
    File source = new File(args[0]);

    poll = null;
    System.out.print("Reading file...");
    try {
        poll = serializer.read(XMLPoll.class, source);
        System.out.print("\t\t\t\t\t\t\t\t\t\t\t\t\tOK\n");
    } catch (Exception e) {
        System.out.print("\t\t\t\t\t\t\t\t\t\t\t\t\tFAILED\n");
        e.printStackTrace();
        return;
    }

    /*
     * Create the initializations needed by the protocol
     */
    G_q = GStarModSafePrime.getInstance(new BigInteger(poll.getP()));
    Z_q = G_q.getZModOrder();
    Zgroup = Z.getInstance();
    generator = poll.getGenerator().getValue(G_q);

    representations = new Element[poll.getOptions().size()];
    int i = 0;
    for (XMLOption op : poll.getOptions()) {
        representations[i] = op.getRepresentation().getValue(Z_q);
        i++;
    }

    System.out.println();
    System.out.println("Verifying vote: \"" + poll.getQuestion() + "\"");

    /*
     * Verify the dependence between cryptographic values and the vote properties
     */
    //Not used anymore, implicitly done with otherInputs of proofs
    //verifyDependencyTextCrypto();

    /*
     * Verify the proofs for the user
     */
    System.out.println();
    System.out.println("Verifying the proof for the participants...");
    a = new Element[poll.getParticipants().size()];
    b = new Element[poll.getParticipants().size()];
    h = new Element[poll.getParticipants().size()];
    hHat = new Element[poll.getParticipants().size()];
    hHatPowX = new Element[poll.getParticipants().size()];
    proofForX = new Element[poll.getParticipants().size()];
    validityProof = new Element[poll.getParticipants().size()];
    equalityProof = new Element[poll.getParticipants().size()];
    Tuple pollTuple = preparePollOtherInput(poll, representations, generator);

    int k = 0;
    for (XMLParticipant p : poll.getParticipants()) {
        System.out.println("\tParticipant " + p.getIdentification() + " (" + p.getUniqueId() + ")");

        if (p.getAi() != null) {
            a[k] = p.getAi().getValue(G_q);
            if (DEBUG)
                System.out.println("Value a: " + a[k]);
        }
        if (p.getHi() != null) {
            h[k] = p.getHi().getValue(G_q);
            if (DEBUG)
                System.out.println("Value h: " + h[k]);
        }
        if (p.getBi() != null) {
            b[k] = p.getBi().getValue(G_q);
            if (DEBUG)
                System.out.println("Value b: " + b[k]);
        }
        if (p.getHiHat() != null) {
            hHat[k] = p.getHiHat().getValue(G_q);
            if (DEBUG)
                System.out.println("Value h hat: " + hHat[k]);
        }
        if (p.getHiHatPowXi() != null) {
            hHatPowX[k] = p.getHiHatPowXi().getValue(G_q);
            if (DEBUG)
                System.out.println("Value h hat ^ x: " + hHatPowX[k]);
        }

        if (p.getProofForXi() != null) {
            ProductGroup group = ProductGroup.getInstance(G_q, Zgroup, Z_q);

            proofForX[k] = p.getProofForXi().getValue(group);
        }

        if (p.getProofValidVote() != null) {

            Group[] tripleElement1Groups = new Group[poll.getOptions().size()];
            Group[] tripleElement2Groups = new Group[poll.getOptions().size()];
            Group[] tripleElement3Groups = new Group[poll.getOptions().size()];
            for (i = 0; i < poll.getOptions().size(); i++) {
                tripleElement1Groups[i] = ProductGroup.getInstance(G_q, G_q);
                tripleElement2Groups[i] = Zgroup;
                tripleElement3Groups[i] = Z_q;
            }
            ProductGroup tripleElement1 = ProductGroup.getInstance(tripleElement1Groups);
            ProductGroup tripleElement2 = ProductGroup.getInstance(tripleElement2Groups);
            ProductGroup tripleElement3 = ProductGroup.getInstance(tripleElement3Groups);
            ProductGroup group = ProductGroup.getInstance(tripleElement1, tripleElement2, tripleElement3);

            validityProof[k] = p.getProofValidVote().getValue(group);

            if (DEBUG)
                System.out.println("Proof of validity: " + validityProof[k]);
        }

        if (p.getProofForHiHat() != null) {
            recoveryNeeded = true;
            ProductGroup group = ProductGroup.getInstance(ProductGroup.getInstance(G_q, G_q), Zgroup, Z_q);

            equalityProof[k] = p.getProofForHiHat().getValue(group);
        }

        Tuple participantTuple = prepareParticipantOtherInput(p);
        otherInput = Tuple.getInstance(participantTuple, pollTuple);

        /*
         * Verify proof of knowledge 
         */
        System.out.print("\t\tVerifying proof of knowledge of x value...");
        verifyProofOfKnowledgeOfX(k);

        /*
         * Verify proof of validity 
         */
        if (validityProof[k] != null) {
            System.out.print("\t\tVerifying proof of valid vote...");
            verifyValidityProof(k);
        }

        /*
         * Verify proof of equality 
         */
        if (equalityProof[k] != null) {
            System.out.print("\t\tVerifying proof of equality between discrete logarithm g^x and h_hat^x...");
            verifyEqualityProof(k);
        }
        k++;
    }

    /**
     * Computing the result
     */
    System.out.println();
    System.out.print("Computing the result...");
    computeResult();

}

From source file:Main.java

public static boolean checkAreEquals(byte[] a, byte[] b) {
    return Arrays.equals(a, b);
}

From source file:Main.java

public static boolean arrayEquals(Object[] a1, Object[] a2) {
    return Arrays.equals(a1, a2);
}

From source file:Main.java

public static boolean cmpByteArrayShowResult(byte[] array1, byte[] array2, String comparedAlg) {
    return Arrays.equals(array1, array2);
}

From source file:Main.java

public static int indexOfSubArray(final int[][] array, final int[] subArray) {
    if (!isEmpty(array) && !isEmpty(subArray)) {
        for (int i = 0; i < array.length; i++) {
            if (Arrays.equals(array[i], subArray)) {
                return i;
            }//from  ww w  . j  a  v a  2s.co m
        }
    }

    return -1;
}