Java Code Examples for org.bouncycastle.crypto.Digest#update()

The following examples show how to use org.bouncycastle.crypto.Digest#update() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RFC6637KDF.java    From InflatableDonkey with MIT License 6 votes vote down vote up
public byte[] apply(ECPoint S, byte[] fingerprint) throws IOException {
    // RFC Sections 7, 8
    byte[] ZB = S.getAffineXCoord().getEncoded();

    Digest digest = digestFactory.get();

    digest.update((byte) 0x00);                                     // 00
    digest.update((byte) 0x00);                                     // 00
    digest.update((byte) 0x00);                                     // 00 
    digest.update((byte) 0x01);                                     // 01 
    digest.update(ZB, 0, ZB.length);                                // ZB

    // Params
    digest.update(formattedOid, 0, formattedOid.length);            // curve_OID_len || curve_OID 
    digest.update(publicKeyAlgID);                                  // public_key_alg_ID
    digest.update((byte) 0x03);                                     // 03
    digest.update((byte) 0x01);                                     // 01
    digest.update(kdfHashID);                                       // KDF_hash_ID
    digest.update(symAlgID);                                        // KEK_alg_ID for AESKeyWrap
    digest.update(ANONYMOUS_SENDER, 0, ANONYMOUS_SENDER.length);    // "Anonymous Sender    "
    digest.update(fingerprint, 0, fingerprint.length);              // recipient_fingerprint

    byte[] hash = new byte[digest.getDigestSize()];
    digest.doFinal(hash, 0);
    return hash;
}
 
Example 2
Source File: KeySet.java    From InflatableDonkey with MIT License 6 votes vote down vote up
byte[] calculateChecksum() {
    try {
        // Re-encode the data minus the supplied checksum then calculate SHA256 hash.
        // This should ideally match the supplied checksum.
        // Verifies data integrity AND our decode/ encode processes.
        byte[] contents = toASN1Primitive(false).getEncoded();

        Digest digest = DIGEST.get();
        byte[] calculatedChecksum = new byte[digest.getDigestSize()];
        digest.update(contents, 0, contents.length);
        digest.doFinal(calculatedChecksum, 0);

        return calculatedChecksum;

    } catch (IOException ex) {
        throw new IllegalArgumentException(ex);
    }
}
 
Example 3
Source File: Endpoint.java    From fabric-sdk-java with Apache License 2.0 6 votes vote down vote up
byte[] getClientTLSCertificateDigest() {
    //The digest must be SHA256 over the DER encoded certificate. The PEM has the exact DER sequence in hex encoding around the begin and end markers

    if (tlsClientCertificatePEMBytes != null && clientTLSCertificateDigest == null) {

        String pemCert = new String(tlsClientCertificatePEMBytes, UTF_8);
        byte[] derBytes = Base64.getDecoder().decode(
                pemCert.replaceAll("-+[ \t]*(BEGIN|END)[ \t]+CERTIFICATE[ \t]*-+", "").replaceAll("\\s", "").trim()
        );

        Digest digest = new SHA256Digest();
        clientTLSCertificateDigest = new byte[digest.getDigestSize()];
        digest.update(derBytes, 0, derBytes.length);
        digest.doFinal(clientTLSCertificateDigest, 0);
    }

    return clientTLSCertificateDigest;
}
 
Example 4
Source File: Sha3Hash.java    From nuls-v2 with MIT License 6 votes vote down vote up
public static String sha3(byte[] bytes, int bitLength) {
    Digest digest = new SHA3Digest(bitLength);
    digest.update(bytes, 0, bytes.length);
    byte[] rsData = new byte[digest.getDigestSize()];
    digest.doFinal(rsData, 0);
    return HexUtil.encode(rsData);
}
 
Example 5
Source File: SHATest.java    From java_security with MIT License 5 votes vote down vote up
public static void bcSHA224()
{
	
	Digest digest = new SHA224Digest();
	digest.update(src.getBytes(), 0, src.getBytes().length );
	byte[] sha224Bytes = new byte[digest.getDigestSize()];
	digest.doFinal(sha224Bytes, 0);
	System.out.println("bc sha-224:" + org.bouncycastle.util.encoders.Hex.toHexString(sha224Bytes));		
}
 
