OmemoFingerprint.java

  1. /**
  2.  *
  3.  * Copyright 2017 Paul Schaub
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.jivesoftware.smackx.omemo.trust;

  18. public class OmemoFingerprint implements CharSequence {

  19.     private final String fingerprintString;

  20.     public OmemoFingerprint(String fingerprintString) {
  21.         this.fingerprintString = fingerprintString;
  22.     }

  23.     @Override
  24.     public int length() {
  25.         return fingerprintString.length();
  26.     }

  27.     @Override
  28.     public char charAt(int index) {
  29.         return fingerprintString.charAt(index);
  30.     }

  31.     @Override
  32.     public CharSequence subSequence(int start, int end) {
  33.         return fingerprintString.subSequence(start, end);
  34.     }

  35.     @Override
  36.     public String toString() {
  37.         return fingerprintString;
  38.     }

  39.     @Override
  40.     public boolean equals(Object other) {
  41.         if (!(other instanceof OmemoFingerprint)) {
  42.             return false;
  43.         }
  44.         OmemoFingerprint otherFingerprint = (OmemoFingerprint) other;
  45.         return this.toString().trim().equals(otherFingerprint.toString().trim());
  46.     }

  47.     /**
  48.      * Split the fingerprint in blocks of 8 characters with spaces between.
  49.      *
  50.      * @return Block representation of the fingerprint.
  51.      */
  52.     public String blocksOf8Chars() {
  53.         StringBuilder pretty = new StringBuilder();
  54.         for (int i = 0; i < 8; i++) {
  55.             if (i != 0) pretty.append(' ');
  56.             pretty.append(this.fingerprintString.substring(8 * i, 8 * (i + 1)));
  57.         }
  58.         return pretty.toString();
  59.     }

  60.     @Override
  61.     public int hashCode() {
  62.         return toString().hashCode();
  63.     }
  64. }