Download X - LIFC
Transcript
k6 package wallet ; import javacard.framework.*; public class Wallet extends Applet ... final static byte WALLET = 0xAA ; // codes of INS byte in the command APDU header final static byte GET = 0x01 ; final static byte CREDIT = 0x02 ; final static byte DEBIT = 0x03 ; final static short MAX BALANCE = 10000 ; final static short MAX TRANSACTION AMOUNT = 100 ; private short balance ; public Wallet() balance = 0 ; register() ; public static void install( APDU apdu ) new Wallet() ; public void process( APDU apdu ) byte[] buffer = apdu.getBuffer(); if ( buffer[ISO.OFFSET CLA] != WALLET ) ISOException.throwIt(ISO.SW CLA NOT SUPPORTED) ; switch (buffer[ ISO.OFFSET INS]) case GET : getBalance(buffer); break ; case CREDIT : credit(buffer); break ; case DEBIT : debit(buffer); break ; default : ISOException.throwIt(ISO.SW INS NOT SUPPORTED) ; ... void credit( Byte[] buffer) byte byteRead = (byte) apdu.setIncomingAndReceive(); if (byteRead != 1) ISOException. throwIt( ISO.SW WRONG LENGTH) ; short creditAmount = (buffer[ISO.OFFSET CDATA] 8) buffer[ISO.OFFSET CDATA+1] ; // process data if ((creditAmount MAX TRANSACTION AMOUNT) (creditAmount 0 )) ISOException.throwIt (INVALID TRANSACTION AMOUNT) ; // check the new balance if ((balance - creditAmount) MAX BALANCE) ISOException.throwIt(EXCEED MAXIMUM BALANCE) ; balance = balance - creditAmount ; §#¨©aª; lW h« & Mq "! # $% &