mirror of
https://github.com/pgpainless/pgpainless.git
synced 2024-12-23 03:17:58 +01:00
Kotlin conversion: HashAlgorithmNegotiator
This commit is contained in:
parent
b5d8126e48
commit
fb235d7810
2 changed files with 71 additions and 70 deletions
|
@ -1,70 +0,0 @@
|
||||||
// SPDX-FileCopyrightText: 2021 Paul Schaub <vanitasvitae@fsfe.org>
|
|
||||||
//
|
|
||||||
// SPDX-License-Identifier: Apache-2.0
|
|
||||||
|
|
||||||
package org.pgpainless.algorithm.negotiation;
|
|
||||||
|
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
import org.pgpainless.algorithm.HashAlgorithm;
|
|
||||||
import org.pgpainless.policy.Policy;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Interface for a class that negotiates {@link HashAlgorithm HashAlgorithms}.
|
|
||||||
*
|
|
||||||
* You can provide your own implementation using custom logic by implementing the
|
|
||||||
* {@link #negotiateHashAlgorithm(Set)} method.
|
|
||||||
*/
|
|
||||||
public interface HashAlgorithmNegotiator {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Pick one {@link HashAlgorithm} from the ordered set of acceptable algorithms.
|
|
||||||
*
|
|
||||||
* @param orderedHashAlgorithmPreferencesSet hash algorithm preferences
|
|
||||||
* @return picked algorithms
|
|
||||||
*/
|
|
||||||
HashAlgorithm negotiateHashAlgorithm(Set<HashAlgorithm> orderedHashAlgorithmPreferencesSet);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Return an instance that negotiates {@link HashAlgorithm HashAlgorithms} used for non-revocation signatures
|
|
||||||
* based on the given {@link Policy}.
|
|
||||||
*
|
|
||||||
* @param policy algorithm policy
|
|
||||||
* @return negotiator
|
|
||||||
*/
|
|
||||||
static HashAlgorithmNegotiator negotiateSignatureHashAlgorithm(Policy policy) {
|
|
||||||
return negotiateByPolicy(policy.getSignatureHashAlgorithmPolicy());
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Return an instance that negotiates {@link HashAlgorithm HashAlgorithms} used for revocation signatures
|
|
||||||
* based on the given {@link Policy}.
|
|
||||||
*
|
|
||||||
* @param policy algorithm policy
|
|
||||||
* @return negotiator
|
|
||||||
*/
|
|
||||||
static HashAlgorithmNegotiator negotiateRevocationSignatureAlgorithm(Policy policy) {
|
|
||||||
return negotiateByPolicy(policy.getRevocationSignatureHashAlgorithmPolicy());
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Return an instance that negotiates {@link HashAlgorithm HashAlgorithms} based on the given
|
|
||||||
* {@link Policy.HashAlgorithmPolicy}.
|
|
||||||
*
|
|
||||||
* @param hashAlgorithmPolicy algorithm policy for hash algorithms
|
|
||||||
* @return negotiator
|
|
||||||
*/
|
|
||||||
static HashAlgorithmNegotiator negotiateByPolicy(Policy.HashAlgorithmPolicy hashAlgorithmPolicy) {
|
|
||||||
return new HashAlgorithmNegotiator() {
|
|
||||||
@Override
|
|
||||||
public HashAlgorithm negotiateHashAlgorithm(Set<HashAlgorithm> orderedPreferencesSet) {
|
|
||||||
for (HashAlgorithm preference : orderedPreferencesSet) {
|
|
||||||
if (hashAlgorithmPolicy.isAcceptable(preference)) {
|
|
||||||
return preference;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return hashAlgorithmPolicy.defaultHashAlgorithm();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -0,0 +1,71 @@
|
||||||
|
// SPDX-FileCopyrightText: 2023 Paul Schaub <vanitasvitae@fsfe.org>
|
||||||
|
//
|
||||||
|
// SPDX-License-Identifier: Apache-2.0
|
||||||
|
|
||||||
|
package org.pgpainless.algorithm.negotiation
|
||||||
|
|
||||||
|
import org.pgpainless.algorithm.HashAlgorithm
|
||||||
|
import org.pgpainless.policy.Policy
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Interface for a class that negotiates [HashAlgorithms][HashAlgorithm].
|
||||||
|
*
|
||||||
|
* You can provide your own implementation using custom logic by implementing the
|
||||||
|
* [negotiateHashAlgorithm(Set)] method.
|
||||||
|
*/
|
||||||
|
interface HashAlgorithmNegotiator {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Pick one [HashAlgorithm] from the ordered set of acceptable algorithms.
|
||||||
|
*
|
||||||
|
* @param orderedPrefs hash algorithm preferences
|
||||||
|
* @return picked algorithms
|
||||||
|
*/
|
||||||
|
fun negotiateHashAlgorithm(orderedPrefs: Set<HashAlgorithm>): HashAlgorithm
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return an instance that negotiates [HashAlgorithms][HashAlgorithm] used for non-revocation signatures
|
||||||
|
* based on the given [Policy].
|
||||||
|
*
|
||||||
|
* @param policy algorithm policy
|
||||||
|
* @return negotiator
|
||||||
|
*/
|
||||||
|
@JvmStatic
|
||||||
|
fun negotiateSignatureHashAlgorithm(policy: Policy): HashAlgorithmNegotiator {
|
||||||
|
return negotiateByPolicy(policy.signatureHashAlgorithmPolicy)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return an instance that negotiates [HashAlgorithms][HashAlgorithm] used for revocation signatures
|
||||||
|
* based on the given [Policy].
|
||||||
|
*
|
||||||
|
* @param policy algorithm policy
|
||||||
|
* @return negotiator
|
||||||
|
*/
|
||||||
|
@JvmStatic
|
||||||
|
fun negotiateRevocationSignatureAlgorithm(policy: Policy): HashAlgorithmNegotiator {
|
||||||
|
return negotiateByPolicy(policy.revocationSignatureHashAlgorithmPolicy)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return an instance that negotiates [HashAlgorithms][HashAlgorithm] based on the given
|
||||||
|
* [Policy.HashAlgorithmPolicy].
|
||||||
|
*
|
||||||
|
* @param hashAlgorithmPolicy algorithm policy for hash algorithms
|
||||||
|
* @return negotiator
|
||||||
|
*/
|
||||||
|
@JvmStatic
|
||||||
|
fun negotiateByPolicy(hashAlgorithmPolicy: Policy.HashAlgorithmPolicy): HashAlgorithmNegotiator {
|
||||||
|
return object: HashAlgorithmNegotiator {
|
||||||
|
override fun negotiateHashAlgorithm(orderedPrefs: Set<HashAlgorithm>): HashAlgorithm {
|
||||||
|
return orderedPrefs.firstOrNull {
|
||||||
|
hashAlgorithmPolicy.isAcceptable(it)
|
||||||
|
} ?: hashAlgorithmPolicy.defaultHashAlgorithm()
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue