All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.proximax.core.crypto.Hashes Maven / Gradle / Ivy

Go to download

The ProximaX Sirius Chain Java SDK is a Java library for interacting with the Sirius Blockchain.

The newest version!
/*
 * Copyright 2018 NEM
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.proximax.core.crypto;

import java.security.MessageDigest;
import java.security.Security;

import org.spongycastle.jcajce.provider.digest.Keccak;
import org.spongycastle.jce.provider.BouncyCastleProvider;

import io.proximax.core.utils.ExceptionUtils;

/**
 * Static class that exposes hash functions.
 */
public class Hashes {

    static {
        Security.addProvider(new BouncyCastleProvider());
    }

    /**
     * Performs a SHA3-256 hash of the concatenated inputs.
     *
     * @param inputs The byte arrays to concatenate and hash.
     * @return The hash of the concatenated inputs.
     * @throws CryptoException if the hash operation failed.
     */
    public static byte[] sha3_256(final byte[]... inputs) {

        return hash("SHA3-256", inputs);
    }

    /**
     * Performs a SHA3-512 hash of the concatenated inputs.
     *
     * @param inputs The byte arrays to concatenate and hash.
     * @return The hash of the concatenated inputs.
     * @throws CryptoException if the hash operation failed.
     */
    public static byte[] sha3_512(final byte[]... inputs) {
        return hash("SHA3-512", inputs);
    }

    /**
     * Performs a RIPEMD160 hash of the concatenated inputs.
     *
     * @param inputs The byte arrays to concatenate and hash.
     * @return The hash of the concatenated inputs.
     * @throws CryptoException if the hash operation failed.
     */
    public static byte[] ripemd160(final byte[]... inputs) {
        return hash("RIPEMD160", inputs);
    }

    /**
     * Performs a KECCAK_256 hash of the concatenated inputs.
     *
     * @param inputs The byte arrays to concatenate and hash.
     * @return The hash of the concatenated inputs.
     * @throws CryptoException if the hash operation failed.
     */
    public static byte[] keccak256(final byte[] inputs) {

        Keccak.Digest256 keccak = new Keccak.Digest256();
        keccak.update(inputs);

        return keccak.digest();
    }

    /**
     * Performs a KECCAK_256 hash of the concatenated inputs.
     *
     * @param inputs The byte arrays to concatenate and hash.
     * @return The hash of the concatenated inputs.
     * @throws CryptoException if the hash operation failed.
     */
    public static byte[] keccak256(final byte[][] inputs) {

        Keccak.Digest256 keccak = new Keccak.Digest256();

        byte[] concat_inputs = new byte[0];
        byte[] concat_inputsCopy;

        for(int i=0; i {
                    final MessageDigest digest = MessageDigest.getInstance(algorithm, "SC");

                    for (final byte[] input : inputs) {
                        digest.update(input);
                    }

                    return digest.digest();
                },
                CryptoException::new);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy