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 operation that the key contained in the key block can perform.
027 * <p>
028 * Each value represents byte 8 of the Keyblok Header.
029 */
030public enum ModeOfUse {
031
032    /**
033     * The key may be used to perform both encrypt and decrypt operations.
034     */
035    ENCDEC          ('B', "Encryption and Decryption"),
036
037    /**
038     * The key may be used to perform MAC calculation <i>(both generate &
039     * verify)</i> operations.
040     */
041    GENVER          ('C', "Verification and Generation of MAC, CVD"),
042
043    /**
044     * The key may only be used to perform decrypt operations.
045     */
046    DECONLY         ('D', "Data Decryption"),
047
048    /**
049     * The key may only be used to perform encrypt operations.
050     */
051    ENCONLY         ('E', "Data Encryption"),
052
053    /**
054     * The key may only be used to perform MAC generate operations.
055     */
056    GENONLY         ('G', "Generaction of MAC, CVD"),
057
058    /**
059     * No special restrictions apply.
060     */
061    ANY             ('N', "Without restrictions"),
062
063    /**
064     * The key may only be used to perform digital signature generation
065     * operations.
066     */
067    GENSIGN         ('S', "Digital Signature Generation"),
068
069    /**
070     * The key may be used to perform both digital signature generation and
071     * verification operations.
072     */
073    SIGNVER         ('T', "Digital Signature Generation and Verification"),
074
075    /**
076     * The key may only be used to perform digital signature verification
077     * operations.
078     */
079    VERONLY         ('V', "Digital Signature Verification"),
080
081    /**
082     * The key may only be used to derive other keys.
083     */
084    DERIVE          ('X', "Derive Keys"),
085
086    /**
087     * The key may be used to create key variants.
088     */
089    KEYVAR          ('Y', "Key used to create key variants");
090
091
092    private static final Map<Character, ModeOfUse> MAP = new HashMap<>();
093
094    static {
095        for (ModeOfUse tr : ModeOfUse.values())
096            MAP.put(tr.getCode(), tr);
097    }
098
099    private final char code;
100    private final String name;
101
102    ModeOfUse(char code, String name) {
103        Objects.requireNonNull(name, "The name of key use mode is required");
104        this.code = code;
105        this.name = name;
106    }
107
108    /**
109     * Get code of key use mode.
110     *
111     * @return the character which represents code of key use mode
112     */
113    public char getCode() {
114        return code;
115    }
116
117    /**
118     * Get name of key use mode.
119     *
120     * @return the name of key use mode.
121     */
122    public String getName() {
123        return name;
124    }
125
126    @Override
127    public String toString() {
128        return String.format("ModeOfUse[code: %s, name: %s]", code, name);
129    }
130
131    /**
132     * Returns the enum constant of this type with the specified {@code code}.
133     *
134     * @param code the string must match exactly with identifier specified by
135     *        <i>ISO 8583-1:2003(E) Table A.22 — Transaction type codes</i>
136     * @return the enum constant with the specified processing code or
137     *         {@code null} if unknown.
138     */
139    public static ModeOfUse valueOfByCode(char code) {
140        return MAP.get(code);
141    }
142
143}