Example 6
Source File: SRPAssistant.java    From InflatableDonkey with MIT License 5 votes vote down vote up
static byte[] hash(Digest digest, byte[]... bytes) {
    for (byte[] b : bytes) {
        digest.update(b, 0, b.length);
    }

    byte[] output = new byte[digest.getDigestSize()];
    digest.doFinal(output, 0);
    return output;
}
 
Example 7
Source File: KeyID.java    From InflatableDonkey with MIT License 5 votes vote down vote up
static byte[] id(byte[] data) {
    // SHA256 truncated to 20 bytes. 
    Digest digest = new SHA256Digest();
    byte[] out = new byte[digest.getDigestSize()];

    digest.update(data, 0, data.length);
    digest.doFinal(out, 0);

    return Arrays.copyOf(out, 20);
}
 
Example 8
Source File: HashCalculator.java    From xipki with Apache License 2.0 5 votes vote down vote up
public static byte[] hash(HashAlgo hashAlgo, byte[] data, int offset, int len) {
  Args.notNull(hashAlgo, "hashAlgo");
  Args.notNull(data, "data");

  if (data.length - offset < len) {
    throw new IndexOutOfBoundsException("data.length - offset < len");
  }

  if (!MDS_MAP.containsKey(hashAlgo)) {
    throw new IllegalArgumentException("unknown hash algo " + hashAlgo);
  }

  ConcurrentBag<ConcurrentBagEntry<Digest>> mds = MDS_MAP.get(hashAlgo);

  ConcurrentBagEntry<Digest> md0 = null;
  for (int i = 0; i < 3; i++) {
    try {
      md0 = mds.borrow(10, TimeUnit.SECONDS);
      break;
    } catch (InterruptedException ex) { // CHECKSTYLE:SKIP
    }
  }

  if (md0 == null) {
    throw new RuntimeOperatorException("could not get idle MessageDigest");
  }

  try {
    Digest md = md0.value();
    md.reset();
    md.update(data, offset, len);
    byte[] bytes = new byte[md.getDigestSize()];
    md.doFinal(bytes, 0);
    return bytes;
  } finally {
    mds.requite(md0);
  }
}
 
Example 9
Source File: KeccakHash.java    From nuls-v2 with MIT License 5 votes vote down vote up
public static byte[] keccakBytes(byte[] bytes, int bitLength) {
    Digest digest = new KeccakDigest(bitLength);
    digest.update(bytes, 0, bytes.length);
    byte[] rsData = new byte[digest.getDigestSize()];
    digest.doFinal(rsData, 0);
    return rsData;
}
 
Example 10
Source File: KeccakHash.java    From nuls-v2 with MIT License 5 votes vote down vote up
public static String keccak(byte[] bytes, int bitLength) {
    Digest digest = new KeccakDigest(bitLength);
    digest.update(bytes, 0, bytes.length);
    byte[] rsData = new byte[digest.getDigestSize()];
    digest.doFinal(rsData, 0);
    return HexUtil.encode(rsData);
}
 
Example 11
Source File: ResponseCacher.java    From xipki with Apache License 2.0 5 votes vote down vote up
private long deriveId(int issuerId, byte[] identBytes) {
  ConcurrentBagEntry<Digest> digest0 = null;
  try {
    digest0 = idDigesters.borrow(2, TimeUnit.SECONDS);
  } catch (InterruptedException ex) {
    // do nothing
  }

  boolean newDigest = (digest0 == null);
  if (newDigest) {
    digest0 = new ConcurrentBagEntry<Digest>(HashAlgo.SHA1.createDigest());
  }

  byte[] hash = new byte[20];
  try {
    Digest digest = digest0.value();
    digest.reset();
    digest.update(int2Bytes(issuerId), 0, 2);
    digest.update(identBytes, 0, identBytes.length);
    digest.doFinal(hash, 0);
  } finally {
    if (newDigest) {
      idDigesters.add(digest0);
    } else {
      idDigesters.requite(digest0);
    }
  }

  return (0x7FL & hash[0]) << 56 // ignore the first bit
      | (0xFFL & hash[1]) << 48
      | (0xFFL & hash[2]) << 40
      | (0xFFL & hash[3]) << 32
      | (0xFFL & hash[4]) << 24
      | (0xFFL & hash[5]) << 16
      | (0xFFL & hash[6]) << 8
      | (0xFFL & hash[7]);
}
 
