/*-
 * ~~~~~~licensing~~~~~~
 * basic-auth-impl
 * ==========
 * Copyright (C) 2020 - 2025 EmDev LLC
 * ==========
 * You may not use this file except in accordance with the License Terms of the Copyright
 * Holder located at: https://entaxy.ru/eula . All copyrights, all intellectual property
 * rights to the Software and any copies are the property of the Copyright Holder. Unless
 * it is explicitly allowed the Copyright Holder, the User is prohibited from using the
 * Software for commercial purposes to provide services to third parties.
 * 
 * The Copyright Holder hereby declares that the Software is provided on an "AS IS".
 * Under no circumstances does the Copyright Holder guarantee or promise that the
 * Software provided by him will be suitable or not suitable for the specific purposes
 * of the User, that the Software will meet all commercial and personal subjective
 * expectations of the User, that the Software will work properly, without technical
 * errors, quickly and uninterruptedly.
 * 
 * Under no circumstances shall the Copyright Holder or its Affiliates is not liable
 * to the User for any direct or indirect losses of the User, his expenses or actual
 * damage, including, downtime; loss of bussines; lost profit; lost earnings; loss
 * or damage to data, property, etc.
 * ~~~~~~/licensing~~~~~~
 */
package ru.entaxy.esb.system.auth.basic.jpa.util;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.codec.digest.Md5Crypt;
import org.apache.commons.codec.digest.Sha2Crypt;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ru.entaxy.esb.system.auth.basic.jpa.api.entity.field.EncryptionAlgorithm;

import java.nio.charset.StandardCharsets;
import java.security.NoSuchAlgorithmException;

public class EncryptionHelper {

    private static final Logger LOG = LoggerFactory.getLogger(EncryptionHelper.class);

    static final String APR1_PREFIX = "$apr1$";
    static final String SHA512_PREFIX = "$6$";

    private EncryptionHelper() {
        throw new IllegalStateException("Utility class");
    }

    public static String encrypt(String plaintext, String cipher, String salt) throws NoSuchAlgorithmException {
        String passwordHash;

        if (EncryptionAlgorithm.MD5.equalsName(cipher)) {
            passwordHash = Md5Crypt.apr1Crypt((plaintext.getBytes(StandardCharsets.UTF_8)), salt);
            passwordHash = passwordHash.substring(passwordHash.lastIndexOf("$") + 1);
        } else if (EncryptionAlgorithm.SHA1.equalsName(cipher)) {
            passwordHash = Base64.encodeBase64String(
                    DigestUtils.sha1((plaintext + salt).getBytes(StandardCharsets.UTF_8)));
        } else if (EncryptionAlgorithm.SHA512.equalsName(cipher)) {
            passwordHash = Sha2Crypt.sha512Crypt((plaintext.getBytes(StandardCharsets.UTF_8)), SHA512_PREFIX + salt);
            passwordHash = passwordHash.substring(passwordHash.lastIndexOf("$"));
        } else if (EncryptionAlgorithm.PLAIN.equalsName(cipher)) {
            passwordHash = plaintext;
        } else {
            throw new NoSuchAlgorithmException();
        }

        return passwordHash;
    }

}
