List of usage examples for org.bouncycastle.crypto.digests SHA1Digest reset
public void reset()
From source file:frost.crypt.FrostCrypt.java
License:Open Source License
/** * Computes the SHA-1 checksum of given message. *//* w ww . j a v a2s. c o m*/ public synchronized String digest(String message) { try { SHA1Digest stomach = new SHA1Digest(); stomach.reset(); byte[] food = message.getBytes("UTF-8"); stomach.update(food, 0, food.length); byte[] poop = new byte[64]; stomach.doFinal(poop, 0); return (new String(Base64.encode(poop))).substring(0, 27); } catch (UnsupportedEncodingException ex) { Logger.error(this, "UTF-8 encoding is not supported : " + ex.toString()); } return null; }
From source file:TorJava.Common.Encryption.java
License:Open Source License
/** * returns the hash of the input/*ww w. jav a 2s. c o m*/ * * */ public static byte[] getHash(byte[] input) { SHA1Digest sha1 = new SHA1Digest(); sha1.reset(); sha1.update(input, 0, input.length); byte[] hash = new byte[sha1.getDigestSize()]; sha1.doFinal(hash, 0); return hash; }
From source file:TorJava.Node.java
License:Open Source License
/** constructor for server-side. */ Node(Server init, byte[] dh_x_bytes) { if (init == null) throw new NullPointerException("can't init node on NULL server"); // save a pointer to the server's data this.server = init; Random rnd = new Random(); // do Diffie-Hellmann dh_x = new BigInteger(1, dh_x_bytes); dh_private = new BigInteger(dh_p.bitLength() - 1, rnd); BigInteger dh_xy = dh_x.modPow(dh_private, dh_p); byte[] dh_xy_bytes = BigIntegerTo128Bytes(dh_xy); // return dh_y-Bytes BigInteger dh_y = dh_g.modPow(dh_private, dh_p); dh_y_bytes = BigIntegerTo128Bytes(dh_y); // derive key-material SHA1Digest sha1 = new SHA1Digest(); byte[] k = new byte[100]; byte[] sha1_input = new byte[dh_xy_bytes.length + 1]; System.arraycopy(dh_xy_bytes, 0, sha1_input, 0, dh_xy_bytes.length); for (int i = 0; i < 5; ++i) { sha1.reset(); sha1_input[sha1_input.length - 1] = (byte) i; sha1.update(sha1_input, 0, sha1_input.length); sha1.doFinal(k, i * 20);/*from ww w. jav a2s.c o m*/ } ; // DEBUGGING OUTPUT -- BEGIN Logger.logCrypto(Logger.VERBOSE, "Node.<init>: dh_x = \n" + Encoding.toHexString(dh_x_bytes, 100) + "\n" + "dh_y = \n" + Encoding.toHexString(dh_y_bytes, 100) + "\n" + "dh_xy = keymaterial:\n" + Encoding.toHexString(dh_xy_bytes, 100) + "\n" + "Key Data:\n" + Encoding.toHexString(k, 100)); // DEBUGGING OUTPUT -- END // derived key info is correct - save to final destination // handshake kh = new byte[20]; System.arraycopy(k, 0, kh, 0, 20); // forward digest forward_digest = new byte[20]; System.arraycopy(k, 40, forward_digest, 0, 20); sha1_forward = new SHA1Digest(); sha1_forward.update(forward_digest, 0, 20); // backward digest backward_digest = new byte[20]; System.arraycopy(k, 20, backward_digest, 0, 20); sha1_backward = new SHA1Digest(); sha1_backward.update(backward_digest, 0, 20); // secret key for sending data kf = new byte[16]; System.arraycopy(k, 76, kf, 0, 16); aes_encrypt = new AESCounterMode(true, kf); // secret key for receiving data kb = new byte[16]; System.arraycopy(k, 60, kb, 0, 16); aes_decrypt = new AESCounterMode(true, kb); }
From source file:TorJava.Node.java
License:Open Source License
/** * called after receiving created or extended cell: finished DH-key * exchange. Expects the first 148 bytes of the data array to be filled * with:<br>//from w ww .j a v a 2 s .c o m * <ul> * <li>128 bytes of DH-data (g^y) * <li>20 bytes of derivated key data (KH) (see chapter 4.2 of torspec) * </ul> * * @param data * expects the received second half of the DH-key exchange */ void finish_dh(byte[] data) throws TorException { // calculate g^xy // - fix some undocument stuff: all numbers are 128-bytes only! // - add a leading zero to all numbers dh_y_bytes = new byte[128]; System.arraycopy(data, 0, dh_y_bytes, 0, 128); BigInteger dh_y = new BigInteger(1, dh_y_bytes); BigInteger dh_xy = dh_y.modPow(dh_private, dh_p); byte[] dh_xy_bytes = BigIntegerTo128Bytes(dh_xy); // derivate key material SHA1Digest sha1 = new SHA1Digest(); byte[] k = new byte[100]; byte[] sha1_input = new byte[dh_xy_bytes.length + 1]; System.arraycopy(dh_xy_bytes, 0, sha1_input, 0, dh_xy_bytes.length); for (int i = 0; i < 5; ++i) { sha1.reset(); sha1_input[sha1_input.length - 1] = (byte) i; sha1.update(sha1_input, 0, sha1_input.length); sha1.doFinal(k, i * 20); } ; // DEBUGGING OUTPUT -- BEGIN Logger.logCrypto(Logger.VERBOSE, "Node.finish_dh: dh_x = \n" + Encoding.toHexString(dh_x_bytes, 100) + "\n" + "dh_y = \n" + Encoding.toHexString(dh_y_bytes, 100) + "\n" + "dh_xy = keymaterial:\n" + Encoding.toHexString(dh_xy_bytes, 100) + "\n" + "Key Data:\n" + Encoding.toHexString(k, 100) + "\n" + "Data:\n" + Encoding.toHexString(data, 100)); // DEBUGGING OUTPUT -- END // check if derived key data is equal to bytes 128-147 of data[] boolean equal = true; for (int i = 0; equal && (i < 20); ++i) equal = (k[i] == data[128 + i]); // is there some error in the key data? if (!equal) throw new TorException("derived key material is wrong!"); // derived key info is correct - save to final destination // handshake kh = new byte[20]; System.arraycopy(k, 0, kh, 0, 20); // forward digest forward_digest = new byte[20]; System.arraycopy(k, 20, forward_digest, 0, 20); sha1_forward = new SHA1Digest(); sha1_forward.update(forward_digest, 0, 20); // backward digest backward_digest = new byte[20]; System.arraycopy(k, 40, backward_digest, 0, 20); sha1_backward = new SHA1Digest(); sha1_backward.update(backward_digest, 0, 20); // secret key for sending data kf = new byte[16]; System.arraycopy(k, 60, kf, 0, 16); aes_encrypt = new AESCounterMode(true, kf); // secret key for receiving data kb = new byte[16]; System.arraycopy(k, 76, kb, 0, 16); aes_decrypt = new AESCounterMode(true, kb); }