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

The following examples show how to use org.bouncycastle.crypto.Digest#doFinal() . 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: 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 2
Source File: Sha3Hash.java    From nuls-v2 with MIT License 6 votes vote down vote up
public static byte[] sha3bytes(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 rsData;
}
 
Example 3
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 4
Source File: DIGEST.java    From warp10-platform with Apache License 2.0 6 votes vote down vote up
@Override
public Object apply(WarpScriptStack stack) throws WarpScriptException {
  Object o = stack.pop();

  if (!(o instanceof byte[])) {
    throw new WarpScriptException(getName() + " operates on a byte array.");
  }

  byte[] bytes = (byte[]) o;

  try {
    Digest digest = (Digest) digestAlgo.newInstance();

    byte[] digestOctets = new byte[digest.getDigestSize()];

    digest.update(bytes, 0, bytes.length);

    digest.doFinal(digestOctets, 0);

    stack.push(digestOctets);

    return stack;
  } catch (Exception exp) {
    throw new WarpScriptException(getName() + " unable to instantiate message digest.", exp);
  }
}
 
Example 5
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 6
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 7
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 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[]... 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 9
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 10
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 11
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 12
Source File: CryptoPrimitives.java    From fabric-sdk-java with Apache License 2.0 5 votes vote down vote up
@Override
public byte[] hash(byte[] input) {
    Digest digest = getHashDigest();
    byte[] retValue = new byte[digest.getDigestSize()];
    digest.update(input, 0, input.length);
    digest.doFinal(retValue, 0);
    return retValue;
}
 
Example 13
Source File: Hash.java    From WavesJ with MIT License 5 votes vote down vote up
private static byte[] hash(byte[] message, int ofs, int len, ThreadLocal<Digest> alg) {
    final Digest digest = digest(alg);
    final byte[] result = new byte[digest.getDigestSize()];
    digest.update(message, ofs, len);
    digest.doFinal(result, 0);
    return result;
}
 
Example 14
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 15
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 16
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 17
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 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: FileStreamWriter.java    From InflatableDonkey with MIT License 4 votes vote down vote up
static byte[] signature(Digest digest) {
    byte[] out = new byte[digest.getDigestSize()];
    digest.doFinal(out, 0);
    return out;
}
 
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;
}