FileDocCategorySizeDatePackage
MGF1BytesGenerator.javaAPI DocAndroid 1.5 API2795Wed May 06 22:41:06 BST 2009org.bouncycastle.crypto.generators

MGF1BytesGenerator

public class MGF1BytesGenerator extends Object implements org.bouncycastle.crypto.DerivationFunction
Generator for MGF1 as defined in PKCS 1v2

Fields Summary
private org.bouncycastle.crypto.Digest
digest
private byte[]
seed
private int
hLen
Constructors Summary
public MGF1BytesGenerator(org.bouncycastle.crypto.Digest digest)

param
digest the digest to be used as the source of generated bytes

        this.digest = digest;
        this.hLen = digest.getDigestSize();
    
Methods Summary
private voidItoOSP(int i, byte[] sp)
int to octet string.

        sp[0] = (byte)(i >>> 24);
        sp[1] = (byte)(i >>> 16);
        sp[2] = (byte)(i >>> 8);
        sp[3] = (byte)(i >>> 0);
    
public intgenerateBytes(byte[] out, int outOff, int len)
fill len bytes of the output buffer with bytes generated from the derivation function.

throws
DataLengthException if the out buffer is too small.

        if ((out.length - len) < outOff)
        {
            throw new DataLengthException("output buffer too small");
        }
        
        byte[]  hashBuf = new byte[hLen];
        byte[]  C = new byte[4];
        int     counter = 0;

        digest.reset();

        if (len > hLen)
        {
            do
            {
                ItoOSP(counter, C);
    
                digest.update(seed, 0, seed.length);
                digest.update(C, 0, C.length);
                digest.doFinal(hashBuf, 0);
    
                System.arraycopy(hashBuf, 0, out, outOff + counter * hLen, hLen);
            }
            while (++counter < (len / hLen));
        }

        if ((counter * hLen) < len)
        {
            ItoOSP(counter, C);

            digest.update(seed, 0, seed.length);
            digest.update(C, 0, C.length);
            digest.doFinal(hashBuf, 0);

            System.arraycopy(hashBuf, 0, out, outOff + counter * hLen, len - (counter * hLen));
        }

        return len;
    
public org.bouncycastle.crypto.DigestgetDigest()
return the underlying digest.

        return digest;
    
public voidinit(org.bouncycastle.crypto.DerivationParameters param)

        if (!(param instanceof MGFParameters))
        {
            throw new IllegalArgumentException("MGF parameters required for MGF1Generator");
        }

        MGFParameters   p = (MGFParameters)param;

        seed = p.getSeed();