Repository navigation
Expand file tree
/
Copy pathAID.java
More file actions
142 lines (124 loc) · 4.14 KB
/
Copy pathAID.java
File metadata and controls
142 lines (124 loc) · 4.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
* Copyright 2010 sasc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package sasc.iso7816;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Arrays;
import sasc.lookup.RID_DB;
import sasc.terminal.KnownAIDList;
import sasc.util.Log;
import sasc.util.Util;
/**
* Application Identifier (AID)
*
* See EMV Book 1 page 153
* @author sasc
*/
public class AID {
//Registered Application Provider Identifier
private byte[] rid;
//Proprietary Application Identifier Extension
private byte[] pix;
public AID(byte[] rid, byte[] pix) {
if (rid == null || pix == null){
throw new IllegalArgumentException("Arguments 'rid' and 'pix' cannot be null");
}
if (rid.length != 5) {
throw new IllegalArgumentException("RID length != 5. Length=" + rid.length);
}
if (pix.length > 11) {
throw new IllegalArgumentException("PIX length > 11. Length=" + pix.length);
}
this.rid = rid;
this.pix = pix;
}
public AID(byte[] aid) {
if (aid == null){
throw new IllegalArgumentException("Argument 'aid' cannot be null");
}
if (aid.length < 5) {
throw new IllegalArgumentException("AID length < 5. Length=" + aid.length);
}
if (aid.length > 16) {
throw new IllegalArgumentException("AID length > 16. Length=" + aid.length);
}
rid = new byte[5];
System.arraycopy(aid, 0, rid, 0, rid.length);
pix = new byte[aid.length-rid.length];
System.arraycopy(aid, rid.length, pix, 0, pix.length);
}
public AID(String aid) {
this(Util.fromHexString(aid));
}
public byte[] getAIDBytes() {
byte[] tmp = new byte[rid.length + pix.length];
System.arraycopy(rid, 0, tmp, 0, rid.length);
System.arraycopy(pix, 0, tmp, rid.length, pix.length);
return tmp;
}
public byte[] getRIDBytes() {
return Util.copyByteArray(rid);
}
public byte[] getPIXBytes() {
return Util.copyByteArray(pix);
}
public boolean belongsToRID(byte[] rid) {
return Arrays.equals(this.rid, rid);
}
@Override
public String toString(){
StringWriter sw = new StringWriter();
dump(new PrintWriter(sw), 0);
return sw.toString();
}
public void dump(PrintWriter pw, int indent){
KnownAIDList.KnownAID knownAID = KnownAIDList.searchAID(getAIDBytes());
String aidName = "";
if(knownAID != null){
aidName = " ("+knownAID.getName()+")";
}
pw.println(Util.getSpaces(indent)+"AID: "+Util.prettyPrintHexNoWrap(getAIDBytes()) + aidName);
String indentStr = Util.getSpaces(indent+Log.INDENT_SIZE);
RID ridFromDB = RID_DB.searchRID(rid);
String description = "";
if (ridFromDB != null) {
description = " (" + ridFromDB.getApplicant() + " [" + ridFromDB.getCountry() + "])";
}
pw.println(indentStr+"RID: "+Util.prettyPrintHexNoWrap(rid) + description);
pw.println(indentStr+"PIX: "+Util.prettyPrintHexNoWrap(pix));
}
@Override
public boolean equals(Object obj){
if(!(obj instanceof AID)){
return false;
}
AID that = (AID)obj;
if(this == that){
return true;
}
if(Arrays.equals(this.getAIDBytes(), that.getAIDBytes())){
return true;
}
return false;
}
@Override
public int hashCode() {
int hash = 7;
hash = 97 * hash + Arrays.hashCode(this.rid);
hash = 97 * hash + Arrays.hashCode(this.pix);
return hash;
}
}