| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.content.pm; |
| 18 |
|
| 19 |
import android.annotation.Nullable; |
| 20 |
import android.os.Parcel; |
| 21 |
import android.os.Parcelable; |
| 22 |
|
| 23 |
import com.android.internal.annotations.VisibleForTesting; |
| 24 |
|
| 25 |
import java.io.UnsupportedEncodingException; |
| 26 |
import java.security.SecureRandom; |
| 27 |
import java.util.Random; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
public class VerifierDeviceIdentity implements Parcelable { |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
private static final int LONG_SIZE = 13; |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
private static final int GROUP_SIZE = 4; |
| 51 |
|
| 52 |
private final long mIdentity; |
| 53 |
|
| 54 |
private final String mIdentityString; |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
public VerifierDeviceIdentity(long identity) { |
| 63 |
mIdentity = identity; |
| 64 |
mIdentityString = encodeBase32(identity); |
| 65 |
} |
| 66 |
|
| 67 |
private VerifierDeviceIdentity(Parcel source) { |
| 68 |
final long identity = source.readLong(); |
| 69 |
|
| 70 |
mIdentity = identity; |
| 71 |
mIdentityString = encodeBase32(identity); |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
public static VerifierDeviceIdentity generate() { |
| 80 |
final SecureRandom sr = new SecureRandom(); |
| 81 |
return generate(sr); |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
@VisibleForTesting |
| 93 |
static VerifierDeviceIdentity generate(Random rng) { |
| 94 |
long identity = rng.nextLong(); |
| 95 |
return new VerifierDeviceIdentity(identity); |
| 96 |
} |
| 97 |
|
| 98 |
private static final char ENCODE[] = { |
| 99 |
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', |
| 100 |
'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', |
| 101 |
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', |
| 102 |
'Y', 'Z', '2', '3', '4', '5', '6', '7', |
| 103 |
}; |
| 104 |
|
| 105 |
private static final char SEPARATOR = '-'; |
| 106 |
|
| 107 |
private static final String encodeBase32(long input) { |
| 108 |
final char[] alphabet = ENCODE; |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
final char encoded[] = new char[LONG_SIZE + (LONG_SIZE / GROUP_SIZE)]; |
| 115 |
|
| 116 |
int index = encoded.length; |
| 117 |
for (int i = 0; i < LONG_SIZE; i++) { |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
if (i > 0 && (i % GROUP_SIZE) == (LONG_SIZE % GROUP_SIZE)) { |
| 125 |
encoded[--index] = SEPARATOR; |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
final int group = (int) (input & 0x1F); |
| 132 |
input >>>= 5; |
| 133 |
|
| 134 |
encoded[--index] = alphabet[group]; |
| 135 |
} |
| 136 |
|
| 137 |
return String.valueOf(encoded); |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
private static final long decodeBase32(byte[] input) throws IllegalArgumentException { |
| 142 |
long output = 0L; |
| 143 |
int numParsed = 0; |
| 144 |
|
| 145 |
final int N = input.length; |
| 146 |
for (int i = 0; i < N; i++) { |
| 147 |
final int group = input[i]; |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
final int value; |
| 154 |
if ('A' <= group && group <= 'Z') { |
| 155 |
value = group - 'A'; |
| 156 |
} else if ('2' <= group && group <= '7') { |
| 157 |
value = group - ('2' - 26); |
| 158 |
} else if (group == SEPARATOR) { |
| 159 |
continue; |
| 160 |
} else if ('a' <= group && group <= 'z') { |
| 161 |
|
| 162 |
value = group - 'a'; |
| 163 |
} else if (group == '0') { |
| 164 |
|
| 165 |
value = 'O' - 'A'; |
| 166 |
} else if (group == '1') { |
| 167 |
|
| 168 |
value = 'I' - 'A'; |
| 169 |
} else { |
| 170 |
throw new IllegalArgumentException("base base-32 character: " + group); |
| 171 |
} |
| 172 |
|
| 173 |
output = (output << 5) | value; |
| 174 |
numParsed++; |
| 175 |
|
| 176 |
if (numParsed == 1) { |
| 177 |
if ((value & 0xF) != value) { |
| 178 |
throw new IllegalArgumentException("illegal start character; will overflow"); |
| 179 |
} |
| 180 |
} else if (numParsed > 13) { |
| 181 |
throw new IllegalArgumentException("too long; should have 13 characters"); |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
if (numParsed != 13) { |
| 186 |
throw new IllegalArgumentException("too short; should have 13 characters"); |
| 187 |
} |
| 188 |
|
| 189 |
return output; |
| 190 |
} |
| 191 |
|
| 192 |
@Override |
| 193 |
public int hashCode() { |
| 194 |
return (int) mIdentity; |
| 195 |
} |
| 196 |
|
| 197 |
@Override |
| 198 |
public boolean equals(@Nullable Object other) { |
| 199 |
if (!(other instanceof VerifierDeviceIdentity)) { |
| 200 |
return false; |
| 201 |
} |
| 202 |
|
| 203 |
final VerifierDeviceIdentity o = (VerifierDeviceIdentity) other; |
| 204 |
return mIdentity == o.mIdentity; |
| 205 |
} |
| 206 |
|
| 207 |
@Override |
| 208 |
public String toString() { |
| 209 |
return mIdentityString; |
| 210 |
} |
| 211 |
|
| 212 |
public static VerifierDeviceIdentity parse(String deviceIdentity) |
| 213 |
throws IllegalArgumentException { |
| 214 |
final byte[] input; |
| 215 |
try { |
| 216 |
input = deviceIdentity.getBytes("US-ASCII"); |
| 217 |
} catch (UnsupportedEncodingException e) { |
| 218 |
throw new IllegalArgumentException("bad base-32 characters in input"); |
| 219 |
} |
| 220 |
|
| 221 |
return new VerifierDeviceIdentity(decodeBase32(input)); |
| 222 |
} |
| 223 |
|
| 224 |
@Override |
| 225 |
public int describeContents() { |
| 226 |
return 0; |
| 227 |
} |
| 228 |
|
| 229 |
@Override |
| 230 |
public void writeToParcel(Parcel dest, int flags) { |
| 231 |
dest.writeLong(mIdentity); |
| 232 |
} |
| 233 |
|
| 234 |
public static final @android.annotation.NonNull Parcelable.Creator<VerifierDeviceIdentity> CREATOR |
| 235 |
= new Parcelable.Creator<VerifierDeviceIdentity>() { |
| 236 |
public VerifierDeviceIdentity createFromParcel(Parcel source) { |
| 237 |
return new VerifierDeviceIdentity(source); |
| 238 |
} |
| 239 |
|
| 240 |
public VerifierDeviceIdentity[] newArray(int size) { |
| 241 |
return new VerifierDeviceIdentity[size]; |
| 242 |
} |
| 243 |
}; |
| 244 |
} |