Example usage for org.bouncycastle.crypto.digests SHA256Digest SHA256Digest

List of usage examples for org.bouncycastle.crypto.digests SHA256Digest SHA256Digest

Introduction

In this page you can find the example usage for org.bouncycastle.crypto.digests SHA256Digest SHA256Digest.

Prototype

public SHA256Digest() 

Source Link

Document

Standard constructor

Usage

From source file:org.jitsi.bccontrib.prng.FortunaGenerator.java

License:Open Source License

public FortunaGenerator(byte[] seed) {
    generator = new Generator(new AESFastEngine(), new SHA256Digest());
    pools = new Digest[NUM_POOLS];
    for (int i = 0; i < NUM_POOLS; i++)
        pools[i] = new SHA256Digest();
    buffer = new byte[256];
    if (seed != null) {
        generator.init(seed);/*  w  w  w .jav  a  2 s  .c o  m*/
        fillBlock();
        initialized = true;
    }
}

From source file:org.jnotary.dvcs.SimpleRequestTest.java

License:Open Source License

@Test
public void ccpd() throws IOException {

    byte[] req_data = testData.getBytes();
    Digest digest = new SHA256Digest();
    digest.update(req_data, 0, req_data.length);

    byte[] digestData = new byte[digest.getDigestSize()];
    digest.doFinal(digestData, 0);/*from  ww  w .  j  a v  a  2 s. c  o m*/

    DigestInfo messageImprint = new DigestInfo(new AlgorithmIdentifier(NISTObjectIdentifiers.id_sha256),
            digestData);
    Data data = new Data(messageImprint);

    DVCSRequestInformation requestInformation = new DVCSRequestInformation(ServiceType.CCPD);

    requestInformation.setNonce(new DERInteger(random.nextLong()));

    DVCSTime requestTime = new DVCSTime(new DERGeneralizedTime(new java.util.Date()));
    requestInformation.setRequestTime(requestTime);

    DVCSRequest reqOut = new DVCSRequest(requestInformation, data);

    DVCSRequest reqIn = DVCSRequest.getInstance(reqOut.getEncoded());
    assertTrue("Service type is incorrect", reqIn.getRequestInformation().getService() == ServiceType.CCPD);
    assertTrue("Nonce is incorrect",
            reqIn.getRequestInformation().getNonce().equals(reqOut.getRequestInformation().getNonce()));
    assertTrue("Request Time is incorrect", reqIn.getRequestInformation().getRequestTime()
            .equals(reqOut.getRequestInformation().getRequestTime()));
    assertTrue("Digest alg is incorrect", reqIn.getData().getMessageImprint().getAlgorithmId()
            .equals(reqOut.getData().getMessageImprint().getAlgorithmId()));
    assertTrue("Digest value is incorrect", Arrays.equals(reqIn.getData().getMessageImprint().getDigest(),
            reqOut.getData().getMessageImprint().getDigest()));
}

From source file:org.jnotary.dvcs.SimpleResponseTest.java

License:Open Source License

@Test
public void allGoodResponses() throws IOException {

    DVCSRequestInformation requestInformation = new DVCSRequestInformation(ServiceType.CPD);
    requestInformation.setNonce(new DERInteger(random.nextLong()));
    DVCSTime requestTime = new DVCSTime(new DERGeneralizedTime(new java.util.Date()));
    requestInformation.setRequestTime(requestTime);

    byte[] req_data = testData.getBytes();
    Digest digest = new SHA256Digest();
    digest.update(req_data, 0, req_data.length);
    byte[] digestData = new byte[digest.getDigestSize()];
    digest.doFinal(digestData, 0);/*from ww  w.ja v  a2s  .co  m*/
    DigestInfo messageImprint = new DigestInfo(new AlgorithmIdentifier(NISTObjectIdentifiers.id_sha256),
            digestData);

    DERInteger serialNumber = new DERInteger(random.nextLong());
    DVCSTime responseTime = new DVCSTime(new DERGeneralizedTime(new java.util.Date()));

    DVCSCertInfo dvCertInfo = new DVCSCertInfo(requestInformation, messageImprint, serialNumber, responseTime);
    DVCSResponse respOut = new DVCSResponse(dvCertInfo);

    DVCSResponse respIn = DVCSResponse.getInstance(respOut.getEncoded());
    assertTrue("Service type is incorrect",
            respIn.getDvCertInfo().getRequestInformation().getService() == ServiceType.CPD);
    assertTrue("Nonce is incorrect", respIn.getDvCertInfo().getRequestInformation().getNonce()
            .equals(respOut.getDvCertInfo().getRequestInformation().getNonce()));
    assertTrue("Request Time is incorrect", respIn.getDvCertInfo().getRequestInformation().getRequestTime()
            .equals(respOut.getDvCertInfo().getRequestInformation().getRequestTime()));
    assertTrue("Message imprint is incorrect",
            respIn.getDvCertInfo().getMessageImprint().equals(respOut.getDvCertInfo().getMessageImprint()));
    assertTrue("Serial number is incorrect",
            respIn.getDvCertInfo().getSerialNumber().equals(respOut.getDvCertInfo().getSerialNumber()));
    assertTrue("Response time is incorrect",
            respIn.getDvCertInfo().getResponseTime().equals(respOut.getDvCertInfo().getResponseTime()));
}

