|
5 | 5 | */ |
6 | 6 | package org.whispersystems.libsignal.fingerprint; |
7 | 7 |
|
8 | | -import org.whispersystems.libsignal.IdentityKey; |
9 | 8 | import org.whispersystems.libsignal.util.ByteUtil; |
10 | | -import org.whispersystems.libsignal.util.IdentityKeyComparator; |
11 | 9 |
|
12 | | -import java.io.ByteArrayOutputStream; |
13 | | -import java.security.MessageDigest; |
14 | | -import java.security.NoSuchAlgorithmException; |
15 | | -import java.util.ArrayList; |
16 | | -import java.util.Collections; |
17 | | -import java.util.LinkedList; |
18 | | -import java.util.List; |
| 10 | +public class DisplayableFingerprint { |
19 | 11 |
|
20 | | -public class DisplayableFingerprint extends BaseFingerprintType { |
| 12 | + private final String localFingerprintNumbers; |
| 13 | + private final String remoteFingerprintNumbers; |
21 | 14 |
|
22 | | - private static final int VERSION = 0; |
23 | | - |
24 | | - private final String localFingerprint; |
25 | | - private final String remoteFingerprint; |
26 | | - |
27 | | - DisplayableFingerprint(int iterations, |
28 | | - String localStableIdentifier, final IdentityKey localIdentityKey, |
29 | | - String remoteStableIdentifier, final IdentityKey remoteIdentityKey) |
30 | | - { |
31 | | - this(iterations, localStableIdentifier, |
32 | | - new LinkedList<IdentityKey>(){{ |
33 | | - add(localIdentityKey); |
34 | | - }}, |
35 | | - remoteStableIdentifier, |
36 | | - new LinkedList<IdentityKey>() {{ |
37 | | - add(remoteIdentityKey); |
38 | | - }}); |
39 | | - } |
40 | | - |
41 | | - DisplayableFingerprint(int iterations, |
42 | | - String localStableIdentifier, List<IdentityKey> localIdentityKeys, |
43 | | - String remoteStableIdentifier, List<IdentityKey> remoteIdentityKeys) |
| 15 | + DisplayableFingerprint(byte[] localFingerprint, byte[] remoteFingerprint) |
44 | 16 | { |
45 | | - this.localFingerprint = getDisplayStringFor(iterations, localStableIdentifier, localIdentityKeys); |
46 | | - this.remoteFingerprint = getDisplayStringFor(iterations, remoteStableIdentifier, remoteIdentityKeys); |
| 17 | + this.localFingerprintNumbers = getDisplayStringFor(localFingerprint); |
| 18 | + this.remoteFingerprintNumbers = getDisplayStringFor(remoteFingerprint); |
47 | 19 | } |
48 | 20 |
|
49 | 21 | public String getDisplayText() { |
50 | | - if (localFingerprint.compareTo(remoteFingerprint) <= 0) { |
51 | | - return localFingerprint + remoteFingerprint; |
| 22 | + if (localFingerprintNumbers.compareTo(remoteFingerprintNumbers) <= 0) { |
| 23 | + return localFingerprintNumbers + remoteFingerprintNumbers; |
52 | 24 | } else { |
53 | | - return remoteFingerprint + localFingerprint; |
| 25 | + return remoteFingerprintNumbers + localFingerprintNumbers; |
54 | 26 | } |
55 | 27 | } |
56 | 28 |
|
57 | | - private String getDisplayStringFor(int iterations, String stableIdentifier, List<IdentityKey> unsortedIdentityKeys) { |
58 | | - try { |
59 | | - ArrayList<IdentityKey> sortedIdentityKeys = new ArrayList<>(unsortedIdentityKeys); |
60 | | - Collections.sort(sortedIdentityKeys, new IdentityKeyComparator()); |
61 | | - |
62 | | - MessageDigest digest = MessageDigest.getInstance("SHA-512"); |
63 | | - byte[] publicKey = getLogicalKeyBytes(sortedIdentityKeys); |
64 | | - byte[] hash = ByteUtil.combine(ByteUtil.shortToByteArray(VERSION), |
65 | | - publicKey, stableIdentifier.getBytes()); |
66 | | - |
67 | | - for (int i=0;i<iterations;i++) { |
68 | | - digest.update(hash); |
69 | | - hash = digest.digest(publicKey); |
70 | | - } |
71 | | - |
72 | | - return getEncodedChunk(hash, 0) + |
73 | | - getEncodedChunk(hash, 5) + |
74 | | - getEncodedChunk(hash, 10) + |
75 | | - getEncodedChunk(hash, 15) + |
76 | | - getEncodedChunk(hash, 20) + |
77 | | - getEncodedChunk(hash, 25); |
78 | | - } catch (NoSuchAlgorithmException e) { |
79 | | - throw new AssertionError(e); |
80 | | - } |
| 29 | + private String getDisplayStringFor(byte[] fingerprint) { |
| 30 | + return getEncodedChunk(fingerprint, 0) + |
| 31 | + getEncodedChunk(fingerprint, 5) + |
| 32 | + getEncodedChunk(fingerprint, 10) + |
| 33 | + getEncodedChunk(fingerprint, 15) + |
| 34 | + getEncodedChunk(fingerprint, 20) + |
| 35 | + getEncodedChunk(fingerprint, 25); |
81 | 36 | } |
82 | 37 |
|
83 | 38 | private String getEncodedChunk(byte[] hash, int offset) { |
|
0 commit comments