All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.pengrad.telegrambot.passport.decrypt.SecretHash Maven / Gradle / Ivy

There is a newer version: 7.11.0
Show newest version
package com.pengrad.telegrambot.passport.decrypt;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;

/**
 * Stas Parshin
 * 31 July 2018
 */
class SecretHash {

    private final byte[] secretHash;

    public SecretHash(byte[] secret, byte[] hash) throws Exception {
        secretHash = sha512(concat(secret, hash));
    }

    public byte[] key() {
        return Arrays.copyOfRange(secretHash, 0, 32);
    }

    public byte[] iv() {
        return Arrays.copyOfRange(secretHash, 32, 48);
    }

    private byte[] sha512(byte[] string) throws NoSuchAlgorithmException {
        MessageDigest md = MessageDigest.getInstance("SHA-512");
        return md.digest(string);
    }

    private byte[] concat(byte[]... arrays) {
        int length = 0;
        for (byte[] array : arrays) {
            length += array.length;
        }
        byte[] result = new byte[length];
        int pos = 0;
        for (byte[] array : arrays) {
            for (byte element : array) {
                result[pos] = element;
                pos++;
            }
        }
        return result;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy