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

The following examples show how to use org.bouncycastle.crypto.Digest#getDigestSize() . 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: 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 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: 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 6
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 7
Source File: SHATest.java    From java_security with MIT License 5 votes vote down vote up
public static void bcSHA1()
{
	
	Digest digest = new SHA1Digest();
	digest.update(src.getBytes(), 0, src.getBytes().length );
	byte[] sha1Bytes = new byte[digest.getDigestSize()];
	digest.doFinal(sha1Bytes, 0);
	System.out.println("bc sha-1:" + org.bouncycastle.util.encoders.Hex.toHexString(sha1Bytes));		
}
 
Example 8
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 9
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 10
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 11
Source File: HMac.java    From google-authenticator with Apache License 2.0 5 votes vote down vote up
private HMac(
    Digest digest,
    int    byteLength)
{
    this.digest = digest;
    digestSize = digest.getDigestSize();

    this.blockLength = byteLength;

    inputPad = new byte[blockLength];
    outputPad = new byte[blockLength];
}
 
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: 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 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: 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 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;
}