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

anlavn.hash.SHA Maven / Gradle / Ivy

The newest version!
package anlavn.hash;
// Make By Bình An || AnLaVN || KatoVN

import anlavn.file.Log;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**The SHA256 class only supports object encryption, decryption it is impossible. Make sure your object class was "implements Serializable".
* SHA256 uses the Secure Hash Algorithm 2 cryptographic hash function designed by the US National Security Agency with a 256-bit long hash value. * @author AnLaVN - https://github.com/AnLaVN */ public class SHA { private static String encrypt(Types type, final byte[] bytes){ try { final MessageDigest digest = MessageDigest.getInstance(type.toString().replaceFirst("_", "-")); final byte[] hash = digest.digest(bytes); final StringBuilder hexString = new StringBuilder(); for (int i = 0; i < hash.length; i++) { final String hex = Integer.toHexString(0xff & hash[i]); if (hex.length() == 1) hexString.append('0'); hexString.append(hex); } return hexString.toString(); } catch (NoSuchAlgorithmException e) { Log.add("!!! Error try to Encrypt SHA256 !!!\n\tError code: " + e.toString()); throw new RuntimeException(e); } } /**The SHA (Secure Hash Algorithm) Family designates a family of 8 different hash functions. */ public enum Types{ /** Output: 224 bits ~ 56 characters */ SHA_224, /** Output: 256 bits ~ 64 characters */ SHA_256, /** Output: 384 bits ~ 96 characters */ SHA_384, /** Output: 512 bits ~ 128 characters */ SHA_512, /** Output: 224 bits ~ 56 characters */ SHA3_224, /** Output: 256 bits ~ 64 characters */ SHA3_256, /** Output: 384 bits ~ 96 characters */ SHA3_384, /** Output: 512 bits ~ 128 characters */ SHA3_512 } /**Use this method to encrypt the original object. * @param type is the type in SHA Family use to encrypt.
* @param objToEncrypt is the object need encrypt.
* @return a SHA256 hash code of the original object. * @see SHA#encrypt(anlavn.hash.SHA.Types, java.lang.String) */ public static final String encrypt(Types type, final Object objToEncrypt) { try { final ByteArrayOutputStream bos = new ByteArrayOutputStream(); final ObjectOutputStream oos = new ObjectOutputStream(bos); oos.writeObject(objToEncrypt); oos.flush(); return encrypt(type, bos.toByteArray()); } catch (IOException e) { Log.add("!!! Error try to Encrypt SHA256 an Object. Can not parse Object to bytes. !!!\n\tError code: " + e.toString()); return null; } } /**Use this method to encrypt the original string. * @param type is the type in SHA Family use to encrypt.
* @param strToEncrypt is the string need encrypt.
* @return a SHA256 hash code of the original string. * @see SHA#encrypt(anlavn.hash.SHA.Types, java.lang.Object) */ public static final String encrypt(Types type, final String strToEncrypt) { return encrypt(type, strToEncrypt.getBytes()); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy