mirror of
https://github.com/pgpainless/pgpainless.git
synced 2024-11-14 00:12:06 +01:00
Kotlin conversion: SessionKey
This commit is contained in:
parent
02511ac1ca
commit
43335cbcd3
2 changed files with 48 additions and 66 deletions
|
@ -1,66 +0,0 @@
|
||||||
// SPDX-FileCopyrightText: 2021 Paul Schaub <vanitasvitae@fsfe.org>
|
|
||||||
//
|
|
||||||
// SPDX-License-Identifier: Apache-2.0
|
|
||||||
|
|
||||||
package org.pgpainless.util;
|
|
||||||
|
|
||||||
import javax.annotation.Nonnull;
|
|
||||||
|
|
||||||
import org.bouncycastle.openpgp.PGPSessionKey;
|
|
||||||
import org.bouncycastle.util.encoders.Hex;
|
|
||||||
import org.pgpainless.algorithm.SymmetricKeyAlgorithm;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A {@link SessionKey} is the symmetric key that is used to encrypt/decrypt an OpenPGP message.
|
|
||||||
* The OpenPGP message header contains a copy of the session key, encrypted for the public key of each recipient.
|
|
||||||
*/
|
|
||||||
public class SessionKey {
|
|
||||||
|
|
||||||
private final SymmetricKeyAlgorithm algorithm;
|
|
||||||
private final byte[] key;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Constructor to create a session key from a BC {@link PGPSessionKey} object.
|
|
||||||
*
|
|
||||||
* @param sessionKey BC session key
|
|
||||||
*/
|
|
||||||
public SessionKey(@Nonnull PGPSessionKey sessionKey) {
|
|
||||||
this(SymmetricKeyAlgorithm.requireFromId(sessionKey.getAlgorithm()), sessionKey.getKey());
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Create a session key object from an algorithm and a key.
|
|
||||||
*
|
|
||||||
* @param algorithm algorithm
|
|
||||||
* @param key key
|
|
||||||
*/
|
|
||||||
public SessionKey(@Nonnull SymmetricKeyAlgorithm algorithm, @Nonnull byte[] key) {
|
|
||||||
this.algorithm = algorithm;
|
|
||||||
this.key = key;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Return the symmetric key algorithm.
|
|
||||||
*
|
|
||||||
* @return algorithm
|
|
||||||
*/
|
|
||||||
public SymmetricKeyAlgorithm getAlgorithm() {
|
|
||||||
return algorithm;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Return the bytes of the key.
|
|
||||||
*
|
|
||||||
* @return key
|
|
||||||
*/
|
|
||||||
public byte[] getKey() {
|
|
||||||
byte[] copy = new byte[key.length];
|
|
||||||
System.arraycopy(key, 0, copy, 0, copy.length);
|
|
||||||
return copy;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString() {
|
|
||||||
return "" + getAlgorithm().getAlgorithmId() + ":" + Hex.toHexString(getKey());
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -0,0 +1,48 @@
|
||||||
|
// SPDX-FileCopyrightText: 2023 Paul Schaub <vanitasvitae@fsfe.org>
|
||||||
|
//
|
||||||
|
// SPDX-License-Identifier: Apache-2.0
|
||||||
|
|
||||||
|
package org.pgpainless.util
|
||||||
|
|
||||||
|
import org.bouncycastle.openpgp.PGPSessionKey
|
||||||
|
import org.bouncycastle.util.encoders.Hex
|
||||||
|
import org.pgpainless.algorithm.SymmetricKeyAlgorithm
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A [SessionKey] is the symmetric key that is used to encrypt/decrypt an OpenPGP message payload.
|
||||||
|
* The OpenPGP message header contains a copy of the session key, encrypted for the public key of each recipient.
|
||||||
|
*
|
||||||
|
* @param algorithm symmetric key algorithm
|
||||||
|
* @param key bytes of the key
|
||||||
|
*/
|
||||||
|
data class SessionKey(val algorithm: SymmetricKeyAlgorithm,
|
||||||
|
val key: ByteArray) {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor to create a session key from a BC [PGPSessionKey] object.
|
||||||
|
*
|
||||||
|
* @param sessionKey BC session key
|
||||||
|
*/
|
||||||
|
constructor(sessionKey: PGPSessionKey):
|
||||||
|
this(SymmetricKeyAlgorithm.requireFromId(sessionKey.algorithm), sessionKey.key)
|
||||||
|
|
||||||
|
override fun toString(): String {
|
||||||
|
return "${algorithm.algorithmId}:${Hex.toHexString(key)}"
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun equals(other: Any?): Boolean {
|
||||||
|
if (this === other) return true
|
||||||
|
if (javaClass != other?.javaClass) return false
|
||||||
|
|
||||||
|
other as SessionKey
|
||||||
|
|
||||||
|
if (algorithm != other.algorithm) return false
|
||||||
|
if (!key.contentEquals(other.key)) return false
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun hashCode(): Int {
|
||||||
|
return 31 * algorithm.hashCode() + key.contentHashCode()
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue