Example usage for org.bouncycastle.crypto.util Pack bigEndianToLong

List of usage examples for org.bouncycastle.crypto.util Pack bigEndianToLong

Introduction

In this page you can find the example usage for org.bouncycastle.crypto.util Pack bigEndianToLong.

Prototype

public static long bigEndianToLong(byte[] bs, int off) 

Source Link

Usage

From source file:com.licel.jcardsim.crypto.MessageDigestImpl.java

License:Apache License

public void setInitialDigest(byte[] initialDigestBuf, short initialDigestOffset, short initialDigestLength,
        byte[] digestedMsgLenBuf, short digestedMsgLenOffset, short digestedMsgLenLength)
        throws CryptoException {
    // initialDigestLength must be == DIGEST_SIZE
    if (engine.getDigestSize() != initialDigestLength) {
        CryptoException.throwIt(CryptoException.ILLEGAL_VALUE);
    }/*from   ww w .  j a  v  a2s  .c  om*/
    // digestedMsgLenLength must be > 0 and < long value, more formal 2^64-1 bits
    // TODO support length more 2^128-1 bits (SHA-384, SHA-512) 
    if (digestedMsgLenLength == 0 || digestedMsgLenLength > 8) {
        CryptoException.throwIt(CryptoException.ILLEGAL_VALUE);
    }
    long byteCount = 0;
    for (short i = 0; i < digestedMsgLenLength; i++) {
        byteCount = (byteCount << 8) + (digestedMsgLenBuf[digestedMsgLenOffset + i] & 0xff);
    }
    // byte count % block size must be == 0
    if (byteCount % blockSize != 0) {
        CryptoException.throwIt(CryptoException.ILLEGAL_VALUE);
    }
    // set hash state - BouncyCastle specific
    try {
        for (byte i = 0; i < componentCount; i++) {
            // some reflection work
            Field h = digestClass.getDeclaredField("H" + (i + componentStartIdx));
            h.setAccessible(true);
            if (componentSize == 4) {
                h.setInt(engine,
                        Pack.bigEndianToInt(initialDigestBuf, initialDigestOffset + i * componentSize));
            } else {
                h.setLong(engine,
                        Pack.bigEndianToLong(initialDigestBuf, initialDigestOffset + i * componentSize));
            }
        }
        // set byteCount
        Field h = digestClass.getSuperclass().getDeclaredField(byteCountFieldName);
        h.setAccessible(true);
        h.setLong(engine, byteCount);
    } catch (Exception e) {
        CryptoException.throwIt(CryptoException.ILLEGAL_USE);
    }
}