Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.bouncycastle.jcajce.provider.asymmetric.ecgost;
  
 
 
 public class BCECGOST3410PublicKey
 {
     static final long serialVersionUID = 7026240464295649314L;
 
     private String algorithm = "ECGOST3410";
     private boolean withCompression;
 
     private transient org.bouncycastle.math.ec.ECPoint q;
     private transient ECParameterSpec ecSpec;
     private transient GOST3410PublicKeyAlgParameters gostParams;
 
     public BCECGOST3410PublicKey(
         BCECGOST3410PublicKey key)
     {
         this. = key.q;
         this. = key.ecSpec;
         this. = key.withCompression;
         this. = key.gostParams;
     }
 
     public BCECGOST3410PublicKey(
         ECPublicKeySpec spec)
     {
         this. = spec.getParams();
         this. = EC5Util.convertPoint(spec.getW(), false);
     }
 
     public BCECGOST3410PublicKey(
         org.bouncycastle.jce.spec.ECPublicKeySpec spec)
     {
         this. = spec.getQ();
 
         if (spec.getParams() != null// can be null if implictlyCa
         {
             ECCurve curve = spec.getParams().getCurve();
             EllipticCurve ellipticCurve = EC5Util.convertCurve(curvespec.getParams().getSeed());
 
             this. = EC5Util.convertSpec(ellipticCurvespec.getParams());
         }
         else
         {
             if (.getCurve() == null)
             {
 
                  = s.getCurve().createPoint(.getAffineXCoord().toBigInteger(), .getAffineYCoord().toBigInteger());
             }
             this. = null;
         }
     }
 
     public BCECGOST3410PublicKey(
         String algorithm,
         ECPublicKeyParameters params,
         ECParameterSpec spec)
     {
         ECDomainParameters dp = params.getParameters();
 
         this. = algorithm;
         this. = params.getQ();
 
        if (spec == null)
        {
            EllipticCurve ellipticCurve = EC5Util.convertCurve(dp.getCurve(), dp.getSeed());
            this. = createSpec(ellipticCurvedp);
        }
        else
        {
            this. = spec;
        }
    }
    public BCECGOST3410PublicKey(
        String algorithm,
        ECPublicKeyParameters params,
        org.bouncycastle.jce.spec.ECParameterSpec spec)
    {
        ECDomainParameters dp = params.getParameters();
        this. = algorithm;
        this. = params.getQ();
        if (spec == null)
        {
            EllipticCurve ellipticCurve = EC5Util.convertCurve(dp.getCurve(), dp.getSeed());
            this. = createSpec(ellipticCurvedp);
        }
        else
        {
            EllipticCurve ellipticCurve = EC5Util.convertCurve(spec.getCurve(), spec.getSeed());
            this. = EC5Util.convertSpec(ellipticCurvespec);
        }
    }
    /*
     * called for implicitCA
     */
    public BCECGOST3410PublicKey(
        String algorithm,
        ECPublicKeyParameters params)
    {
        this. = algorithm;
        this. = params.getQ();
        this. = null;
    }
    private ECParameterSpec createSpec(EllipticCurve ellipticCurveECDomainParameters dp)
    {
        return new ECParameterSpec(
            ellipticCurve,
            new ECPoint(
                dp.getG().getAffineXCoord().toBigInteger(),
                dp.getG().getAffineYCoord().toBigInteger()),
            dp.getN(),
            dp.getH().intValue());
    }
    public BCECGOST3410PublicKey(
        ECPublicKey key)
    {
        this. = key.getAlgorithm();
        this. = key.getParams();
        this. = EC5Util.convertPoint(this.key.getW(), false);
    }
        SubjectPublicKeyInfo info)
    {
        populateFromPubKeyInfo(info);
    }
    private void populateFromPubKeyInfo(SubjectPublicKeyInfo info)
    {
        DERBitString bits = info.getPublicKeyData();
        ASN1OctetString key;
        this. = "ECGOST3410";
        try
        {
            key = (ASN1OctetString)ASN1Primitive.fromByteArray(bits.getBytes());
        }
        catch (IOException ex)
        {
            throw new IllegalArgumentException("error recovering public key");
        }
        byte[] keyEnc = key.getOctets();
        byte[] x = new byte[32];
        byte[] y = new byte[32];
        for (int i = 0; i != x.lengthi++)
        {
            x[i] = keyEnc[32 - 1 - i];
        }
        for (int i = 0; i != y.lengthi++)
        {
            y[i] = keyEnc[64 - 1 - i];
        }
         = GOST3410PublicKeyAlgParameters.getInstance(info.getAlgorithm().getParameters());
        ECNamedCurveParameterSpec spec = ECGOST3410NamedCurveTable.getParameterSpec(ECGOST3410NamedCurves.getName(.getPublicKeyParamSet()));
        ECCurve curve = spec.getCurve();
        EllipticCurve ellipticCurve = EC5Util.convertCurve(curvespec.getSeed());
        this. = curve.createPoint(new BigInteger(1, x), new BigInteger(1, y));
         = new ECNamedCurveSpec(
            ECGOST3410NamedCurves.getName(.getPublicKeyParamSet()),
            ellipticCurve,
            new ECPoint(
                spec.getG().getAffineXCoord().toBigInteger(),
                spec.getG().getAffineYCoord().toBigInteger()),
            spec.getN(), spec.getH());
    }
    public String getAlgorithm()
    {
        return ;
    }
    public String getFormat()
    {
        return "X.509";
    }
    public byte[] getEncoded()
    {
        ASN1Encodable params;
        SubjectPublicKeyInfo info;
        if ( != null)
        {
            params = ;
        }
        else
        {
            if ( instanceof ECNamedCurveSpec)
            {
                params = new GOST3410PublicKeyAlgParameters(
                    ECGOST3410NamedCurves.getOID(((ECNamedCurveSpec)).getName()),
                    .);
            }
            else
            {   // strictly speaking this may not be applicable...
                ECCurve curve = EC5Util.convertCurve(.getCurve());
                X9ECParameters ecP = new X9ECParameters(
                    curve,
                    EC5Util.convertPoint(curve.getGenerator(), ),
                    .getOrder(),
                    BigInteger.valueOf(.getCofactor()),
                    .getCurve().getSeed());
                params = new X962Parameters(ecP);
            }
        }
        BigInteger bX = this..getAffineXCoord().toBigInteger();
        BigInteger bY = this..getAffineYCoord().toBigInteger();
        byte[] encKey = new byte[64];
        extractBytes(encKey, 0, bX);
        extractBytes(encKey, 32, bY);
        try
        {
            info = new SubjectPublicKeyInfo(new AlgorithmIdentifier(.params), new DEROctetString(encKey));
        }
        catch (IOException e)
        {
            return null;
        }
        return KeyUtil.getEncodedSubjectPublicKeyInfo(info);
    }
    private void extractBytes(byte[] encKeyint offSetBigInteger bI)
    {
        byte[] val = bI.toByteArray();
        if (val.length < 32)
        {
            byte[] tmp = new byte[32];
            System.arraycopy(val, 0, tmptmp.length - val.lengthval.length);
            val = tmp;
        }
        for (int i = 0; i != 32; i++)
        {
            encKey[offSet + i] = val[val.length - 1 - i];
        }
    }
    public ECParameterSpec getParams()
    {
        return ;
    }
    {
        if ( == null)     // implictlyCA
        {
            return null;
        }
        return EC5Util.convertSpec();
    }
    public ECPoint getW()
    {
        return new ECPoint(.getAffineXCoord().toBigInteger(), .getAffineYCoord().toBigInteger());
    }
    public org.bouncycastle.math.ec.ECPoint getQ()
    {
        if ( == null)
        {
            return .getDetachedPoint();
        }
        return ;
    }
    {
        return ;
    }
    {
        if ( != null)
        {
            return EC5Util.convertSpec();
        }
    }
    public String toString()
    {
        StringBuffer buf = new StringBuffer();
        String nl = System.getProperty("line.separator");
        buf.append("EC Public Key").append(nl);
        buf.append("            X: ").append(this..getAffineXCoord().toBigInteger().toString(16)).append(nl);
        buf.append("            Y: ").append(this..getAffineYCoord().toBigInteger().toString(16)).append(nl);
        return buf.toString();
    }
    public void setPointFormat(String style)
    {
         = !("UNCOMPRESSED".equalsIgnoreCase(style));
    }
    public boolean equals(Object o)
    {
        if (!(o instanceof BCECGOST3410PublicKey))
        {
            return false;
        }
        BCECGOST3410PublicKey other = (BCECGOST3410PublicKey)o;
        return engineGetQ().equals(other.engineGetQ()) && (engineGetSpec().equals(other.engineGetSpec()));
    }
    public int hashCode()
    {
        return engineGetQ().hashCode() ^ engineGetSpec().hashCode();
    }
    private void readObject(
        ObjectInputStream in)
        throws IOExceptionClassNotFoundException
    {
        in.defaultReadObject();
        byte[] enc = (byte[])in.readObject();
        populateFromPubKeyInfo(SubjectPublicKeyInfo.getInstance(ASN1Primitive.fromByteArray(enc)));
    }
    private void writeObject(
        ObjectOutputStream out)
        throws IOException
    {
        out.defaultWriteObject();
        out.writeObject(this.getEncoded());
    }
    {
        return ;
    }
New to GrepCode? Check out our FAQ X