1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2024-12-23 03:17:58 +01:00

Kotlin conversion: KeyType

This commit is contained in:
Paul Schaub 2023-09-07 14:46:56 +02:00
parent 1ebf8e1e6f
commit 472d5c4beb
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
2 changed files with 110 additions and 118 deletions

View file

@ -1,118 +0,0 @@
// SPDX-FileCopyrightText: 2018 Paul Schaub <vanitasvitae@fsfe.org>
//
// SPDX-License-Identifier: Apache-2.0
package org.pgpainless.key.generation.type;
import java.security.spec.AlgorithmParameterSpec;
import org.pgpainless.algorithm.PublicKeyAlgorithm;
import org.pgpainless.key.generation.type.ecc.EllipticCurve;
import org.pgpainless.key.generation.type.ecc.ecdh.ECDH;
import org.pgpainless.key.generation.type.ecc.ecdsa.ECDSA;
import org.pgpainless.key.generation.type.eddsa.EdDSA;
import org.pgpainless.key.generation.type.eddsa.EdDSACurve;
import org.pgpainless.key.generation.type.rsa.RsaLength;
import org.pgpainless.key.generation.type.rsa.RSA;
import org.pgpainless.key.generation.type.xdh.XDH;
import org.pgpainless.key.generation.type.xdh.XDHSpec;
public interface KeyType {
/**
* Return the encryption algorithm name.
*
* @return algorithm name.
*/
String getName();
/**
* Return the public key algorithm.
*
* @return public key algorithm
*/
PublicKeyAlgorithm getAlgorithm();
/**
* Return the strength of the key in bits.
* @return strength of the key in bits
*/
int getBitStrength();
/**
* Return an implementation of {@link AlgorithmParameterSpec} that can be used to generate the key.
*
* @return algorithm parameter spec
*/
AlgorithmParameterSpec getAlgorithmSpec();
/**
* Return true if the key that is generated from this type is able to carry the SIGN_DATA key flag.
* See {@link org.pgpainless.algorithm.KeyFlag#SIGN_DATA}.
*
* @return true if the key can sign.
*/
default boolean canSign() {
return getAlgorithm().isSigningCapable();
}
/**
* Return true if the key that is generated from this type is able to carry the CERTIFY_OTHER key flag.
* See {@link org.pgpainless.algorithm.KeyFlag#CERTIFY_OTHER}.
*
* @return true if the key is able to certify other keys
*/
default boolean canCertify() {
return canSign();
}
/**
* Return true if the key that is generated from this type is able to carry the AUTHENTICATION key flag.
* See {@link org.pgpainless.algorithm.KeyFlag#AUTHENTICATION}.
*
* @return true if the key can be used for authentication purposes.
*/
default boolean canAuthenticate() {
return canSign();
}
/**
* Return true if the key that is generated from this type is able to carry the ENCRYPT_COMMS key flag.
* See {@link org.pgpainless.algorithm.KeyFlag#ENCRYPT_COMMS}.
*
* @return true if the key can encrypt communication
*/
default boolean canEncryptCommunication() {
return getAlgorithm().isEncryptionCapable();
}
/**
* Return true if the key that is generated from this type is able to carry the ENCRYPT_STORAGE key flag.
* See {@link org.pgpainless.algorithm.KeyFlag#ENCRYPT_STORAGE}.
*
* @return true if the key can encrypt for storage
*/
default boolean canEncryptStorage() {
return getAlgorithm().isEncryptionCapable();
}
static KeyType RSA(RsaLength length) {
return RSA.withLength(length);
}
static KeyType ECDH(EllipticCurve curve) {
return ECDH.fromCurve(curve);
}
static KeyType ECDSA(EllipticCurve curve) {
return ECDSA.fromCurve(curve);
}
static KeyType EDDSA(EdDSACurve curve) {
return EdDSA.fromCurve(curve);
}
static KeyType XDH(XDHSpec curve) {
return XDH.fromSpec(curve);
}
}