Example 12
Source File: Sha512Hash.java    From nuls-v2 with MIT License 5 votes vote down vote up
public static byte[] sha512(byte[] bytes) {
    Digest digest = new SHA512Digest();
    digest.update(bytes, 0, bytes.length);
    byte[] rsData = new byte[digest.getDigestSize()];
    digest.doFinal(rsData, 0);
    return rsData;
}
 
Example 13
Source File: Sha512Test.java    From nuls-v2 with MIT License 5 votes vote down vote up
@Test
public void test() {
    byte[] bytes = HexUtil.decode("92d1552a53f2b526895542131bc768eae406ece0b8f5437631d5b0cc750b89e6");
    Digest digest = new SHA512Digest();
    digest.update(bytes, 0, bytes.length);
    byte[] rsData = new byte[digest.getDigestSize()];
    digest.doFinal(rsData, 0);
    int[] result = uint8Array(rsData);
    System.out.println(HexUtil.encode(rsData));
    System.out.println(Arrays.toString(result));
    // sha512 hex string: fc72ef919a913dd93d4d83215c0db7a9895c82aee1987c2cefdf1911caee2a154039be04f02552cf6870f3aa0ada43af8c02b3d20e5db90c5ae2ea146f6824ab
}
 
Example 14
Source File: HashUtil.java    From nuls-v2 with MIT License 5 votes vote down vote up
/**
 * @param data - message to hash
 * @return - reipmd160 hash of the message
 */
public static byte[] ripemd160(byte[] data) {
    Digest digest = new RIPEMD160Digest();
    if (data != null) {
        byte[] resBuf = new byte[digest.getDigestSize()];
        digest.update(data, 0, data.length);
        digest.doFinal(resBuf, 0);
        return resBuf;
    }
    throw new NullPointerException("Can't hash a NULL value");
}
 
Example 15
Source File: HashCalculator.java    From xipki with Apache License 2.0 5 votes vote down vote up
public static byte[] hash(HashAlgo hashAlgo, byte[]... datas) {
  Args.notNull(hashAlgo, "hashAlgo");
  Args.notNull(datas, "datas");

  if (!MDS_MAP.containsKey(hashAlgo)) {
    throw new IllegalArgumentException("unknown hash algo " + hashAlgo);
  }

  ConcurrentBag<ConcurrentBagEntry<Digest>> mds = MDS_MAP.get(hashAlgo);

  ConcurrentBagEntry<Digest> md0 = null;
  for (int i = 0; i < 3; i++) {
    try {
      md0 = mds.borrow(10, TimeUnit.SECONDS);
      break;
    } catch (InterruptedException ex) { // CHECKSTYLE:SKIP
    }
  }

  if (md0 == null) {
    throw new RuntimeOperatorException("could not get idle MessageDigest");
  }

  try {
    Digest md = md0.value();
    md.reset();
    for (byte[] data : datas) {
      if (data != null && data.length > 0) {
        md.update(data, 0, data.length);
      }
    }
    byte[] bytes = new byte[md.getDigestSize()];
    md.doFinal(bytes, 0);
    return bytes;
  } finally {
    mds.requite(md0);
  }
}
 
Example 16
Source File: SM2Signer.java    From web3sdk with Apache License 2.0 4 votes vote down vote up
private void addUserID(Digest digest, byte[] userID) {
    int len = userID.length * 8;
    digest.update((byte) (len >> 8 & 0xFF));
    digest.update((byte) (len & 0xFF));
    digest.update(userID, 0, userID.length);
}
 
