Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/**
* (c) 2002-2021 JADAPTIVE Limited. All Rights Reserved.
*
* This file is part of the Maverick Synergy Java SSH API.
*
* Maverick Synergy is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Maverick Synergy is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Maverick Synergy. If not, see .
*/
package com.sshtools.common.ssh.components.jce;
import java.io.IOException;
import java.math.BigInteger;
import java.security.InvalidAlgorithmParameterException;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.interfaces.ECPrivateKey;
import java.security.interfaces.ECPublicKey;
import java.security.spec.ECPoint;
import java.security.spec.ECPrivateKeySpec;
import java.security.spec.EllipticCurve;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.X509EncodedKeySpec;
import com.sshtools.common.util.SimpleASNWriter;
public class ECUtils {
public static byte[] toByteArray(ECPoint e, EllipticCurve curve) {
byte[] x = e.getAffineX().toByteArray();
byte[] y = e.getAffineY().toByteArray();
int i, xoff = 0, yoff = 0;
for (i = 0; i < x.length - 1; i++)
if (x[i] != 0) {
xoff = i;
break;
}
for (i = 0; i < y.length - 1; i++)
if (y[i] != 0) {
yoff = i;
break;
}
int len = (curve.getField().getFieldSize() + 7) / 8;
if ((x.length - xoff) > len || (y.length - yoff) > len)
return null;
byte[] ret = new byte[len * 2 + 1];
ret[0] = 4;
System.arraycopy(x, xoff, ret, 1 + len - (x.length - xoff), x.length
- xoff);
System.arraycopy(y, yoff, ret, ret.length - (y.length - yoff), y.length
- yoff);
return ret;
}
public static ECPoint fromByteArray(byte[] b, EllipticCurve curve) {
int len = (curve.getField().getFieldSize() + 7) / 8;
if (b.length != 2 * len + 1 || b[0] != 4)
return null;
byte[] x = new byte[len];
byte[] y = new byte[len];
System.arraycopy(b, 1, x, 0, len);
System.arraycopy(b, len + 1, y, 0, len);
return new ECPoint(new BigInteger(1,x), new BigInteger(1,y));
}
public static byte[] ensureLeadingZero(byte[] x) {
if(x[0]!=0) {
byte[] tmp = new byte[x.length+1];
System.arraycopy(x, 0, tmp, 1, x.length);
return tmp;
}
return x;
}
public static String getNameFromEncodedKey(PrivateKey prv) {
byte[] encoded = prv.getEncoded();
byte[] secp256r1 = new byte[] { 0x2A, (byte) 0x86, 0x48, (byte) 0xCE, 0x3D, 0x03, 0x01, 0x07};
if(contains(encoded, secp256r1)) {
return "secp256r1";
}
byte[] secp384r1 = new byte[] { 0x2B, (byte) 0x81, 0x04, 0x00, 0x22};
if(contains(encoded, secp384r1)) {
return "secp384r1";
}
byte[] secp521r1 = new byte[] { 0x2B, (byte) 0x81, 0x04, 0x00, 0x23};
if(contains(encoded, secp521r1)) {
return "secp521r1";
}
throw new IllegalArgumentException("Unable to determine EC curve type.");
}
private static boolean contains(byte[] source, byte[] find) {
int i;
for(i=0;i