001/*
002 * jPOS Project [http://jpos.org]
003 * Copyright (C) 2000-2026 jPOS Software SRL
004 *
005 * This program is free software: you can redistribute it and/or modify
006 * it under the terms of the GNU Affero General Public License as
007 * published by the Free Software Foundation, either version 3 of the
008 * License, or (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013 * GNU Affero General Public License for more details.
014 *
015 * You should have received a copy of the GNU Affero General Public License
016 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
017 */
018
019package org.jpos.security;
020
021import java.util.HashMap;
022import java.util.Map;
023import java.util.Objects;
024
025/**
026 * Defines the cryptographic algorithm with which the key contained in the key
027 * block will be used.
028 * <p>
029 * Each value repesents byte 7 of the Keyblok Header.
030 */
031public enum Algorithm {
032
033    /**
034     * AES - Advanced Encryption Standard.
035     */
036    AES         ('A', "AES - Advanced Encryption Standard"),
037
038    /**
039     * DES - Data Encryption Standard.
040     */
041    DES         ('D', "DES - Data Encryption Standard"),
042
043    /**
044     * Elliptic curve.
045     */
046    EC          ('E', "Elliptic curve"),
047
048    /**
049     * HMAC - Hash Message Authentication Code.
050     */
051    HMAC        ('H', "HMAC - Hash Message Authentication Code"),
052
053    /**
054     * RSA - Rivest–Shamir–Adleman.
055     */
056    RSA         ('R', "RSA - Rivest Shamir Adleman"),
057
058    /**
059     * DSA - Digital Signature Algorithm.
060     */
061    DSA         ('S', "DSA - Digital Signature Algorithm"),
062
063    /**
064     * TDES - Triple Data Encryption Standard.
065     * <p>
066     * Also known as TDSA <i>(official Triple Data Encryption Algorithm)</i>.
067     */
068    TDES        ('T', "Triple DES - Triple Data Encryption Standard");
069
070
071    private static final Map<Character, Algorithm> MAP = new HashMap<>();
072
073    static {
074        for (Algorithm alg : Algorithm.values())
075            MAP.put(alg.getCode(), alg);
076    }
077
078    private final char code;
079    private final String name;
080
081    Algorithm(char code, String name) {
082        Objects.requireNonNull(name, "The name of algorithm is required");
083        this.code = code;
084        this.name = name;
085    }
086
087    /**
088     * Get algorithm code.
089     *
090     * @return character algorithm code
091     */
092    public char getCode() {
093        return code;
094    }
095
096    /**
097     * Get algorithm name.
098     *
099     * @return the algorithm name
100     */
101    public String getName() {
102        return name;
103    }
104
105    @Override
106    public String toString() {
107        return String.format("Algorithm[code: %s, name: %s]", code, name);
108    }
109
110    /**
111     * Returns the enum constant of this type with the specified {@code code}.
112     *
113     * @param code the string must match exactly with identifier specified by
114     *        <i>ISO 8583-1:2003(E) Table A.22 — Transaction type codes</i>
115     * @return the enum constant with the specified processing code or
116     *         {@code null} if unknown.
117     */
118    public static Algorithm valueOfByCode(char code) {
119        return MAP.get(code);
120    }
121
122}