From source file:org.opcfoundation.ua.transport.security.BcCryptoProvider.java

License:Open Source License

private HMac createMac(SecurityAlgorithm algorithm, KeyParameter param) throws ServiceResultException {

    HMac hmac = null;//from w w w  .  j a va 2  s .  c o  m
    if (algorithm.equals(SecurityAlgorithm.HmacSha1)) {
        hmac = new HMac(new SHA1Digest());
    } else if (algorithm.equals(SecurityAlgorithm.HmacSha256)) {
        hmac = new HMac(new SHA256Digest());
    } else {
        throw new ServiceResultException(StatusCodes.Bad_SecurityPolicyRejected,
                "Unsupported symmetric signature algorithm: " + algorithm);
    }
    hmac.init(param);
    return hmac;

}

From source file:org.opcfoundation.ua.transport.security.BcCryptoProvider.java

License:Open Source License

private Signer getAsymmetricSigner(boolean forSigning, SecurityAlgorithm algorithm, CipherParameters params)
        throws ServiceResultException {

    Signer signer = null;/*from   ww  w. ja va2 s . co m*/
    if (algorithm.equals(SecurityAlgorithm.RsaSha1)) {
        signer = new RSADigestSigner(new SHA1Digest());
    } else if (algorithm.equals(SecurityAlgorithm.RsaSha256)) {
        signer = new RSADigestSigner(new SHA256Digest());
    } else {
        throw new ServiceResultException(StatusCodes.Bad_SecurityPolicyRejected,
                "Unsupported asymmetric signature algorithm: " + algorithm);
    }
    signer.init(forSigning, params);
    return signer;

}

From source file:org.opendaylight.capwap.dtls.DtlsUtils.java

License:Open Source License

static byte[] sha256DigestOf(byte[] input) {
    SHA256Digest d = new SHA256Digest();
    d.update(input, 0, input.length);//from w  w  w  .  j  a v a2s .c o  m
    byte[] result = new byte[d.getDigestSize()];
    d.doFinal(result, 0);
    return result;
}

From source file:org.panbox.core.crypto.io.AuthTagVerifier.java

License:Open Source License

/**
 * @param/*from ww w.  jav  a  2  s . co  m*/
 */
public AuthTagVerifier(EncRandomAccessFile encRandomAccessFile) {
    this.backEncRandomAccessFile = encRandomAccessFile;
    this.CHUNK_AUTH_TAG_LENGTH = encRandomAccessFile.CHUNK_TLEN;
    this.authTagHMac = new HMac(new SHA256Digest());
    // KeyParameter keyParame = new
    // KeyParameter(backEncRandomAccessFile.secretKey.)
    // authTagHMac.init(null);

    this.atagList = new ArrayList<byte[]>();
}

From source file:org.panbox.core.crypto.io.AuthTagVerifierTest.java

License:Open Source License

/**
 * Test method for//  w  ww . j  a v a 2 s.  co m
 * {@link org.panbox.core.crypto.io.AuthTagVerifier#updateFileAuthTag()}.
 * 
 * @throws FileEncryptionException
 */
