package com.hsmtest.hsm.operations;
import java.io.IOException;
import org.jpos.core.SimpleConfiguration;
import org.jpos.iso.FSDISOMsg;
import org.jpos.iso.ISOUtil;
import org.jpos.iso.channel.FSDChannel;
import org.jpos.iso.packager.DummyPackager;
import org.jpos.util.FSDMsg;
import org.jpos.util.LogEvent;
import org.jpos.util.Logger;
/**
* @author Foly
*
*/
public class ThalesAdapter {
private static final String PREFIX = "src/test/resources/";
/*Assign the class name to the logger */
static Logger log = Logger.getLogger("ThalesAdapter.class");
private boolean trace = true;
FSDChannel channel = null;
/**
*
*/
public ThalesAdapter(String host, int port) {
channel = new FSDChannel();
channel.setHost(host);
channel.setPort(port);
channel.setPackager(new DummyPackager());
}
public FSDMsg diagnostics () {
return command(createRequest ("NC"));
}
public FSDMsg encryptPin (String pin, String acctNo) {
FSDMsg req = createRequest("BA");
req .set("PIN", pin);
req.set("Account Number", acctNo); /*The rightmost 12 digits excluding check digit */
System.out.printf ("pn %s and pan %s == " , pin, acctNo);
System.out.println();
return command(req);
}
public FSDMsg calculateVisaPvv (String pvk, String pin, String acctNo, String pvki) {
FSDMsg req = createRequest("DG");
req.set("PVK Pair", pvk);
req.set("PIN", pin); /* Encrypted Under LMK */
req.set("Account Number", acctNo); /*The rightmost 12 digits excluding check digit */
req.set("PVKI", pvki);
return command(req);
}
public FSDMsg calculateMasterCardPinOffset (String pvk, String pin, String acctNo, String chklen, String dlTable, String pinVlData) {
FSDMsg req = createRequest("DE");
req.set("PVK", pvk);
req.set("PIN under LMK", pin); /* Encrypted Under LMK */
req.set("Check Length", chklen);
req.set("Account Number", acctNo); /*The rightmost 12 digits excluding check digit */
req.set("Decimalisation Table", dlTable);
req.set("PIN Validation Data", pinVlData);
return command(req);
}
public FSDMsg createRequest (String command) {
FSDMsg req = new FSDMsg ("file:" + PREFIX + "hsm-");
if (command != null) {
req.set ("command", command);
req.setCharset(ISOUtil.CHARSET);
}
return req;
}
public FSDMsg createResponse (String response) {
FSDMsg resp = new FSDMsg ("file:" + PREFIX + "hsm-");
if (response != null)
resp.set("response", response);
return resp;
}
public synchronized FSDMsg command(FSDMsg request) {
LogEvent evt =new LogEvent();
FSDMsg resp;
StringBuffer sbuffer = new StringBuffer(request.get("command"));
//System.out.println("My Original buffer string == " + sbuffer.toString());
sbuffer.setCharAt(1, (char) (sbuffer.charAt(1) + 1));
resp = createResponse(sbuffer.toString());
System.out.println("Response Message Path == " + resp.getBasePath());
System.out.println("command being executed == " + request.get("command"));
try {
String s = request.pack();
if (s != null) {
/* Check if channel is not connected. If no, connect */
if (!isConnected()) connect();
FSDISOMsg isomsg = new FSDISOMsg(request);
isomsg.setHeader(new byte[] { (byte) 0, (byte) 0, (byte) 0, (byte) 0});
evt.addMessage ("Sending Message to HSM >>> ");
System.out.println("command being executed == " + request.get("command"));
SimpleConfiguration cfg = new SimpleConfiguration();
cfg.put("schema", request.getBasePath());
channel.setConfiguration(cfg);
channel.send(isomsg);
FSDISOMsg respIsomsg = (FSDISOMsg) channel.receive();
evt.addMessage ("****** Message received from HSM before merge operation ******");
evt.addMessage (respIsomsg);
evt.addMessage ("************************** End *******************************");
resp.merge(respIsomsg.getFSDMsg());
if (trace)
evt.addMessage (request);
} else {
if (trace)
evt.addMessage ("*****Request is null- Unable to pack request.*****");
evt.addMessage (request);
}
disconnect(); /* Disconnect from channel */
} catch (Exception e) {
evt.addMessage(e.getStackTrace());
} finally {
Logger.log (evt);
}
return resp;
}
public void connect() throws IOException {
channel.connect();
}
public boolean isConnected() {
return channel.isConnected();
}
public void disconnect() throws IOException {
channel.disconnect();
}
}
package com.hsmtest.hsm.operations;
import org.jpos.util.FSDMsg;
import org.jpos.util.LogEvent;
import org.jpos.util.Logger;
/**
* @author Foly
*
*/
public class HsmComPvv {
private static final String PREFIX = "src/test/resources/";
/*Assign the class name to the logger */
static Logger log = Logger.getLogger("HsmComPvv.class");
private static boolean trace = true;
/**
* @param args
* @throws InterruptedException
*/
public static void main(String[] args) throws InterruptedException {
testCalculatePVVviaHSM ();
}
public static void testCalculatePVVviaHSM () {
LogEvent evt =new LogEvent();
try {
/* Extract the rightmost 12 digits of the PAN excluding check digit */
String mpan = "5*****0208863679";
String pinValidationData = mpan.substring(0, 10) + "N" + (mpan.length()-10);
mpan = mpan.substring(0, mpan.length()-1);
int startIndex = mpan.length() - 12;
String mAccountNumber = mpan.substring(startIndex, mpan.length());
System.out.println("12 Digit MasterCard Account Number >>> " + mAccountNumber);
/* Extract the rightmost 12 digits of the PAN excluding check digit */
String vpan = "4*****0220759492";
vpan = vpan.substring(0, vpan.length()-1);
startIndex = vpan.length() - 12;
String vAccountNumber = vpan.substring(startIndex, vpan.length());
System.out.println("12 Digit Visa Account Number >>> " + vAccountNumber);
FSDMsg respGetFileds = new FSDMsg ("file:" + PREFIX + "hsm-");
/* Ping Thales HSM with "NC" command and record response
* before making actual HSM function
* call to calculate PVV
*/
ThalesAdapter thales = new ThalesAdapter("10.4.139.9", 9990);
/* Perform HSM diagnostic test to return LMK check digit & HSM firmware number */
FSDMsg responseMsg = thales.diagnostics();
if (trace) evt.addMessage (responseMsg);
responseMsg.dump(System.out, "NC Command Dump - ");
System.out.println ("responseMsg Path +++++++ " + responseMsg.getBasePath());
/*respGetFileds.unpack(responseMsg.pack().getBytes());
System.out.println ("Hmmm....");
System.out.println (" 123&&&&&&&& " + respGetFileds.get("response"));*/
responseMsg.dump(System.out, "NC Command Dump - ");
/* Next Step: Call HSM to Encrypt MasterCard PIN */
FSDMsg resp = thales.encryptPin ("1234", mAccountNumber);
if (trace) evt.addMessage (resp);
/* Retrieve error code from response received.
* If "00", then all is ok. Retrieve encrypted pin*/
resp.dump(System.out, "MC BA Command Dump - ");
/* Next Step: Call HSM to Encrypt Visa PIN */
resp = thales.encryptPin ("1234", vAccountNumber);
if (trace) evt.addMessage (resp);
/* Retrieve error code from response received.
* If "00", then all is ok. Retrieve encrypted pin*/
resp.dump(System.out, "Visa BA Command Dump --- ");
/* Next Step: Call HSM to generate VISA card PVV */
resp = thales.calculateVisaPvv ("0D0A7C61FB7E7987" + "0A2992B6DBE1A7ED", "6224", vAccountNumber, "1");
if (trace) evt.addMessage (resp);
/* Retrieve error code from response received. If "00", then all is ok */
resp.dump(System.out, "Visa Calculation - ");
/* Next Step: Call HSM to generate MasterCard PVV */
resp = thales.calculateMasterCardPinOffset("7674DF79DFB7FDFB", "1234", mAccountNumber, "04", "0123456789012345", pinValidationData);
if (trace) evt.addMessage (resp);
/* Retrieve error code from response received. If "00", then all is ok */
resp.dump(System.out, "MasterCard PinOffset Calculation - ");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
Logger.log (evt);
}
}
}
******************************
BA, BB, DE & DF XML files
*******************************
<?xml version="1.0" encoding="UTF-8"?>
<schema id='BA'>
<field id="Command code" type="A" length="2" />
<field id="PIN" type="H" length="4" />
<field id="Account Number" type="N" length="12" />
</schema>
<?xml version="1.0" encoding="UTF-8"?>
<schema id='BB'>
<field id="Response code" type="A" length="2" />
<field id="Error code" type="N" length="2" />
<field id="PIN" type="N" length="16" />
<!-- LN for PIN / -->
</schema>
<?xml version="1.0" encoding="UTF-8"?>
<schema id='DE'>
<field id="Command code" type="A" length="2" />
<!-- field id="Tpk" / -->
<field id="PVK" type="A" length="16" />
<!-- field id="PIN" type="H" length="8" LN or LH/ -->
<field id="PIN-Under-LMK" type="N" length="4" />
<field id="Check length" type="N" length="2" />
<field id="Account Number" type="N" length="12" />
<field id="Decimalisation Table" type="N" length="16" />
<field id="PIN validation data" type="A" length="12" />
</schema>
<?xml version="1.0" encoding="UTF-8"?>
<schema id='DF'>
<field id="Response code" type="A" length="2" />
<field id="Error code" type="N" length="2" />
<field id="Offset" type="N" length="12" />
</schema>
--
--
jPOS is licensed under AGPL - free for community usage for your open-source project. Licenses are also available for commercial usage. Please support jPOS, contact: sa...@jpos.org
---
You received this message because you are subscribed to the Google Groups "jPOS Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jpos-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jpos-users/1a901f5b-694d-4b78-9d39-99e263b0e7aen%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jpos-users/fHH0tKj0idvcEARRW1SC6oUShfLjHmpvxj3t_4dGE-RTRGYLWHBAxCGlRGC6TUBRuvyDSla2se_XhqYWpE-SwpYP3fjT4kdka5zuEF85Yys%3D%40pm.me.

isomsg.setHeader(new byte[] { (byte) 0, (byte) 0, (byte) 0, (byte) 0});
To view this discussion on the web visit https://groups.google.com/d/msgid/jpos-users/c9de8cf4-7849-4029-8005-efe4661f4116n%40googlegroups.com.
<?xml version="1.0" encoding="UTF-8"?>
<schema id='BA'>
<field id="pin" type="H" length="5" />
<field id="account-number" type="N" length="12" />
</schema>
******** hsm-resp-BB.xml ***********
<?xml version="1.0" encoding="UTF-8"?>
<schema id='BB'>
<field id="PIN" type="N" length="5" />
</schema>
******** hsm-NC.xml ***********
<?xml version="1.0" encoding="UTF-8"?>
<schema id='NC'>
</schema>
******** hsm-resp-ND.xml ***********
<?xml version="1.0" encoding="UTF-8"?>
<schema id='ND'>
<field id="lmk-check-value" type="A" length="16" />
<field id="firmware-number" type="A" length="9" />
</schema>
import java.io.IOException;
import org.jpos.core.SimpleConfiguration;
import org.jpos.iso.FSDISOMsg;
import org.jpos.iso.ISOUtil;
import org.jpos.iso.channel.FSDChannel;
import org.jpos.iso.packager.DummyPackager;
import org.jpos.util.FSDMsg;
import org.jpos.util.LogEvent;
import org.jpos.util.Logger;
public class ThalesAdapter {
private static final String PREFIX = "usr/app/testinghsm/";
/*Assign the class name to the logger */
static Logger log = Logger.getLogger("ThalesAdapter.class");
private boolean trace = true;
FSDChannel channel = null;
public ThalesAdapter(String host, int port) {
channel = new FSDChannel();
channel.setHost(host);
channel.setPort(port);
channel.setPackager(new DummyPackager());
}
public FSDMsg diagnostics () {
return command(createRequest ("NC"));
}
public FSDMsg encryptPin (String pin, String acctNo) {
FSDMsg req = createRequest("BA");
req .set("pin", pin);
req.set("account-number", acctNo); /*The rightmost 12 digits excluding check digit */
return command(req);
}
public FSDMsg calculateVisaPvv (String pvk, String pin, String acctNo, String pvki) {
FSDMsg req = createRequest("DG");
req.set("PVK-Pair", pvk);
req.set("PIN", pin); /* Encrypted Under LMK */
req.set("Account-Number", acctNo); /*The rightmost 12 digits excluding check digit */
req.set("PVKI", pvki);
return command(req);
}
public FSDMsg calculateMasterCardPinOffset (String pvk, String pin, String acctNo, String chklen, String dlTable, String pinVlData) {
FSDMsg req = createRequest("DE");
req.set("PVK", pvk);
req.set("PIN", pin); /* Encrypted Under LMK */
req.set("Check-Length", chklen);
req.set("Account-Number", acctNo); /*The rightmost 12 digits excluding check digit */
req.set("Decimalisation-Table", dlTable);
req.set("PIN-Validation-Data", pinVlData);
return command(req);
}
public FSDMsg createRequest (String command) {
FSDMsg req = new FSDMsg ("file:" + PREFIX + "hsm-");
if (command != null) {
req.set ("command", command);
req.setCharset(ISOUtil.CHARSET);
}
return req;
}
public FSDMsg createResponse (String response) {
FSDMsg resp = new FSDMsg ("file:" + PREFIX + "hsm-resp-");
if (response != null) resp.set("response", response);
return resp;
}
public synchronized FSDMsg command(FSDMsg request) {
LogEvent evt =new LogEvent();
FSDMsg resp = null;
StringBuffer sbuffer = new StringBuffer(request.get("command"));
sbuffer.setCharAt(1, (char) (sbuffer.charAt(1) + 1));
resp = createResponse(sbuffer.toString());
System.out.println("command being executed == " + request.get("command"));
try {
if (request.pack() != null) {
/* Check if channel is not connected. If no, connect */
if (!isConnected()) connect();
FSDISOMsg isomsg = new FSDISOMsg(request);
evt.addMessage ("Sending Message to HSM >>> ");
System.out.println("command being executed == " + request.get("command"));
SimpleConfiguration cfg = new SimpleConfiguration();
cfg.put("schema", resp.getBasePath());
channel.setConfiguration(cfg);
isomsg.dump(System.out, "Message sennt to HSM");
channel.send(isomsg);
FSDISOMsg respIsomsg = (FSDISOMsg) channel.receive();
respIsomsg.dump(System.out, "Response Received from HSM - ");
evt.addMessage ("****** Message received from HSM before merge operation ******");
evt.addMessage (respIsomsg);
evt.addMessage ("************************** End *******************************");
if (trace) evt.addMessage (request);
resp.merge(respIsomsg.getFSDMsg());
} else {
if (trace)
evt.addMessage ("*****Request is null- Unable to pack request.*****");
evt.addMessage (request);
}
disconnect(); /* Disconnect from channel */
} catch (Exception e) {
evt.addMessage(e.getStackTrace());
} finally {
Logger.log (evt);
}
return resp;
}
public void connect() throws IOException {
channel.connect();
}
public boolean isConnected() {
return channel.isConnected();
}
public void disconnect() throws IOException {
channel.disconnect();
}
}
******* callHsm Call ***********
import org.jpos.util.FSDMsg;
import org.jpos.util.LogEvent;
import org.jpos.util.Logger;
public class callHsm {
/*Assign the class name to the logger */
static Logger log = Logger.getLogger("callHsm.class");
private static boolean trace = true;
/**
* @param args
* @throws InterruptedException
*/
public static void main(String[] args) throws InterruptedException {
testCalculatePVVviaHSM ();
}
public static void testCalculatePVVviaHSM () {
LogEvent evt =new LogEvent();
try {
/* Extract the rightmost 12 digits of the PAN excluding check digit */
String mpan = "5973100208863679";
String pinValidationData = mpan.substring(0, 10) + "N" + (mpan.length()-10);
mpan = mpan.substring(0, mpan.length()-1);
int startIndex = mpan.length() - 12;
String mAccountNumber = mpan.substring(startIndex, mpan.length());
System.out.println("12 Digit MasterCard Account Number >>> " + mAccountNumber);
/* Ping Thales HSM with "NC" command and record response
* before making actual HSM function
* call to calculate PVV
*/
ThalesAdapter thales = new ThalesAdapter("10.4.139.9", 9990);
/* Perform HSM diagnostic test to return LMK check digit & HSM firmware number */
FSDMsg responseMsg = thales.diagnostics();
if (trace) evt.addMessage (responseMsg);
//System.out.println ("responseMsg Path +++++++ " + responseMsg.getBasePath());
responseMsg.dump(System.out, "NC Command Dump - ");
/* Next Step: Call HSM to Encrypt MasterCard PIN */
FSDMsg resp = thales.encryptPin ("1234F", mAccountNumber);
if (trace) evt.addMessage (resp);
/* Retrieve error code from response received.
* If "00", then all is ok. Retrieve encrypted pin*/
resp.dump(System.out, "MC BA Command Dump - ");
To view this discussion on the web visit https://groups.google.com/d/msgid/jpos-users/49c66891-62eb-4a69-86f4-d1366db771dbn%40googlegroups.com.