001/**
002 *
003 * Copyright 2017 Paul Schaub
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.jivesoftware.smackx.omemo.trust;
018
019public class OmemoFingerprint implements CharSequence {
020
021    private final String fingerprintString;
022
023    public OmemoFingerprint(String fingerprintString) {
024        this.fingerprintString = fingerprintString;
025    }
026
027    @Override
028    public int length() {
029        return fingerprintString.length();
030    }
031
032    @Override
033    public char charAt(int index) {
034        return fingerprintString.charAt(index);
035    }
036
037    @Override
038    public CharSequence subSequence(int start, int end) {
039        return fingerprintString.subSequence(start, end);
040    }
041
042    @Override
043    public String toString() {
044        return fingerprintString;
045    }
046
047    @Override
048    public boolean equals(Object other) {
049        if (!(other instanceof OmemoFingerprint)) {
050            return false;
051        }
052        OmemoFingerprint otherFingerprint = (OmemoFingerprint) other;
053        return this.toString().trim().equals(otherFingerprint.toString().trim());
054    }
055
056    /**
057     * Split the fingerprint in blocks of 8 characters with spaces between.
058     *
059     * @return Block representation of the fingerprint.
060     */
061    public String blocksOf8Chars() {
062        StringBuilder pretty = new StringBuilder();
063        for (int i = 0; i < 8; i++) {
064            if (i != 0) pretty.append(' ');
065            pretty.append(this.fingerprintString.substring(8 * i, 8 * (i + 1)));
066        }
067        return pretty.toString();
068    }
069
070    @Override
071    public int hashCode() {
072        return toString().hashCode();
073    }
074}