List of usage examples for org.bouncycastle.crypto.digests SHA256Digest doFinal
public int doFinal(byte[] out, int outOff)
From source file:org.cryptoworkshop.ximix.test.tests.BLSProcessingTest.java
License:Apache License
@Test public void testBLSSigning() throws Exception { SquelchingThrowableHandler handler = new SquelchingThrowableHandler(); ///*www. j ava2s .c om*/ // Squelch out socket exceptions emitted by close of connections below. // handler.squelchType(SocketException.class); XimixNode nodeOne = getXimixNode("/conf/mixnet.xml", "/conf/node1.xml", handler); NodeTestUtil.launch(nodeOne); XimixNode nodeTwo = getXimixNode("/conf/mixnet.xml", "/conf/node2.xml", handler); NodeTestUtil.launch(nodeTwo); XimixNode nodeThree = getXimixNode("/conf/mixnet.xml", "/conf/node3.xml", handler); NodeTestUtil.launch(nodeThree); XimixNode nodeFour = getXimixNode("/conf/mixnet.xml", "/conf/node4.xml", handler); NodeTestUtil.launch(nodeFour); XimixNode nodeFive = getXimixNode("/conf/mixnet.xml", "/conf/node5.xml", handler); NodeTestUtil.launch(nodeFive); XimixRegistrar registrar = XimixRegistrarFactory .createAdminServiceRegistrar(ResourceAnchor.load("/conf/mixnet.xml"), new TestNotifier()); KeyGenerationService keyGenerationService = registrar.connect(KeyGenerationService.class); KeyGenerationOptions keyGenOptions = new KeyGenerationOptions.Builder(Algorithm.BLS, "secp256r1") .withThreshold(3).withNodes("A", "B", "C", "D").build(); BLS01PublicKeyParameters sigPubKey = BLSPublicKeyFactory .createKey(keyGenerationService.generatePublicKey("BLSKEY", keyGenOptions)); SigningService signingService = registrar.connect(SigningService.class); SHA256Digest sha256 = new SHA256Digest(); byte[] message = "hello world!".getBytes(); byte[] hash = new byte[sha256.getDigestSize()]; sha256.update(message, 0, message.length); sha256.doFinal(hash, 0); SignatureGenerationOptions sigGenOptions = new SignatureGenerationOptions.Builder(Algorithm.BLS) .withThreshold(3).withNodes("A", "B", "C", "D").build(); byte[] blsSig = signingService.generateSignature("BLSKEY", sigGenOptions, hash); // // check the signature locally. // BLS01Signer signer = new BLS01Signer(sha256); signer.init(false, sigPubKey); signer.update(message, 0, message.length); Assert.assertTrue(signer.verifySignature(blsSig)); // // Shutdown nodes and close services. // keyGenerationService.shutdown(); signingService.shutdown(); NodeTestUtil.shutdownNodes(); }
From source file:org.cryptoworkshop.ximix.test.tests.BLSProcessingTest.java
License:Apache License
private void doMixedMissingTest(SigningService signingService, final BLS01PublicKeyParameters sigPubKey, String[] sigNodes) throws Exception { SHA256Digest sha256 = new SHA256Digest(); byte[] message = "hello world!".getBytes(); byte[] hash = new byte[sha256.getDigestSize()]; sha256.update(message, 0, message.length); sha256.doFinal(hash, 0); SignatureGenerationOptions sigGenOptions = new SignatureGenerationOptions.Builder(Algorithm.BLS) .withThreshold(2).withNodes(sigNodes).build(); byte[] blsSig = signingService.generateSignature("BLSKEY", sigGenOptions, hash); ////from w w w . j a va 2 s .com // check the signature locally. // BLS01Signer signer = new BLS01Signer(sha256); signer.init(false, sigPubKey); signer.update(message, 0, message.length); Assert.assertTrue(signer.verifySignature(blsSig)); }
From source file:org.cryptoworkshop.ximix.test.tests.ECDSAProcessingTest.java
License:Apache License
@Test public void testECDSASigning() throws Exception { SquelchingThrowableHandler handler = new SquelchingThrowableHandler(); ///*from w w w .j av a 2s .c o m*/ // Squelch out socket exceptions emitted by close of connections below. // handler.squelchType(SocketException.class); XimixNode nodeOne = getXimixNode("/conf/mixnet.xml", "/conf/node1.xml", handler); NodeTestUtil.launch(nodeOne); XimixNode nodeTwo = getXimixNode("/conf/mixnet.xml", "/conf/node2.xml", handler); NodeTestUtil.launch(nodeTwo); XimixNode nodeThree = getXimixNode("/conf/mixnet.xml", "/conf/node3.xml", handler); NodeTestUtil.launch(nodeThree); XimixNode nodeFour = getXimixNode("/conf/mixnet.xml", "/conf/node4.xml", handler); NodeTestUtil.launch(nodeFour); XimixNode nodeFive = getXimixNode("/conf/mixnet.xml", "/conf/node5.xml", handler); NodeTestUtil.launch(nodeFive); XimixRegistrar registrar = XimixRegistrarFactory .createAdminServiceRegistrar(ResourceAnchor.load("/conf/mixnet.xml"), new TestNotifier()); KeyGenerationService keyGenerationService = registrar.connect(KeyGenerationService.class); KeyGenerationOptions keyGenOptions = new KeyGenerationOptions.Builder(Algorithm.ECDSA, "secp256r1") .withThreshold(2).withNodes("A", "B", "C", "D").build(); ECPublicKeyParameters sigPubKey = (ECPublicKeyParameters) PublicKeyFactory .createKey(keyGenerationService.generatePublicKey("ECKEY", keyGenOptions)); SigningService signingService = registrar.connect(SigningService.class); SHA256Digest sha256 = new SHA256Digest(); byte[] message = "hello world!".getBytes(); byte[] hash = new byte[sha256.getDigestSize()]; sha256.update(message, 0, message.length); sha256.doFinal(hash, 0); SignatureGenerationOptions sigGenOptions = new SignatureGenerationOptions.Builder(Algorithm.ECDSA) .withThreshold(2).withNodes("A", "B", "C", "D").build(); byte[] dsaSig = signingService.generateSignature("ECKEY", sigGenOptions, hash); // // check the signature locally. // ECDSASigner signer = new ECDSASigner(); signer.init(false, sigPubKey); BigInteger[] rs = decodeSig(dsaSig); Assert.assertTrue(signer.verifySignature(hash, rs[0], rs[1])); // // Shutdown nodes and close services. // NodeTestUtil.shutdownNodes(); keyGenerationService.shutdown(); signingService.shutdown(); }
From source file:org.cryptoworkshop.ximix.test.tests.ECDSAProcessingTest.java
License:Apache License
private void doMixedMissingTest(SigningService signingService, final ECPublicKeyParameters sigPubKey, String[] sigNodes) throws Exception { SHA256Digest sha256 = new SHA256Digest(); byte[] message = "hello world!".getBytes(); byte[] hash = new byte[sha256.getDigestSize()]; sha256.update(message, 0, message.length); sha256.doFinal(hash, 0); SignatureGenerationOptions sigGenOptions = new SignatureGenerationOptions.Builder(Algorithm.ECDSA) .withThreshold(2).withNodes(sigNodes).build(); byte[] dsaSig = signingService.generateSignature("ECKEY", sigGenOptions, hash); ////from w w w . ja v a 2 s. co m // check the signature locally. // ECDSASigner signer = new ECDSASigner(); signer.init(false, sigPubKey); BigInteger[] rs = decodeSig(dsaSig); Assert.assertTrue(signer.verifySignature(hash, rs[0], rs[1])); }
From source file:org.fnppl.opensdx.security.KeyLog.java
License:Open Source License
public byte[] calcSha256LocalProof() throws Exception { //localproof of ipv4, ipv6, action.signature.signaturesbytes byte[] ret = new byte[32]; //256 bit = 32 byte SHA256Digest sha256 = new org.bouncycastle.crypto.digests.SHA256Digest(); byte[] data;// w w w . j ava 2s. c o m if (ipv4 != null && ipv4.length() > 0) { data = ipv4.getBytes("UTF-8"); //System.out.println("ipv4: "+ipv4); sha256.update(data, 0, data.length); } if (ipv6 != null && ipv4.length() > 0) { data = ipv6.getBytes("UTF-8"); //System.out.println("ipv6: "+ipv6); sha256.update(data, 0, data.length); } data = action.getSignatureBytes(); if (data != null) { //System.out.println("sigbytes: "+SecurityHelper.HexDecoder.encode(data, '\0', -1)); sha256.update(data, 0, data.length); } sha256.doFinal(ret, 0); return ret; }
From source file:org.fnppl.opensdx.security.SecurityHelper.java
License:Open Source License
public static byte[][] getMD5SHA1SHA256(byte[] data) { byte[] ret = new byte[16 + 20 + 32]; //160 bit = 20 byte + md5 128bit = 16 + sha256 256bit = 32 byte byte[] md5ret = new byte[16]; byte[] sha1ret = new byte[20]; byte[] sha256ret = new byte[32]; org.bouncycastle.crypto.digests.MD5Digest md5 = new org.bouncycastle.crypto.digests.MD5Digest(); md5.update(data, 0, data.length);//from ww w . j a va2 s.c o m md5.doFinal(ret, 0); org.bouncycastle.crypto.digests.SHA1Digest sha1 = new org.bouncycastle.crypto.digests.SHA1Digest(); sha1.update(data, 0, data.length); sha1.doFinal(ret, 16); org.bouncycastle.crypto.digests.SHA256Digest sha256 = new org.bouncycastle.crypto.digests.SHA256Digest(); sha256.update(data, 0, data.length); sha256.doFinal(ret, 16 + 20); System.arraycopy(ret, 0, md5ret, 0, md5ret.length); System.arraycopy(ret, 16, sha1ret, 0, sha1ret.length); System.arraycopy(ret, 16 + 20, sha256ret, 0, sha256ret.length); return new byte[][] { ret, md5ret, sha1ret, sha256ret }; }
From source file:org.fnppl.opensdx.security.SecurityHelper.java
License:Open Source License
public static byte[][] getMD5SHA1SHA256(InputStream fin) throws Exception { byte[] ret = new byte[16 + 20 + 32]; //160 bit = 20 byte + md5 128bit = 16 + sha256 256bit = 32 byte byte[] md5ret = new byte[16]; byte[] sha1ret = new byte[20]; byte[] sha256ret = new byte[32]; org.bouncycastle.crypto.digests.MD5Digest md5 = new org.bouncycastle.crypto.digests.MD5Digest(); org.bouncycastle.crypto.digests.SHA1Digest sha1 = new org.bouncycastle.crypto.digests.SHA1Digest(); org.bouncycastle.crypto.digests.SHA256Digest sha256 = new org.bouncycastle.crypto.digests.SHA256Digest(); int read = -1; byte[] buff = new byte[4096]; while ((read = fin.read(buff)) != -1) { md5.update(buff, 0, read);// w w w. ja v a 2 s. c o m sha1.update(buff, 0, read); sha256.update(buff, 0, read); } sha1.doFinal(ret, 16); md5.doFinal(ret, 0); sha256.doFinal(ret, 16 + 20); System.arraycopy(ret, 0, md5ret, 0, md5ret.length); System.arraycopy(ret, 16, sha1ret, 0, sha1ret.length); System.arraycopy(ret, 16 + 20, sha256ret, 0, sha256ret.length); return new byte[][] { ret, md5ret, sha1ret, sha256ret }; }
From source file:org.fnppl.opensdx.security.SecurityHelper.java
License:Open Source License
public static byte[] getSHA256LocalProof(Vector<Element> ve) throws Exception { byte[] ret = new byte[32];//256 bit = 32 byte SHA256Digest sha256 = new SHA256Digest(); //System.out.println("--- sha1localproof ---"); for (Element e : ve) { rekursiveUpdateSHA256(sha256, e); }//from www .j a v a 2 s .c om sha256.doFinal(ret, 0); //System.out.println("--- RESULT ----"); //System.out.println(SecurityHelper.HexDecoder.encode(ret, ':',-1)); return ret; }
From source file:org.fnppl.opensdx.security.SecurityHelper.java
License:Open Source License
public static byte[] getSHA256(byte[] data) { byte[] ret = new byte[32]; //256 bit = 32 byte org.bouncycastle.crypto.digests.SHA256Digest sha256 = new org.bouncycastle.crypto.digests.SHA256Digest(); sha256.update(data, 0, data.length); sha256.doFinal(ret, 0); return ret;// w w w. jav a 2 s . co m }
From source file:org.freenetproject.freemail.RTSFetcher.java
License:Open Source License
private boolean handle_rts(File rtsmessage) throws ConnectionTerminatedException, InterruptedException { // sanity check! if (!rtsmessage.exists()) return false; if (rtsmessage.length() > RTS_MAX_SIZE) { Logger.normal(this, "RTS Message is too large - discarding!"); return true; }//from w w w . ja v a 2 s.c o m // decrypt byte[] plaintext; try { plaintext = decrypt_rts(rtsmessage); } catch (IOException ioe) { Logger.normal(this, "Error reading RTS message!"); return false; } catch (InvalidCipherTextException icte) { Logger.normal(this, "Could not decrypt RTS message - discarding. " + icte.getMessage()); return true; } File rtsfile = null; byte[] their_encrypted_sig; int messagebytes = 0; LineReadingInputStream lis = null; PrintStream ps = null; try { rtsfile = File.createTempFile("rtstmp", "tmp", Freemail.getTempDir()); ByteArrayInputStream bis = new ByteArrayInputStream(plaintext); lis = new LineReadingInputStream(bis); ps = new PrintStream(new FileOutputStream(rtsfile)); String line; while (true) { try { line = lis.readLine(200, 200, false); } catch (TooLongException tle) { Logger.normal(this, "RTS message has lines that are too long. Discarding."); rtsfile.delete(); return true; } messagebytes += lis.getLastBytesRead(); if (line == null || line.equals("")) break; //FreemailLogger.normal(this, line); ps.println(line); } if (line == null) { // that's not right, we shouldn't have reached the end of the file, just the blank line before the signature Logger.normal(this, "Couldn't find signature on RTS message - ignoring!"); rtsfile.delete(); return true; } // read the rest of the file into a byte array. // will probably have extra stuff on the end because // the byte array returned by the decrypt function // isn't resized when we know how much plaintext // there is. It would be a waste of time, we know // we have to read exactly one RSA block's worth. their_encrypted_sig = new byte[bis.available()]; int totalread = 0; while (true) { int read = bis.read(their_encrypted_sig, totalread, bis.available()); if (read <= 0) break; totalread += read; } } catch (IOException ioe) { Logger.normal(this, "IO error whilst handling RTS message. " + ioe.getMessage()); ioe.printStackTrace(); if (rtsfile != null) rtsfile.delete(); return false; } finally { if (ps != null) { ps.close(); } if (lis != null) { try { lis.close(); } catch (IOException e) { Logger.error(this, "Caugth IOException while closing input", e); } } } PropsFile rtsprops = PropsFile.createPropsFile(rtsfile); try { validate_rts(rtsprops); } catch (Exception e) { Logger.normal(this, "RTS message does not contain vital information: " + e.getMessage() + " - discarding"); rtsfile.delete(); return true; } // verify the signature String their_mailsite = rtsprops.get("mailsite"); SHA256Digest sha256 = new SHA256Digest(); sha256.update(plaintext, 0, messagebytes); byte[] our_hash = new byte[sha256.getDigestSize()]; sha256.doFinal(our_hash, 0); HighLevelFCPClient fcpcli = new HighLevelFCPClient(); Logger.normal(this, "Trying to fetch sender's mailsite: " + their_mailsite); File msfile; try { msfile = fcpcli.fetch(their_mailsite); } catch (FCPFetchException fe) { // oh well, try again in a bit rtsfile.delete(); return false; } catch (FCPException e) { Logger.error(this, "Unknown error while checking sender's mailsite: " + e.getMessage()); //Try again later rtsfile.delete(); return false; } PropsFile mailsite = PropsFile.createPropsFile(msfile); String their_exponent = mailsite.get("asymkey.pubexponent"); String their_modulus = mailsite.get("asymkey.modulus"); if (their_exponent == null || their_modulus == null) { Logger.normal(this, "Mailsite fetched successfully but missing vital information! Discarding this RTS."); msfile.delete(); rtsfile.delete(); return true; } RSAKeyParameters their_pubkey = new RSAKeyParameters(false, new BigInteger(their_modulus, 32), new BigInteger(their_exponent, 32)); AsymmetricBlockCipher deccipher = new RSAEngine(); deccipher.init(false, their_pubkey); byte[] their_hash; try { their_hash = deccipher.processBlock(their_encrypted_sig, 0, deccipher.getInputBlockSize()); } catch (InvalidCipherTextException icte) { Logger.normal(this, "It was not possible to decrypt the signature of this RTS message. Discarding the RTS message."); msfile.delete(); rtsfile.delete(); return true; } // finally we can now check that our hash and their hash // match! if (their_hash.length < our_hash.length) { Logger.normal(this, "The signature of the RTS message is not valid (our hash: " + our_hash.length + "bytes, their hash: " + their_hash.length + "bytes. Discarding the RTS message."); msfile.delete(); rtsfile.delete(); return true; } int i; for (i = 0; i < our_hash.length; i++) { if (their_hash[i] != our_hash[i]) { Logger.normal(this, "The signature of the RTS message is not valid. Discarding the RTS message."); msfile.delete(); rtsfile.delete(); return true; } } Logger.normal(this, "Signature valid :)"); // the signature is valid! Hooray! // Now verify the message is for us if (!account.getIdentity().equals(rtsprops.get("to"))) { Logger.normal(this, "Recieved an RTS message that was not intended for the recipient. Discarding."); msfile.delete(); rtsfile.delete(); return true; } Logger.normal(this, "Original message intended for us :)"); //Clean up temp files if (!msfile.delete()) { Logger.error(this, "Couldn't delete fetched mailsite: " + msfile); } if (!rtsfile.delete()) { Logger.error(this, "Couldn't delete rts file: " + rtsfile); } account.getMessageHandler().createChannelFromRTS(rtsprops); return true; }