List of usage examples for org.bouncycastle.crypto.digests SHA256Digest update
public void update(byte[] in, int inOff, int len)
From source file:de.tsenger.animamea.crypto.KeyDerivationFunction.java
License:Open Source License
/** * Erzeugt AES-256 Schlssel//from w ww . j a v a2 s .c o m * * @return Schlssel als Byte-Array */ public byte[] getAES256Key() { byte[] checksum = new byte[32]; SHA256Digest sha256 = new SHA256Digest(); sha256.update(mergedData, 0, mergedData.length); sha256.doFinal(checksum, 0); return checksum; }
From source file:dorkbox.util.HashUtil.java
License:Apache License
/** * gets the SHA256 hash + SALT of the specified username, as UTF-16 *//*from ww w . j a v a 2 s. co m*/ public static byte[] getSha256WithSalt(String username, byte[] saltBytes) { if (username == null) { return null; } byte[] charToBytes = Sys.charToBytes16(username.toCharArray()); byte[] userNameWithSalt = Sys.concatBytes(charToBytes, saltBytes); SHA256Digest sha256 = new SHA256Digest(); byte[] usernameHashBytes = new byte[sha256.getDigestSize()]; sha256.update(userNameWithSalt, 0, userNameWithSalt.length); sha256.doFinal(usernameHashBytes, 0); return usernameHashBytes; }
From source file:dorkbox.util.HashUtil.java
License:Apache License
/** * gets the SHA256 hash of the specified string, as UTF-16 *///from ww w.ja v a 2s . c o m public static byte[] getSha256(String string) { byte[] charToBytes = Sys.charToBytes16(string.toCharArray()); SHA256Digest sha256 = new SHA256Digest(); byte[] usernameHashBytes = new byte[sha256.getDigestSize()]; sha256.update(charToBytes, 0, charToBytes.length); sha256.doFinal(usernameHashBytes, 0); return usernameHashBytes; }
From source file:dorkbox.util.HashUtil.java
License:Apache License
/** * gets the SHA256 hash of the specified byte array *//*from w w w. java 2s . com*/ public static byte[] getSha256(byte[] bytes) { SHA256Digest sha256 = new SHA256Digest(); byte[] hashBytes = new byte[sha256.getDigestSize()]; sha256.update(bytes, 0, bytes.length); sha256.doFinal(hashBytes, 0); return hashBytes; }
From source file:dorkbox.util.HashUtil.java
License:Apache License
public static byte[] getSha256WithSalt(byte[] bytes, byte[] saltBytes) { if (bytes == null || saltBytes == null) { return null; }/*w ww . j a v a2 s . c o m*/ byte[] bytesWithSalt = dorkbox.util.Sys.concatBytes(bytes, saltBytes); SHA256Digest sha256 = new SHA256Digest(); byte[] usernameHashBytes = new byte[sha256.getDigestSize()]; sha256.update(bytesWithSalt, 0, bytesWithSalt.length); sha256.doFinal(usernameHashBytes, 0); return usernameHashBytes; }
From source file:dorkbox.util.Sys.java
License:Apache License
/** * gets the SHA256 hash + SALT of the specified username, as UTF-16 *//*www .j a va2s. c o m*/ public static byte[] getSha256WithSalt(String username, byte[] saltBytes) { if (username == null) { return null; } byte[] charToBytes = Sys.charToBytes(username.toCharArray()); byte[] userNameWithSalt = Sys.concatBytes(charToBytes, saltBytes); SHA256Digest sha256 = new SHA256Digest(); byte[] usernameHashBytes = new byte[sha256.getDigestSize()]; sha256.update(userNameWithSalt, 0, userNameWithSalt.length); sha256.doFinal(usernameHashBytes, 0); return usernameHashBytes; }
From source file:dorkbox.util.Sys.java
License:Apache License
/** * gets the SHA256 hash of the specified string, as UTF-16 */// w w w . j a v a2s .c o m public static byte[] getSha256(String string) { byte[] charToBytes = Sys.charToBytes(string.toCharArray()); SHA256Digest sha256 = new SHA256Digest(); byte[] usernameHashBytes = new byte[sha256.getDigestSize()]; sha256.update(charToBytes, 0, charToBytes.length); sha256.doFinal(usernameHashBytes, 0); return usernameHashBytes; }
From source file:freemail.HashSlotManager.java
License:Open Source License
@Override protected String incSlot(String slot) { byte[] buf = Base32.decode(slot); SHA256Digest sha256 = new SHA256Digest(); sha256.update(buf, 0, buf.length); sha256.doFinal(buf, 0);// w w w. j a v a 2 s . co m return Base32.encode(buf); }
From source file:freemail.OutboundContact.java
License:Open Source License
/** * Set up an outbound contact. Fetch the mailsite, generate a new SSK keypair and post an RTS message to the appropriate KSK. * Will block for mailsite retrieval and RTS insertion * * @return true for success//from www . ja va2s . c o m */ private boolean init() throws ConnectionTerminatedException, InterruptedException { Logger.normal(this, "Initialising Outbound Contact " + address.toString()); // try to fetch get all necessary info. will fetch mailsite / generate new keys if necessary String initialslot = this.getCurrentLowestSlot(); SSKKeyPair commssk = this.getCommKeyPair(); if (commssk == null) return false; SSKKeyPair ackssk = this.getAckKeyPair(); RSAKeyParameters their_pub_key = this.getPubKey(); if (their_pub_key == null) return false; String rtsksk = this.getRtsKsk(); if (rtsksk == null) return false; StringBuffer rtsmessage = new StringBuffer(); // the public part of the SSK keypair we generated rtsmessage.append("commssk=" + commssk.pubkey + "\r\n"); rtsmessage.append("ackssk=" + ackssk.privkey + "\r\n"); rtsmessage.append("initialslot=" + initialslot + "\r\n"); rtsmessage.append("messagetype=rts\r\n"); // must include who this RTS is to, otherwise we're vulnerable to surreptitious forwarding rtsmessage.append("to=" + this.address.getSubDomain() + "\r\n"); // get our mailsite URI String our_mailsite_uri = account.getProps().get("mailsite.pubkey"); rtsmessage.append("mailsite=" + our_mailsite_uri + "\r\n"); rtsmessage.append("\r\n"); //FreemailLogger.normal(this,rtsmessage.toString()); // sign the message SHA256Digest sha256 = new SHA256Digest(); sha256.update(rtsmessage.toString().getBytes(), 0, rtsmessage.toString().getBytes().length); byte[] hash = new byte[sha256.getDigestSize()]; sha256.doFinal(hash, 0); RSAKeyParameters our_priv_key = AccountManager.getPrivateKey(account.getProps()); AsymmetricBlockCipher sigcipher = new RSAEngine(); sigcipher.init(true, our_priv_key); byte[] sig = null; try { sig = sigcipher.processBlock(hash, 0, hash.length); } catch (InvalidCipherTextException icte) { Logger.error(this, "Failed to RSA encrypt hash: " + icte.getMessage()); icte.printStackTrace(); return false; } ByteArrayOutputStream bos = new ByteArrayOutputStream(); try { bos.write(rtsmessage.toString().getBytes()); bos.write(sig); } catch (IOException ioe) { ioe.printStackTrace(); return false; } // make up a symmetric key PaddedBufferedBlockCipher aescipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()), new PKCS7Padding()); // quick paranoia check! if (aescipher.getBlockSize() != AES_BLOCK_LENGTH) { // bouncycastle must have changed their implementation, so // we're in trouble Logger.normal(this, "Incompatible block size change detected in cryptography API! Are you using a newer version of the bouncycastle libraries? If so, we suggest you downgrade for now, or check for a newer version of Freemail."); return false; } byte[] aes_iv_and_key = this.getAESParams(); // now encrypt that with our recipient's public key AsymmetricBlockCipher enccipher = new RSAEngine(); enccipher.init(true, their_pub_key); byte[] encrypted_aes_params = null; try { encrypted_aes_params = enccipher.processBlock(aes_iv_and_key, 0, aes_iv_and_key.length); } catch (InvalidCipherTextException icte) { Logger.error(this, "Failed to perform asymmertic encryption on RTS symmetric key: " + icte.getMessage()); icte.printStackTrace(); return false; } // now encrypt the message with the symmetric key KeyParameter kp = new KeyParameter(aes_iv_and_key, aescipher.getBlockSize(), AES_KEY_LENGTH); ParametersWithIV kpiv = new ParametersWithIV(kp, aes_iv_and_key, 0, aescipher.getBlockSize()); aescipher.init(true, kpiv); byte[] encmsg = new byte[aescipher.getOutputSize(bos.toByteArray().length) + encrypted_aes_params.length]; System.arraycopy(encrypted_aes_params, 0, encmsg, 0, encrypted_aes_params.length); int offset = encrypted_aes_params.length; offset += aescipher.processBytes(bos.toByteArray(), 0, bos.toByteArray().length, encmsg, offset); try { aescipher.doFinal(encmsg, offset); } catch (InvalidCipherTextException icte) { Logger.error(this, "Failed to perform symmertic encryption on RTS data: " + icte.getMessage()); icte.printStackTrace(); return false; } // insert it! HighLevelFCPClient cli = new HighLevelFCPClient(); if (cli.slotInsert(encmsg, "KSK@" + rtsksk + "-" + DateStringFactory.getKeyString(), 1, "") < 0) { // safe to copy the message into the contact outbox though return false; } // remember the fact that we have successfully inserted the rts this.contactfile.put("status", "rts-sent"); // and remember when we sent it! this.contactfile.put("rts-sent-at", Long.toString(System.currentTimeMillis())); // and since that's been successfully inserted to that key, we can // throw away the symmetric key this.contactfile.remove("aesparams"); Logger.normal(this, "Succesfully initialised Outbound Contact"); return true; }
From source file:freemail.OutboundContact.java
License:Open Source License
private String popNextSlot() { String slot = this.contactfile.get("nextslot"); if (slot == null) { return generateFirstSlot(); }// www. j a v a 2 s . c o m SHA256Digest sha256 = new SHA256Digest(); sha256.update(Base32.decode(slot), 0, Base32.decode(slot).length); byte[] nextslot = new byte[sha256.getDigestSize()]; sha256.doFinal(nextslot, 0); this.contactfile.put("nextslot", Base32.encode(nextslot)); return slot; }