List of usage examples for org.bouncycastle.crypto.digests SHA256Digest SHA256Digest
public SHA256Digest()
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);/*w w w . j ava 2s .c o m*/ 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 ww . j a v a 2 s . c om*/ 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 ww w . ja v a 2 s . c om } 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.ScripterRon.BitcoinCore.ECKey.java
License:Apache License
/** * Creates a signature for the supplied contents using the private key * * @param contents Contents to be signed * @return ECDSA signature * @throws ECException Unable to create signature *///from w w w. j av a 2 s . c o m public ECDSASignature createSignature(byte[] contents) throws ECException { if (privKey == null) throw new IllegalStateException("No private key available"); // // Get the double SHA-256 hash of the signed contents // byte[] contentsHash = Utils.doubleDigest(contents); // // Create the signature // BigInteger[] sigs; try { ECDSASigner signer = new ECDSASigner(new HMacDSAKCalculator(new SHA256Digest())); ECPrivateKeyParameters privKeyParams = new ECPrivateKeyParameters(privKey, ecParams); signer.init(true, privKeyParams); sigs = signer.generateSignature(contentsHash); } catch (RuntimeException exc) { throw new ECException("Exception while creating signature", exc); } // // Create a canonical signature by adjusting the S component to be less than or equal to // half the curve order. // if (sigs[1].compareTo(HALF_CURVE_ORDER) > 0) sigs[1] = ecParams.getN().subtract(sigs[1]); return new ECDSASignature(sigs[0], sigs[1]); }
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 w ww. j ava 2s . c om*/ 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 w w . j av a2 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 w w w.jav a 2 s. c o 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; }
From source file:org.tramaci.onionmail.Stdio.java
License:Open Source License
public static byte[] sha256(byte[] a) throws Exception { Digest d = new SHA256Digest(); byte[] r = new byte[d.getDigestSize()]; d.update(a, 0, a.length);/*from w w w . j a v a2s. c o m*/ d.doFinal(r, 0); return r; }
From source file:org.tramaci.onionmail.Stdio.java
License:Open Source License
public static byte[] sha256a(byte[][] a) throws Exception { Digest d = new SHA256Digest(); byte[] r = new byte[d.getDigestSize()]; int cx = a.length; for (int ax = 0; ax < cx; ax++) d.update(a[ax], 0, a[ax].length); d.doFinal(r, 0);// w w w.ja v a2 s. co m return r; }
From source file:org.xipki.commons.security.HashAlgoType.java
License:Open Source License
public Digest createDigest() { switch (this) { case SHA1:// w ww . ja va 2s .co m return new SHA1Digest(); case SHA224: return new SHA224Digest(); case SHA256: return new SHA256Digest(); case SHA384: return new SHA384Digest(); case SHA512: return new SHA512Digest(); case SHA3_224: return new SHA3Digest(224); case SHA3_256: return new SHA3Digest(256); case SHA3_384: return new SHA3Digest(384); case SHA3_512: return new SHA3Digest(512); default: throw new RuntimeException("should not reach here, unknown HashAlgoType " + name()); } }