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

com.uid2.shared.secret.KeyHasher Maven / Gradle / Ivy

package com.uid2.shared.secret;

import com.uid2.shared.Utils;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.SecureRandom;

public class KeyHasher {
    private static final int SALT_BYTES = 32;

    public byte[] hashKey(String key, byte[] salt) {
        try {
            MessageDigest md = MessageDigest.getInstance("SHA-512");
            md.update(salt);
            return md.digest(key.getBytes(StandardCharsets.UTF_8));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public KeyHashResult hashKey(String key) {
        byte[] saltBytes = generateSaltBytes();
        byte[] hashBytes = hashKey(key, saltBytes);
        return new KeyHashResult(Utils.toBase64String(hashBytes), Utils.toBase64String(saltBytes));
    }

    private static byte[] generateSaltBytes() {
        SecureRandom random = new SecureRandom();
        byte[] bytes = new byte[SALT_BYTES];
        random.nextBytes(bytes);
        return bytes;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy