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

it.auties.whatsapp.crypto.Hmac Maven / Gradle / Ivy

There is a newer version: 2.7.2
Show newest version
package it.auties.whatsapp.crypto;

import lombok.NonNull;
import lombok.SneakyThrows;
import lombok.experimental.UtilityClass;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

@UtilityClass
public class Hmac {
    private final String HMAC_SHA_256 = "HmacSHA256";
    private final String HMAC_SHA_512 = "HmacSHA512";

    public byte[] calculateSha256(byte @NonNull [] plain, byte @NonNull [] key) {
        return calculate(HMAC_SHA_256, plain, key);
    }

    @SneakyThrows
    private byte[] calculate(String algorithm, byte[] plain, byte[] key) {
        var localMac = Mac.getInstance(algorithm);
        localMac.init(new SecretKeySpec(key, algorithm));
        return localMac.doFinal(plain);
    }

    public byte[] calculateSha512(byte @NonNull [] plain, byte @NonNull [] key) {
        return calculate(HMAC_SHA_512, plain, key);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy