Example usage for org.bouncycastle.crypto.digests SHA256Digest doFinal

List of usage examples for org.bouncycastle.crypto.digests SHA256Digest doFinal

Introduction

In this page you can find the example usage for org.bouncycastle.crypto.digests SHA256Digest doFinal.

Prototype

public int doFinal(byte[] out, int outOff) 

Source Link

Usage

From source file:org.freenetproject.freemail.transport.Channel.java

License:Open Source License

private String calculateNextSlot(String slot) {
    byte[] buf = Base32.decode(slot);
    SHA256Digest sha256 = new SHA256Digest();
    sha256.update(buf, 0, buf.length);//  w  w w  . j a  v  a2 s. c  o m
    sha256.doFinal(buf, 0);

    return Base32.encode(buf);
}

From source file:org.jcryptool.visual.hashing.views.HashingView.java

License:Open Source License

private String computeHash(String hashName, String inputText, Text hashText) {
    hash = hash.getName(hashName);/*from   w  ww  .  j ava  2  s  .co  m*/
    byte[] digest = null;
    switch (hash) {
    case MD2:
        MD2Digest md2 = new MD2Digest();
        md2.update(inputText.getBytes(), 0, inputText.getBytes().length);
        digest = new byte[md2.getDigestSize()];
        md2.doFinal(digest, 0);

        break;
    case MD4:
        MD4Digest md4 = new MD4Digest();
        md4.update(inputText.getBytes(), 0, inputText.getBytes().length);
        digest = new byte[md4.getDigestSize()];
        md4.doFinal(digest, 0);

        break;
    case MD5:
        MD5Digest md5 = new MD5Digest();
        md5.update(inputText.getBytes(), 0, inputText.getBytes().length);
        digest = new byte[md5.getDigestSize()];
        md5.doFinal(digest, 0);

        break;
    case SHA1:
        SHA1Digest sha1 = new SHA1Digest();
        sha1.update(inputText.getBytes(), 0, inputText.getBytes().length);
        digest = new byte[sha1.getDigestSize()];
        sha1.doFinal(digest, 0);

        break;
    case SHA256:
        SHA256Digest sha256 = new SHA256Digest();
        sha256.update(inputText.getBytes(), 0, inputText.getBytes().length);
        digest = new byte[sha256.getDigestSize()];
        sha256.doFinal(digest, 0);

        break;
    case SHA512:
        SHA512Digest sha512 = new SHA512Digest();
        sha512.update(inputText.getBytes(), 0, inputText.getBytes().length);
        digest = new byte[sha512.getDigestSize()];
        sha512.doFinal(digest, 0);

        break;
    case SHA3_224:
        SHA3.Digest224 sha3_224 = new SHA3.Digest224();
        sha3_224.update(inputText.getBytes(), 0, inputText.getBytes().length);
        digest = new byte[sha3_224.getDigestLength()];
        digest = sha3_224.digest();

        break;
    case SHA3_256:
        SHA3.Digest256 sha3_256 = new SHA3.Digest256();
        sha3_256.update(inputText.getBytes(), 0, inputText.getBytes().length);
        digest = new byte[sha3_256.getDigestLength()];
        digest = sha3_256.digest();

        break;
    case SHA3_384:
        SHA3.Digest384 sha3_384 = new SHA3.Digest384();
        sha3_384.update(inputText.getBytes(), 0, inputText.getBytes().length);
        digest = new byte[sha3_384.getDigestLength()];
        digest = sha3_384.digest();

        break;
    case SHA3_512:
        SHA3.Digest512 sha3_512 = new SHA3.Digest512();
        sha3_512.update(inputText.getBytes(), 0, inputText.getBytes().length);
        digest = new byte[sha3_512.getDigestLength()];
        digest = sha3_512.digest();

        break;
    case SKEIN_256:
        Skein.Digest_256_256 skein_256 = new Skein.Digest_256_256();
        skein_256.update(inputText.getBytes(), 0, inputText.getBytes().length);
        digest = new byte[skein_256.getDigestLength()];
        digest = skein_256.digest();

        break;
    case SKEIN_512:
        Skein.Digest_512_512 skein_512 = new Skein.Digest_512_512();
        skein_512.update(inputText.getBytes(), 0, inputText.getBytes().length);
        digest = new byte[skein_512.getDigestLength()];
        digest = skein_512.digest();

        break;
    case SKEIN_1024:
        Skein.Digest_1024_1024 skein_1024 = new Skein.Digest_1024_1024();
        skein_1024.update(inputText.getBytes(), 0, inputText.getBytes().length);
        digest = new byte[skein_1024.getDigestLength()];
        digest = skein_1024.digest();

        break;
    case RIPEMD160:
        RIPEMD160Digest ripemd160 = new RIPEMD160Digest();
        ripemd160.update(inputText.getBytes(), 0, inputText.getBytes().length);
        digest = new byte[ripemd160.getDigestSize()];
        ripemd160.doFinal(digest, 0);

        break;
    case SM3:
        SM3Digest sm3 = new SM3Digest();
        sm3.update(inputText.getBytes(), 0, inputText.getBytes().length);
        digest = new byte[sm3.getDigestSize()];
        sm3.doFinal(digest, 0);

        break;
    case TIGER:
        TigerDigest tiger = new TigerDigest();
        tiger.update(inputText.getBytes(), 0, inputText.getBytes().length);
        digest = new byte[tiger.getDigestSize()];
        tiger.doFinal(digest, 0);

        break;
    case GOST3411:
        GOST3411Digest gost3411 = new GOST3411Digest();
        gost3411.update(inputText.getBytes(), 0, inputText.getBytes().length);
        digest = new byte[gost3411.getDigestSize()];
        gost3411.doFinal(digest, 0);

        break;
    case WHIRLPOOL:
        WhirlpoolDigest whirlpool = new WhirlpoolDigest();
        whirlpool.update(inputText.getBytes(), 0, inputText.getBytes().length);
        digest = new byte[whirlpool.getDigestSize()];
        whirlpool.doFinal(digest, 0);

        break;
    default:
        break;
    }

    String hashHexValue = new String(Hex.encode(digest));
    if (btnHexadezimal.getSelection()) {
        String hashValueOutput = hashHexValue.toUpperCase().replaceAll(".{2}", "$0 "); //$NON-NLS-1$ //$NON-NLS-2$
        hashText.setText(hashValueOutput);
    } else if (btnDezimal.getSelection()) {
        String hashValue = hexToDecimal(hashHexValue);
        hashValue = hashValue.replaceAll(".{3}", "$0 "); //$NON-NLS-1$ //$NON-NLS-2$
        hashText.setText(hashValue);
    } else if (btnBinary.getSelection()) {
        String hashValue = hexToBinary(hashHexValue);
        hashValue = hashValue.replaceAll(".{8}", "$0#"); //$NON-NLS-1$ //$NON-NLS-2$
        hashText.setText(hashValue);
    }

    return hashHexValue;
}

From source file:org.opendaylight.capwap.dtls.DtlsUtils.java

License:Open Source License

static byte[] sha256DigestOf(byte[] input) {
    SHA256Digest d = new SHA256Digest();
    d.update(input, 0, input.length);// w w  w.j a  v a  2s. c om
    byte[] result = new byte[d.getDigestSize()];
    d.doFinal(result, 0);
    return result;
}

From source file:org.pwsafe.lib.crypto.SHA256Pws.java

License:Open Source License

public static byte[] digest(byte[] incoming) {

    SHA256Digest digest = new SHA256Digest();
    byte[] output = new byte[digest.getDigestSize()];

    digest.update(incoming, 0, incoming.length);
    digest.doFinal(output, 0);

    return output;

}

From source file:org.pwsafe.lib.crypto.SHA256Pws.java

License:Open Source License

private static byte[] digestNJava(byte[] p, int iter) {
    SHA256Digest digest = new SHA256Digest();
    byte[] output = new byte[digest.getDigestSize()];
    byte[] input = new byte[digest.getDigestSize()];
    byte[] t;/*from w w w.java2  s  .c o  m*/

    digest.update(p, 0, p.length);
    digest.doFinal(output, 0);

    for (int i = 0; i < iter; ++i) {
        t = input;
        input = output;
        output = t;

        digest.reset();
        digest.update(input, 0, input.length);
        digest.doFinal(output, 0);
    }

    return output;
}

From source file:org.satochip.satochipclient.CardConnectorTest.java

License:Apache License

public void testCardParseTransaction(byte keynbr) throws CardConnectorException, ECException {

    // recover pubkey
    byte[] pubkey, response;
    CardDataParser.PubKeyData dataparser = new CardDataParser.PubKeyData(authentikey);
    if (keynbr == bip32_keynbr) {
        response = cc.cardBip32GetExtendedKey(default_bip32path);
        authentikey = dataparser.parseBip32GetExtendedKey(response).authentikey;
        pubkey = dataparser.pubkey;/*from   w ww.  j av  a 2 s. c o  m*/
    } else {
        response = cc.cardGetPublicKeyFromPrivate(keynbr);
        pubkey = dataparser.parseGetPublicKeyFromPrivate(response).pubkey;
    }

    // bitcoinj
    NetworkParameters params;
    params = RegTestParams.get();
    Transaction tx = new Transaction(params);
    ECKey serverKey = new ECKey(null, pubkey, true);
    BigInteger nanoCoins = Utils.toNanoCoins(1, 0);
    TransactionOutput outputToMe = new TransactionOutput(params, tx, nanoCoins, serverKey);

    // simple tx
    tx.addOutput(outputToMe);
    tx.addInput(new TransactionInput(params, tx, outputToMe.getScriptBytes()));

    int inputIndex = 0;
    byte[] connectedScript = outputToMe.getScriptBytes();
    byte sigHashType = (byte) TransactionSignature.calcSigHashValue(SigHash.ALL, false);
    byte[] rawtxforhashing = byteArrayForSignature(tx, inputIndex, connectedScript, sigHashType);

    // unused
    System.out.println("Raw tx for hashing:" + toHexString(rawtxforhashing));
    byte[] rawtxhash = new byte[32];
    SHA256Digest sha256 = new SHA256Digest();
    sha256.reset();
    sha256.update(rawtxforhashing, 0, rawtxforhashing.length);
    sha256.doFinal(rawtxhash, 0);
    //System.out.println("Raw tx singlehash:" + toString(rawtxhash));
    sha256.reset();
    sha256.update(rawtxhash, 0, rawtxhash.length);
    sha256.doFinal(rawtxhash, 0);
    //System.out.println("Raw tx doublehash:" + toString(rawtxhash));

    Sha256Hash rawtxhash2 = tx.hashForSignature(inputIndex, connectedScript, sigHashType);
    byte[] txhash_sw = rawtxhash2.getBytes();
    System.out.println("Tx hash Bitcoinj: " + toHexString(txhash_sw));

    // send to card for parsing
    //byte[] response= cc.cardParseTransaction(rawtxforhashing);
    response = cc.cardParseTx(rawtxforhashing);
    CardDataParser.PubKeyData txparser = new CardDataParser.PubKeyData(authentikey);
    byte[] txhash_hw = txparser.parseTxHash(response).data; //Arrays.copyOfRange(response, 2, 2+32);
    System.out.println("Tx hash SatoChip: " + toHexString(txhash_hw));
    System.out.println(txparser.toString());
    assertArrayEquals(txhash_hw, txhash_sw);

    // check if 2fa is required
    boolean need_2fa_chalresp = ((txparser.option_flags & 0x8000) == 0x8000) ? true : false; // if msb is set, a challenge-response 2nd factor authentification is needed
    byte[] txhmac = null;
    if (need_2fa_chalresp) {
        try {
            System.out.println("Second factor authentication required for challenge response...");
            System.out.println("Please insert a configured yubikey!");
            MILLISECONDS.sleep(2000);
        } catch (InterruptedException ex) {
        }
        YubikeyConnector yubikey = new YubikeyConnector(false);
        yubikey.findYubikey(YubikeyConnector.PRODUCT_ID_NEO);
        yubikey.openYubikey();
        yubikey.attachYubikeyInterface();
        txhmac = yubikey.challenge_response(txhash_hw, YubikeyConnector.MODE_HMAC, YubikeyConnector.SLOT_2,
                false, true);
        yubikey.releaseYubikeyInterface();
        yubikey.closeYubikey();
        System.out.println("txhmac: " + toHexString(txhmac));
        // test with wrong hmac:
        //txhmac[0]=0;
    }
    byte[] txsign = cc.cardSignTransaction(keynbr, txhash_hw, txhmac);
    System.out.println("txsign: " + toHexString(txsign));

}

From source file:org.sperle.keepass.crypto.bc.SHA256Hash.java

License:Open Source License

public byte[] getHash(byte[][] messages, ProgressMonitor pm) {
    SHA256Digest md = new SHA256Digest();
    if (pm != null)
        pm.nextStep(sumLength(messages) / DWORD_LENGTH, "pm_hash");
    for (int i = 0; i < messages.length; i++) {
        md.update(messages[i], 0, messages[i].length, pm);
        if (pm != null && pm.isCanceled())
            return null;
    }//from ww w.j a va 2 s.  c o  m
    byte[] hash = new byte[md.getDigestSize()];
    md.doFinal(hash, 0);
    return hash;
}

From source file:org.sperle.keepass.io.j2me.J2meIOManager.java

License:Open Source License

public byte[] generateHash(String filename, int packetSize) throws IOException {
    FileConnection conn = null;/*from w  ww. jav a 2 s.  c  o  m*/
    byte[] hash = null;
    try {
        conn = (FileConnection) Connector.open(filename, Connector.READ);
        InputStream is = conn.openInputStream();
        byte[] buf = new byte[packetSize];
        int read = -1;
        SHA256Digest md = new SHA256Digest();
        while ((read = is.read(buf)) > -1) {
            md.update(buf, 0, read, null);
        }
        hash = new byte[md.getDigestSize()];
        md.doFinal(hash, 0);
    } finally {
        try {
            if (conn != null)
                conn.close();
        } catch (IOException e) {
        }
    }

    return hash;
}

From source file:org.toporin.bitcoincore.ECKey.java

License:Apache License

public static byte[] recoverFromSignature(int recID, byte[] msg, byte[] sig, boolean doublehash)
        throws ECException {

    //return CardConnector.recoverPublicKeyFromSig(recID, msg, sig, doublehash);

    byte[] digest = new byte[32];
    SHA256Digest sha256 = new SHA256Digest();
    sha256.reset();//from  www .  j  av a2  s .  co  m
    sha256.update(msg, 0, msg.length);
    sha256.doFinal(digest, 0);
    if (doublehash) {
        sha256.reset();
        sha256.update(digest, 0, digest.length);
        sha256.doFinal(digest, 0);
    }
    BigInteger bi = new BigInteger(1, digest);
    ECDSASignature ecdsaSig = new ECDSASignature(sig);
    ECKey k = ECKey.recoverFromSignature(recID, ecdsaSig, bi, true);

    if (k != null)
        return k.getPubKey();
    else
        return null;

}