@Test
public void testUpdateFileAuthTag() throws Exception {
    byte[] testTag = new byte[TAGLEN];
    byte[] referenceFileTag = new byte[testVerifier.authTagHMac.getMacSize()];
    HMac refHMac = testVerifier.authTagHMac.getClass().getConstructor(Digest.class)
            .newInstance(new SHA256Digest());

    refHMac.init(new KeyParameter(testKey.getEncoded()));

    // exception should be thrown if AuTagVerifier-HMac cannot be properly
    // initialized
    aesTestFile.shareKey = null;
    try {
        testVerifier.updateFileAuthTag();
        fail("Expected exception for invalid file encryption key!");
    } catch (FileEncryptionException e) {
        assertEquals("Invalid file encryption key in encrypting random access file!", e.getMessage());
    }
    // restore key
    aesTestFile.shareKey = testKey;

    // test if exception is thrown upon file auth tag update without chunk
    // auth tags
    try {
        testVerifier.updateFileAuthTag();
        fail("Expected exception as no chunk auth tags have been set!");
    } catch (FileEncryptionException e) {
        assertEquals("No chunk authentication tags have been set yet!", e.getMessage());
    }

    Arrays.fill(testTag, (byte) 0x41);
    refHMac.update(testTag, 0, TAGLEN);
    testVerifier.insertChunkAuthTag(0, testTag);
    // skip one chunk
    Arrays.fill(testTag, (byte) 0x42);
    refHMac.update(testTag, 0, TAGLEN);
    testVerifier.insertChunkAuthTag(2, testTag);
    try {
        testVerifier.updateFileAuthTag();
        fail("Missing authentication tag should cause an exception");
    } catch (FileEncryptionException e) {
        assertEquals("Invalid chunk authentication tag in auth tag table at offset: 1", e.getMessage());
    }
    Arrays.fill(testTag, (byte) 0x43);
    refHMac.update(testTag, 0, TAGLEN);
    testVerifier.insertChunkAuthTag(1, testTag);
    assertNull(aesTestFile.readFileAuthenticationTag());
    refHMac.doFinal(referenceFileTag, 0);
    testVerifier.updateFileAuthTag();
    assertNotNull(aesTestFile.readFileAuthenticationTag());
    // should NOT be equal as update order for refHMac was 0 - 2 - 1
    assertFalse(Arrays.equals(referenceFileTag, aesTestFile.readFileAuthenticationTag()));

    // calculate actual value
    refHMac.reset();
    Arrays.fill(testTag, (byte) 0x41);
    refHMac.update(testTag, 0, TAGLEN);
    Arrays.fill(testTag, (byte) 0x43);
    refHMac.update(testTag, 0, TAGLEN);
    Arrays.fill(testTag, (byte) 0x42);
    refHMac.update(testTag, 0, TAGLEN);
    refHMac.doFinal(referenceFileTag, 0);

    assertArrayEquals(referenceFileTag, aesTestFile.readFileAuthenticationTag());
}

From source file:org.panbox.core.crypto.io.AuthTagVerifierTest.java

License:Open Source License

/**
 * Test method for//from w w  w .j av a2  s  . com
 * {@link org.panbox.core.crypto.io.AuthTagVerifier#verifyFileAuthTag()}.
 */
@Test
public void testVerifyFileAuthTag() throws Exception {
    // verification with empty file auth tag should cause exception
    assertNull(aesTestFile.readFileAuthenticationTag());
    try {
        testVerifier.verifyFileAuthTag();
        fail("Expected exception for invalid file authentication tag in encrypted file");
    } catch (Exception e) {
        assertEquals("Encrypted file has invalid file authentication tag!", e.getMessage());
    }
    // verification with file auth tag of invalid size should cause
    // exception
    aesTestFile.writeFileAuthenticationTag(new byte[] { 1, 2, 3, 4 });
    try {
        testVerifier.verifyFileAuthTag();
        fail("Expected exception for invalid file authentication tag in encrypted file");
    } catch (Exception e) {
        assertEquals("Encrypted file has invalid file authentication tag!", e.getMessage());
    }

    // insert 2 test chunks auth tags
    byte[] testTag1 = new byte[TAGLEN];
    byte[] testTag2 = new byte[TAGLEN];
    Arrays.fill(testTag1, (byte) 0x41);
    Arrays.fill(testTag2, (byte) 0x42);
    testVerifier.insertChunkAuthTag(0, testTag1);
    testVerifier.insertChunkAuthTag(1, testTag2);

    // create reference for calculating expected file auth tag
    // value
    HMac refHMac = testVerifier.authTagHMac.getClass().getConstructor(Digest.class)
            .newInstance(new SHA256Digest());
    refHMac.init(new KeyParameter(testKey.getEncoded()));

    refHMac.update(testTag1, 0, TAGLEN);
    refHMac.update(testTag2, 0, TAGLEN);

    byte[] refRootTag = new byte[refHMac.getMacSize()];
    refHMac.doFinal(refRootTag, 0);

    // first, test verification of valid file auth tag
    aesTestFile.writeFileAuthenticationTag(refRootTag);
    assertTrue(testVerifier.verifyFileAuthTag());

    // second, test verification for invalid file auth tag
    testVerifier.updateFileAuthTag();
    byte[] tmp = aesTestFile.readFileAuthenticationTag();

    // invalidate file auth tag
    Arrays.fill(tmp, 0, 4, (byte) 0x00);
    aesTestFile.writeFileAuthenticationTag(tmp);

    // check if verification fails for invalid file auth tag
    assertFalse(testVerifier.verifyFileAuthTag());
}

From source file:org.pwsafe.lib.crypto.HmacPws.java

License:Open Source License

public HmacPws(byte[] key) {
    mac = new HMac(new SHA256Digest());
    KeyParameter kp = new KeyParameter(key);
    mac.init(kp);
}