View file

@ -0,0 +1,110 @@
// SPDX-FileCopyrightText: 2023 Paul Schaub <vanitasvitae@fsfe.org>
//
// SPDX-License-Identifier: Apache-2.0
package org.pgpainless.key.generation.type
import org.pgpainless.algorithm.PublicKeyAlgorithm
import org.pgpainless.key.generation.type.ecc.EllipticCurve
import org.pgpainless.key.generation.type.ecc.ecdh.ECDH
import org.pgpainless.key.generation.type.ecc.ecdsa.ECDSA
import org.pgpainless.key.generation.type.eddsa.EdDSA
import org.pgpainless.key.generation.type.eddsa.EdDSACurve
import org.pgpainless.key.generation.type.rsa.RSA
import org.pgpainless.key.generation.type.rsa.RsaLength
import org.pgpainless.key.generation.type.xdh.XDH
import org.pgpainless.key.generation.type.xdh.XDHSpec
import java.security.spec.AlgorithmParameterSpec
@Suppress("INAPPLICABLE_JVM_NAME") // https://youtrack.jetbrains.com/issue/KT-31420
interface KeyType {
/**
* Return the encryption algorithm name.
*
* @return algorithm name.
*/
val name: String
/**
* Return the public key algorithm.
*
* @return public key algorithm
*/
val algorithm: PublicKeyAlgorithm
/**
* Return the strength of the key in bits.
* @return strength of the key in bits
*/
val bitStrength: Int
/**
* Return an implementation of {@link AlgorithmParameterSpec} that can be used to generate the key.
*
* @return algorithm parameter spec
*/
val algorithmSpec: AlgorithmParameterSpec
/**
* Return true if the key that is generated from this type is able to carry the SIGN_DATA key flag.
* See {@link org.pgpainless.algorithm.KeyFlag#SIGN_DATA}.
*
* @return true if the key can sign.
*/
val canSign: Boolean
@JvmName("canSign") get() = algorithm.signingCapable
/**
* Return true if the key that is generated from this type is able to carry the CERTIFY_OTHER key flag.
* See {@link org.pgpainless.algorithm.KeyFlag#CERTIFY_OTHER}.
*
* @return true if the key is able to certify other keys
*/
val canCertify: Boolean
@JvmName("canCertify") get() = canSign
/**
* Return true if the key that is generated from this type is able to carry the AUTHENTICATION key flag.
* See {@link org.pgpainless.algorithm.KeyFlag#AUTHENTICATION}.
*
* @return true if the key can be used for authentication purposes.
*/
val canAuthenticate: Boolean
@JvmName("canAuthenticate") get() = canSign
/**
* Return true if the key that is generated from this type is able to carry the ENCRYPT_COMMS key flag.
* See {@link org.pgpainless.algorithm.KeyFlag#ENCRYPT_COMMS}.
*
* @return true if the key can encrypt communication
*/
val canEncryptCommunication: Boolean
@JvmName("canEncryptCommunication") get() = algorithm.encryptionCapable
/**
* Return true if the key that is generated from this type is able to carry the ENCRYPT_STORAGE key flag.
* See {@link org.pgpainless.algorithm.KeyFlag#ENCRYPT_STORAGE}.
*
* @return true if the key can encrypt for storage
*/
val canEncryptStorage: Boolean
@JvmName("canEncryptStorage") get() = algorithm.encryptionCapable
companion object {
@JvmStatic
fun RSA(length: RsaLength): RSA = RSA.withLength(length)
@JvmStatic
fun ECDH(curve: EllipticCurve): ECDH = ECDH.fromCurve(curve)
@JvmStatic
fun ECDSA(curve: EllipticCurve): ECDSA = ECDSA.fromCurve(curve)
@JvmStatic
fun EDDSA(curve: EdDSACurve): EdDSA = EdDSA.fromCurve(curve)
@JvmStatic
fun XDH(curve: XDHSpec): XDH = XDH.fromSpec(curve)
}
}