Instantly share code, notes, and snippets.

Embed
What would you like to do?
package net.moznion.javacard.helloworld;
import javacard.framework.*;
import javacardx.framework.*;
public class HelloWorldApplet extends Applet {
private final static byte HELLO_WORLD_CLA = (byte)0xB0;
private final static byte SELECT_INS = (byte)0xA4;
private final static byte HELLO_INS = (byte)0x50;
private final static byte[] HELLO_TEXT = {(byte)'H',(byte)'e',(byte)'l',(byte)'l',(byte)'o'};
private HelloWorldApplet() {
register();
}
public static void install(byte[] bArray, short bOffset, byte bLength) {
new HelloWorldApplet();
}
public boolean select() {
return true;
}
public void deselect() {
}
public void process(APDU apdu) {
byte[] buff = apdu.getBuffer();
// ignore SELECT instruction
if (buff[ISO7816.OFFSET_CLA] == 0 && buff[ISO7816.OFFSET_INS] == SELECT_INS) {
return;
}
// check CLA
if (buff[ISO7816.OFFSET_CLA] != HELLO_WORLD_CLA) {
ISOException.throwIt(ISO7816.SW_CLA_NOT_SUPPORTED);
}
switch (buff[ISO7816.OFFSET_INS]) {
case HELLO_INS:
// do something
short le = apdu.setOutgoing();
if (le < (short)5) {
ISOException.throwIt(ISO7816.SW_WRONG_LENGTH);
}
apdu.setOutgoingLength((short)5);
buff[0] = HELLO_TEXT[0];
buff[1] = HELLO_TEXT[1];
buff[2] = HELLO_TEXT[2];
buff[3] = HELLO_TEXT[3];
buff[4] = HELLO_TEXT[4];
apdu.sendBytes((short)0, (short)5);
return;
default:
ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED);
}
}
public Shareable getShareableInterfaceObject(AID clientAID, byte parameter){
return (Shareable)this;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment