Skip to content

Instantly share code, notes, and snippets.

@xtuc
Last active September 28, 2015 11:27
Show Gist options
  • Save xtuc/1e9bb71fb1eb91ba3fb0 to your computer and use it in GitHub Desktop.
Save xtuc/1e9bb71fb1eb91ba3fb0 to your computer and use it in GitHub Desktop.
Java card
package testPackage;
/*
* Package: testPackage
* Filename: TestApplet.java
* Class: TestApplet
* Date: [[10 June]] of [[2015]] 14:55:52
*/
import javacard.framework.*;
/*
* class TestApplet
*/
public class TestApplet extends javacard.framework.Applet
{
// CLA Byte
final static byte HELLO_CLA = (byte) 0xB0;
// Verify PIN
final static byte INS_HELLO = (byte) 0x20;
public static void install (byte [] barray, short bOffset, byte bLength)
{
(new TestApplet()).register(barray,(short) (bOffset + 1), barray[bOffset]);
}
// Process the command APDU
public void process(APDU apdu)
{
byte [] buffer = apdu.getBuffer();
if((buffer[ISO7816.OFFSET_CLA] == 0) && (buffer [ISO7816. OFFSET_INS] == (byte) (0xa4)))
{
return;
}
// Validate the CLA byte
if(buffer[ISO7816.OFFSET_CLA] != HELLO_CLA)
{
ISOException.throwIt(ISO7816.SW_CLA_NOT_SUPPORTED);
}
// Select the appropriate instruction byte (INS)
switch(buffer[ISO7816. OFFSET_INS])
{
case INS_HELLO: getHello(apdu); return;
default: ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED);
}
}
/*
* param APDU
* author Igor Medeiros
* Get user id attribute
*/
private void getHello(APDU apdu)
{
// Byte string with the message "Hello World Java Card"
byte hello[] = {'H','E','L','L','O',' ','W','O','R','L','D'};
// Tells the JCRE to be sent a reply
short le = apdu.setOutgoing();
short totalBytes = (short) hello.length;
// Tells the JCRE the message size in bytes
apdu.setOutgoingLength(totalBytes);
// Send the mensgem to the host
apdu.sendBytes((short) 0, (short) hello.length);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment