Java Card Application Development
By Darryl Barnes
Dr. Dobb's Journal February 1999
protected void validatePIN(APDU _apdu) { // get reference to apdu buffer baBuffer = _apdu.getBuffer(); // get data from terminal byte bLc = (byte)_apdu.setIncomingAndReceive(); // check length being sent if(bLc != PIN_LENGTH) ISOException.throwIt(ISO.SW_WRONG_LENGTH); // validate PIN if(pin.check(baBuffer, ISO.OFFSET_CDATA, PIN_LENGTH)) zIsAuthorized = true; else ISOException.throwIt(ISO.SW_SECURITY_STATUS_NOT_SATISFIED); } protected void debit(APDU _apdu) { // check if PIN is validated if(!zIsAuthorized) ISOException.throwIt(ISO.SW_SECURITY_STATUS_NOT_SATISFIED); // get reference to apdu buffer baBuffer = _apdu.getBuffer(); // get data from terminal byte bLc = (byte)_apdu.setIncomingAndReceive(); // get amount from apdu buffer short sAmount = Util.getShort(baBuffer, ISO.OFFSET_P1); // don't allow negative balance if((sPoints - sAmount) < 0) sPoints = 0; else // debit account sPoints = (short)(sPoints - sAmount); } protected void credit(APDU _apdu) { // check if PIN is validated if(!zIsAuthorized) ISOException.throwIt(ISO.SW_SECURITY_STATUS_NOT_SATISFIED); // get reference to apdu buffer baBuffer = _apdu.getBuffer(); // get data from terminal byte bLc = (byte)_apdu.setIncomingAndReceive(); // get amount from apdu buffer short sAmount = Util.getShort(baBuffer, ISO.OFFSET_P1); // increment balance sPoints = (short)(sPoints + sAmount); } protected void getBalance(APDU _apdu) { // check access permissions if(!zIsAuthorized) ISOException.throwIt(ISO.SW_SECURITY_STATUS_NOT_SATISFIED); // get reference to apdu buffer baBuffer = _apdu.getBuffer(); // put balance value into apdu buffer Util.setShort(baBuffer, ISO.OFFSET_CDATA, sPoints); // send balance _apdu.setOutgoingAndSend(ISO.OFFSET_CDATA, (short)2); }
Example 4: Methods that handle the loyalty functionality.
Copyright © 1999, Dr. Dobb's Journal