X-Git-Url: https://git.ktnx.net/?a=blobdiff_plain;ds=sidebyside;f=app%2Fsrc%2Fmain%2Fjava%2Fnet%2Fktnx%2Fmobileledger%2Futils%2FDigest.java;fp=app%2Fsrc%2Fmain%2Fjava%2Fnet%2Fktnx%2Fmobileledger%2Futils%2FDigest.java;h=c25ca59e9f5951da107728d19afd04fb8f1948c8;hb=a983099d6150044a08a1fbc601d9af9bd8d0c0a8;hp=0000000000000000000000000000000000000000;hpb=81a8e4780e37be81713d8b5b3a3e92ce9d291a78;p=mobile-ledger.git diff --git a/app/src/main/java/net/ktnx/mobileledger/utils/Digest.java b/app/src/main/java/net/ktnx/mobileledger/utils/Digest.java new file mode 100644 index 00000000..c25ca59e --- /dev/null +++ b/app/src/main/java/net/ktnx/mobileledger/utils/Digest.java @@ -0,0 +1,89 @@ +/* + * Copyright © 2018 Damyan Ivanov. + * This file is part of Mobile-Ledger. + * Mobile-Ledger is free software: you can distribute it and/or modify it + * under the term of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your opinion), any later version. + * + * Mobile-Ledger is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License terms for details. + * + * You should have received a copy of the GNU General Public License + * along with Mobile-Ledger. If not, see . + */ + +package net.ktnx.mobileledger.utils; + +import java.nio.ByteBuffer; +import java.security.DigestException; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; + +public class Digest { + private MessageDigest digest; + public Digest(String type) throws NoSuchAlgorithmException { + digest = MessageDigest.getInstance(type); + } + public static char[] hexDigitsFor(byte x) { + return hexDigitsFor((int) ((x<0) ? 256+x : x)); + } + public static char[] hexDigitsFor(int x) { + if ((x < 0) || (x > 255)) throw new ArithmeticException( + String.format("Hex digits must be between 0 and 255 (argument: %d)", x)); + char[] result = new char[]{0, 0}; + result[0] = hexDigitFor(x / 16); + result[1] = hexDigitFor(x % 16); + + return result; + } + public static char hexDigitFor(int x) { + if (x < 0) throw new ArithmeticException( + String.format("Hex digits can't be negative (argument: %d)", x)); + if (x < 10) return (char) ('0' + x); + if (x < 16) return (char) ('a' + x - 10); + throw new ArithmeticException( + String.format("Hex digits can't be greater than 15 (argument: %d)", x)); + } + public void update(byte input) { + digest.update(input); + } + public void update(byte[] input, int offset, int len) { + digest.update(input, offset, len); + } + public void update(byte[] input) { + digest.update(input); + } + public void update(ByteBuffer input) { + digest.update(input); + } + public byte[] digest() { + return digest.digest(); + } + public int digest(byte[] buf, int offset, int len) throws DigestException { + return digest.digest(buf, offset, len); + } + public byte[] digest(byte[] input) { + return digest.digest(input); + } + public String digestToHexString() { + byte[] digest = digest(); + StringBuilder result = new StringBuilder(); + for (int i = 0; i < getDigestLength(); i++) { + result.append(hexDigitsFor(digest[i])); + } + return result.toString(); + } + public void reset() { + digest.reset(); + } + public int getDigestLength() { + return digest.getDigestLength(); + } + @Override + public Object clone() throws CloneNotSupportedException { + return digest.clone(); + } +}