001package org.apache.commons.ssl.org.bouncycastle.asn1.x9;
002
003import java.math.BigInteger;
004
005import org.bouncycastle.math.ec.ECCurve;
006import org.bouncycastle.math.ec.ECFieldElement;
007
008public class X9IntegerConverter
009{
010    public int getByteLength(
011        ECCurve c)
012    {
013        return (c.getFieldSize() + 7) / 8;
014    }
015
016    public int getByteLength(
017        ECFieldElement fe)
018    {
019        return (fe.getFieldSize() + 7) / 8;
020    }
021
022    public byte[] integerToBytes(
023        BigInteger s,
024        int        qLength)
025    {
026        byte[] bytes = s.toByteArray();
027        
028        if (qLength < bytes.length)
029        {
030            byte[] tmp = new byte[qLength];
031        
032            System.arraycopy(bytes, bytes.length - tmp.length, tmp, 0, tmp.length);
033            
034            return tmp;
035        }
036        else if (qLength > bytes.length)
037        {
038            byte[] tmp = new byte[qLength];
039        
040            System.arraycopy(bytes, 0, tmp, tmp.length - bytes.length, bytes.length);
041            
042            return tmp; 
043        }
044    
045        return bytes;
046    }
047}