Skip to content

Instantly share code, notes, and snippets.

View Franzaine's full-sized avatar

Theo Franzén Franzaine

  • Fidesmo AB
  • Stockholm, Sweden
View GitHub Profile
@Franzaine
Franzaine / MainActivity.java
Created October 15, 2015 12:24
Offer user to install applet if applet select failed
byte[] response = isoCard.transceive(Apdu.select(APPLICATION_ID, APP_VERSION));
if (Apdu.hasStatus(response, Apdu.OK_APDU)) {
/* ... */
} else {
setMainMessage("Select not OK");
setMainMessage(R.string.cardlet_not_installed);
installButton.setVisibility(View.VISIBLE);
}
@Franzaine
Franzaine / MainActivity.java
Created October 15, 2015 12:17
Parse readable response from APDU response
/* ... */
byte[] response = isoCard.transceive(Apdu.select(APPLICATION_ID, APP_VERSION));
if (Apdu.hasStatus(response, Apdu.OK_APDU)) {
setMainMessage("Select OK");
byte[] payload = Apdu.responseData(response);
String printableResponse = new String();
for (int i=0; i<payload.length; i++) printableResponse += (char)payload[i];
setMainMessage(printableResponse);
} else {
@Franzaine
Franzaine / MainActivity.java
Last active October 15, 2015 12:03
Select and check status of select
private void communicateWithCard(IsoCard isoCard) {
try {
isoCard.connect();
byte[] response = isoCard.transceive(Apdu.select(APPLICATION_ID, APP_VERSION));
if (Apdu.hasStatus(response, Apdu.OK_APDU)) {
setMainMessage("Select OK");
} else {
setMainMessage("Select not OK");
}
isoCard.close();
@Franzaine
Franzaine / MainActivity.java
Last active October 15, 2015 11:50
Connecting to the card, selecting the Hello Fidesmo! applet and closing the connection
@Override
public void tagDiscovered(Tag tag) {
setMainMessage("Reading card...");
try {
IsoCard isoCard = AndroidCard.get(tag);
communicateWithCard(isoCard);
} catch(IOException e) {
e.printStackTrace();
}
}
@Franzaine
Franzaine / MainActivity.java
Created October 15, 2015 11:23
Generate an IsoCard from a Tag
@Override
public void tagDiscovered(Tag tag) {
Log.v("MainActivity", "Card detected on the NFC interface!");
try {
IsoCard isoCard = AndroidCard.get(tag);
} catch(IOException e) {
e.printStackTrace();
}
}
@Franzaine
Franzaine / MainActivity.java
Last active October 15, 2015 14:56
Using Nordpol to enable and disable exclusive NFC for our app
@Override
protected void onResume() {
super.onResume();
tagDispatcher = TagDispatcher.get(this, this);
tagDispatcher.enableExclusiveNfc();
}
@Override
public void onPause() {
super.onPause();
@Franzaine
Franzaine / MainActivity.java
Created October 11, 2015 21:00
Generate, decipher and print TOTP code
protected void communicateWithCard(IsoCard isoCard) {
try {
isoCard.connect();
Apdu.transceiveAndRequireOk(Apdu.select(OTP_AID), isoCard);
setMainMessage("Select OK");
long timeStamp = getTimeStamp();
byte[] totpCodeApdu = Otp.totpCodeApdu(TOTP_CODE_NAME, timeStamp);
byte[] rawTotpCode = Apdu.transceiveAndGetResponse(totpCodeApdu, isoCard, Otp.SEND_REMAINING_APDU);
if(Apdu.hasStatus(rawTotpCode, Apdu.OK_APDU)){
String totpCode = Otp.decipherTotpCode(rawTotpCode);
@Franzaine
Franzaine / MainActivity.java
Created October 11, 2015 20:42
Ask card for TOTP code and receive response
protected void communicateWithCard(IsoCard isoCard) {
try {
isoCard.connect();
Apdu.transceiveAndRequireOk(Apdu.select(OTP_AID), isoCard);
setMainMessage("Select OK");
long timeStamp = getTimeStamp();
byte[] totpCodeApdu = Otp.totpCodeApdu(TOTP_CODE_NAME, timeStamp);
byte[] rawTotpCode = Apdu.transceiveAndGetResponse(totpCodeApdu, isoCard, Otp.SEND_REMAINING_APDU);
isoCard.close();
} catch (IOException e) {
@Franzaine
Franzaine / MainActivity.java
Created October 11, 2015 20:22
Fetching APDU for generating TOTP code
protected void communicateWithCard(IsoCard isoCard) {
try {
isoCard.connect();
Apdu.transceiveAndRequireOk(Apdu.select(OTP_AID), isoCard);
setMainMessage("Select OK");
long timeStamp = getTimeStamp();
byte[] totpCodeApdu = Otp.totpCodeApdu(TOTP_CODE_NAME, timeStamp);
isoCard.close();
} catch (IOException e) {
e.printStackTrace();
@Franzaine
Franzaine / MainActivity.java
Created October 11, 2015 19:55
Connecting to the card, selecting the OTP applet and closing the connection
@Override
public void tagDiscovered(Tag tag) {
setMainMessage("Reading card...");
try {
IsoCard isoCard = AndroidCard.get(tag);
communicateWithCard(isoCard);
} catch(IOException e) {
e.printStackTrace();
}
}