/*
 * jPOS Project [http://jpos.org]
 * Copyright (C) 2000-2020 jPOS Software SRL
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.jpos.q2.cli.ssm.actions;

import org.jpos.iso.ISODate;
import org.jpos.q2.CLIContext;
import org.jpos.q2.cli.ssm.SsmActionBase;
import org.jpos.security.SMException;
import org.jpos.security.SecureDESKey;
import org.jpos.security.jceadapter.JCESecurityModule;

/**
 * Calculate CVV
 */
public class CVV extends SsmActionBase {
    @Override
    protected boolean checkUsage(CLIContext cli, String[] strings) {
        if (strings.length < 7) {
            cli.println("Usage: cvv keyLength PVKunderLMK KeyCheck PAN EXP ServiceCode");
            return false;
        }
        return true;
    }

    @Override
    protected void doCommand(CLIContext cli, JCESecurityModule sm, short keyLength, String[] args) throws SMException {
        SecureDESKey pvkUnderLmk = new SecureDESKey (keyLength, "CVK", args[2], args[3]);
        String pan = args[4];
        String exp = args[5];
        String serviceCode = args[6];
        sm.calculateCVV(pan, pvkUnderLmk, pvkUnderLmk, ISODate.parseISODate(exp + "01000000"), serviceCode);
    }
}


@@ -1205,9 +1205,9 @@
 h %3C 
-7
+9
 ) %7B%0A
@@ -1255,16 +1255,19 @@
 ngth PVK
+-A-
 underLMK
@@ -1275,16 +1275,44 @@
 KeyCheck
+-A PVK-B-underLMK KeyCheck-B
  PAN EXP
@@ -1545,24 +1545,25 @@
 reDESKey pvk
+A
 UnderLmk = n
@@ -1616,16 +1616,107 @@
 gs%5B3%5D);%0A
+        SecureDESKey pvkBUnderLmk = new SecureDESKey (keyLength, %22CVK%22, args%5B4%5D, args%5B5%5D);%0A
         
@@ -1733,17 +1733,17 @@
  = args%5B
-4
+6
 %5D;%0A     
@@ -1763,17 +1763,17 @@
  = args%5B
-5
+7
 %5D;%0A     
@@ -1793,33 +1793,33 @@
 viceCode = args%5B
-6
+8
 %5D;%0A        sm.ca
@@ -1833,24 +1833,25 @@
 CVV(pan, pvk
+A
 UnderLmk, pv
@@ -1851,16 +1851,17 @@
 Lmk, pvk
+B
 UnderLmk
@@ -1922,11 +1922,10 @@
 ;%0A    %7D%0A
+
 %7D%0A
-%0A

add cvkB