Home | History | Annotate | Download | only in x9
      1 package org.bouncycastle.asn1.x9;
      2 
      3 import org.bouncycastle.math.ec.ECCurve;
      4 import org.bouncycastle.math.ec.ECFieldElement;
      5 
      6 import java.math.BigInteger;
      7 
      8 public class X9IntegerConverter
      9 {
     10     public int getByteLength(
     11         ECCurve c)
     12     {
     13         return (c.getFieldSize() + 7) / 8;
     14     }
     15 
     16     public int getByteLength(
     17         ECFieldElement fe)
     18     {
     19         return (fe.getFieldSize() + 7) / 8;
     20     }
     21 
     22     public byte[] integerToBytes(
     23         BigInteger s,
     24         int        qLength)
     25     {
     26         byte[] bytes = s.toByteArray();
     27 
     28         if (qLength < bytes.length)
     29         {
     30             byte[] tmp = new byte[qLength];
     31 
     32             System.arraycopy(bytes, bytes.length - tmp.length, tmp, 0, tmp.length);
     33 
     34             return tmp;
     35         }
     36         else if (qLength > bytes.length)
     37         {
     38             byte[] tmp = new byte[qLength];
     39 
     40             System.arraycopy(bytes, 0, tmp, tmp.length - bytes.length, bytes.length);
     41 
     42             return tmp;
     43         }
     44 
     45         return bytes;
     46     }
     47 }
     48