1 import type { AlgorithmInfo } from '@proton/crypto';
2 import unique from '@proton/utils/unique';
4 import type { KeyGenConfig } from '../interfaces';
6 const formatCurveName = (curve: AlgorithmInfo['curve']) => {
8 case 'curve25519Legacy':
14 return curve.replace('nist', 'NIST ');
15 case 'brainpoolP256r1':
16 case 'brainpoolP384r1':
17 case 'brainpoolP512r1':
18 return curve.replace('brainpool', 'Brainpool ');
24 export const getFormattedAlgorithmName = ({ algorithm, bits, curve }: AlgorithmInfo) => {
27 return `ElGamal (${bits})`;
29 return `DSA (${bits})`;
31 case 'rsaEncryptSign':
33 return `RSA (${bits})`;
37 return `ECC (${formatCurveName(curve)})`;
40 return `ECC (Curve25519, new format)`;
43 return `ECC (Curve448, new format)`;
45 return algorithm.toUpperCase(); // should never get here
50 * Aggregate different algorithm information, returning a string including the list of unique key algo descriptors.
51 * @param {AlgorithmInfo[]} algorithmInfos
52 * @returns {String} formatted unique algorithm names. Different curves or key sizes result in separate entries, e.g.
53 * [{ name: 'rsa', bits: 2048 }, { name: 'rsa', bits: 4096 }] returns `RSA (2048), RSA (4096)`.
55 export const getFormattedAlgorithmNames = (algorithmInfos: AlgorithmInfo[] = []) => {
56 const formattedAlgos = algorithmInfos.map(getFormattedAlgorithmName);
57 return unique(formattedAlgos).join(', ');
61 * Determine whether any of the given algorithmInfo matches the keyGenConfig
63 export const getAlgorithmExists = (algorithmInfos: AlgorithmInfo[] = [], keyGenConfig: KeyGenConfig) => {
64 return algorithmInfos.some(({ algorithm, curve, bits }) => {
67 case 'rsaEncryptSign':
69 return bits === keyGenConfig.rsaBits;
73 return curve === keyGenConfig.curve;
78 return false; // key generation currently unsupported