Example usage for org.bouncycastle.crypto.params X25519PublicKeyParameters getEncoded

List of usage examples for org.bouncycastle.crypto.params X25519PublicKeyParameters getEncoded

Introduction

In this page you can find the example usage for org.bouncycastle.crypto.params X25519PublicKeyParameters getEncoded.

Prototype

public byte[] getEncoded() 

Source Link

Usage

From source file:com.distrimind.util.crypto.ASymmetricEncryptionType.java

License:Open Source License

@SuppressWarnings("deprecation")
static byte[] encodePublicKey(PublicKey key, ASymmetricAuthenticatedSignatureType type, boolean xdh) {
    if (type == ASymmetricAuthenticatedSignatureType.BC_SHA384withECDSA_CURVE_25519
            || type == ASymmetricAuthenticatedSignatureType.BC_SHA512withECDSA_CURVE_25519
            || type == ASymmetricAuthenticatedSignatureType.BC_SHA256withECDSA_CURVE_25519) {
        return ((ECPublicKey) key).getQ().getEncoded(true);
    } else if (type == ASymmetricAuthenticatedSignatureType.BC_Ed25519) {
        if (xdh) {
            X25519PublicKeyParameters k = null;
            try {
                k = (X25519PublicKeyParameters) xdhPublicKeyField.get(key);
            } catch (IllegalAccessException e) {
                e.printStackTrace();//from w  ww .j  a  v  a 2 s .  c  om
                System.exit(-1);
            }
            return k.getEncoded();
        } else {
            Ed25519PublicKeyParameters k = null;
            try {
                k = (Ed25519PublicKeyParameters) eddsaPublicKeyField.get(key);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
                System.exit(-1);
            }
            return k.getEncoded();
        }
    } else if (type == ASymmetricAuthenticatedSignatureType.BC_Ed448) {
        if (xdh) {
            X448PublicKeyParameters k = null;
            try {
                k = (X448PublicKeyParameters) xdhPublicKeyField.get(key);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
                System.exit(-1);
            }
            return k.getEncoded();
        } else {

            Ed448PublicKeyParameters k = null;
            try {
                k = (Ed448PublicKeyParameters) eddsaPublicKeyField.get(key);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
                System.exit(-1);
            }
            return k.getEncoded();
        }
    }

    /*else if (type.getKeyGeneratorAlgorithmName().contains("ECDSA"))
    {
        try {
        if (key instanceof BCECPublicKey)
            return getKeyAsRawBytes((BCECPublicKey) key);
        else if (key instanceof ECPublicKey)
            return getKeyAsRawBytes((ECPublicKey) key);
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
            
            
    }*/

    return key.getEncoded();

}