Example 17
Source File: SM2PreprocessSigner.java    From gmhelper with Apache License 2.0 4 votes vote down vote up
private void addUserID(Digest digest, byte[] userID) {
    int len = userID.length * 8;
    digest.update((byte) (len >> 8 & 0xFF));
    digest.update((byte) (len & 0xFF));
    digest.update(userID, 0, userID.length);
}
 
Example 18
Source File: HashMode.java    From Decoder-Improved with GNU General Public License v3.0 4 votes vote down vote up
@Override
public byte[] modifyBytes(byte[] input) {
    // Get the selected ByteModifier and use the modifyBytes method from their to update input.
    Digest digest = null;
    byte[] output;

    if (algoComboBox.getSelectedItem().equals("MD2")) {
        digest = new MD2Digest();
    } else if (algoComboBox.getSelectedItem().equals("MD4")) {
        digest = new MD4Digest();
    } else if (algoComboBox.getSelectedItem().equals("MD5")) {
        digest = new MD5Digest();
    } else if (algoComboBox.getSelectedItem().equals("Keccak")) {
        digest = new KeccakDigest(Integer.parseInt((String)keccakComboBox.getSelectedItem()));
    } else if (algoComboBox.getSelectedItem().equals("RIPEMD128")) {
        digest = new RIPEMD128Digest();
    } else if (algoComboBox.getSelectedItem().equals("RIPEMD160")) {
        digest = new RIPEMD160Digest();
    } else if (algoComboBox.getSelectedItem().equals("RIPEMD256")) {
        digest = new RIPEMD256Digest();
    }  else if (algoComboBox.getSelectedItem().equals("RIPEMD320")) {
        digest = new RIPEMD320Digest();
    } else if (algoComboBox.getSelectedItem().equals("SHA1")) {
        digest = new SHA1Digest();
    } else if (algoComboBox.getSelectedItem().equals("SHA224")) {
        digest = new SHA224Digest();
    } else if (algoComboBox.getSelectedItem().equals("SHA256")) {
        digest = new SHA256Digest();
    } else if (algoComboBox.getSelectedItem().equals("SHA384")) {
        digest = new SHA384Digest();
    } else if (algoComboBox.getSelectedItem().equals("SHA512")) {
        digest = new SHA512Digest();
    } else if (algoComboBox.getSelectedItem().equals("SHA3")) {
        digest = new SHA3Digest(Integer.parseInt((String)sha3ComboBox.getSelectedItem()));
    } else if (algoComboBox.getSelectedItem().equals("SHAKE")) {
        digest = new SHAKEDigest(Integer.parseInt((String)shakeComboBox.getSelectedItem()));
    } else if (algoComboBox.getSelectedItem().equals("SM3")) {
        digest = new SM3Digest();
    } else if (algoComboBox.getSelectedItem().equals("Tiger")) {
        digest = new TigerDigest();
    } else if (algoComboBox.getSelectedItem().equals("GOST3411")) {
        digest = new GOST3411Digest();
    } else if (algoComboBox.getSelectedItem().equals("Whirlpool")) {
        digest = new WhirlpoolDigest();
    } else {
        throw new IllegalArgumentException("No such digest");
    }
    output = new byte[digest.getDigestSize()];

    // KeccakDigest	224, 256, 288, 384, 512
    // SHA3Digest	224, 256, 384, 512
    // SHAKEDigest	128, 256

    digest.reset();
    digest.update(input, 0, input.length);
    digest.doFinal(output, 0);
    return output;
}
 
Example 19
Source File: GMUtil.java    From xipki with Apache License 2.0 4 votes vote down vote up
private static void addFieldElement(Digest digest, ECFieldElement element) {
  byte[] encoded = element.getEncoded();
  digest.update(encoded, 0, encoded.length);
}
 
Example 20
Source File: Utils.java    From fabric-sdk-java with Apache License 2.0 3 votes vote down vote up
/**
 * Generate hash of the given input using the given Digest.
 *
 * @param input  input data.
 * @param digest the digest to use for hashing
 * @return hashed data.
 */
public static byte[] hash(byte[] input, Digest digest) {
    byte[] retValue = new byte[digest.getDigestSize()];
    digest.update(input, 0, input.length);
    digest.doFinal(retValue, 0);
    return retValue;
}