public class SignatureRecordDecoder implements WellKnownRecordPayloadDecoder {
@Override
public WellKnownRecord decodePayload(byte[] payload, NdefMessageDecoder messageDecoder) {
SignatureRecord signatureRecord = new SignatureRecord();
try {
ByteArrayInputStream bais = new ByteArrayInputStream(payload);
int version = bais.read();
signatureRecord.setVersion((byte)version);
int header = bais.read();
boolean signatureUriPresent = (header & 0x80) != 0;
SignatureType type = SignatureType.toSignatureType((header & 0x7F));
signatureRecord.setSignatureType(type);
if(signatureUriPresent || type != SignatureType.NOT_PRESENT) {
int size = RecordUtils.readUnsignedShort(bais);
if(size > 0) {
byte[] signatureOrUri = RecordUtils.readByteArray(bais, size);
if(signatureUriPresent) {
signatureRecord.setSignatureUri(new String(signatureOrUri, NdefConstants.UTF_8_CHARSET));
} else {
signatureRecord.setSignature(signatureOrUri);
}
}
int certificateHeader = bais.read();
signatureRecord.setCertificateFormat(CertificateFormat.toCertificateFormat((certificateHeader >> 4) & 0x7));
int numberOfCertificates = certificateHeader & 0xF;
for(int i = 0; i < numberOfCertificates; i++) {
int certificateSize = RecordUtils.readUnsignedShort(bais);
byte[] certificate = RecordUtils.readByteArray(bais, certificateSize);
signatureRecord.add(certificate);
}
if((certificateHeader & 0x80) != 0) { // has certificate uri
int certificateUriSize = RecordUtils.readUnsignedShort(bais);
byte[] certificateUri = RecordUtils.readByteArray(bais, certificateUriSize);
signatureRecord.setCertificateUri(new String(certificateUri, NdefConstants.UTF_8_CHARSET));
}
} else {
// start marker
}