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

com.cybermkd.common.util.crypto.Cryptor Maven / Gradle / Ivy

package com.cybermkd.common.util.crypto;

import com.cybermkd.common.http.Encoding;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.security.GeneralSecurityException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;

public class Cryptor {

    private static List mds = new ArrayList() {{
        add("MD2");
//    add("MD4"); Bouncy Castle
        add("MD5");
    }};

    private static List shas = new ArrayList() {{
        add("SHA-1");
//    add("SHA-224"); Bouncy Castle
        add("SHA-256");
        add("SHA-384");
        add("SHA-512");
    }};

    private static List macs = new ArrayList() {{
//    add("HmacMD2");  Bouncy Castle
//    add("HmacMD4"); Bouncy Castle
        add("HmacMD5");
        add("HmacSHA1");
//    add("HmacSHA224");Bouncy Castle
        add("HmacSHA256");
        add("HmacSHA384");
        add("HmacSHA512");
    }};

    public static String crypto(String algorithm, String message) {
        return crypto(algorithm, message, null);
    }

    public static String crypto(String algorithm, String message, String salt) {
        String result;
        if (macs.contains(algorithm)) {
            Mac mac = null;
            try {
                mac = Mac.getInstance(algorithm);
                if (salt != null && !salt.isEmpty()) {
                    SecretKeySpec secretKey = new SecretKeySpec(salt.getBytes(Encoding.UTF_8), algorithm);
                    mac.init(secretKey);
                } else {
                    throw new CryptoException("Could not found secretKey for mac's crypto.");
                }
            } catch (GeneralSecurityException e) {
                throw new CryptoException(e.getMessage(), e);
            }
            result = Hex.encodeHexString(mac.doFinal(message.getBytes(Encoding.UTF_8)));
        } else if (mds.contains(algorithm) || shas.contains(algorithm)) {
            MessageDigest md = null;
            try {
                md = MessageDigest.getInstance(algorithm);
            } catch (NoSuchAlgorithmException e) {
                throw new CryptoException(e.getMessage(), e);
            }

            if (salt != null && !salt.isEmpty()) {
                md.update(salt.getBytes(Encoding.UTF_8));
            }
            result = Hex.encodeHexString(md.digest(message.getBytes()));
        } else {
            throw new CryptoException("Could not support this crypto's type " + algorithm + ".");
        }

        return result;
    }
}








© 2015 - 2024 Weber Informatics LLC | Privacy Policy