Data security

Preface: Based on my work in the docking pay abroad, need to study in terms of data security of hash algorithm, encryption algorithm, attestation algorithm, etc., through this opportunity, also in the nuggets started publishing technology related articles, follow-up will be here to update all the source code (Java), first of all, I want to share is about data security related source code, Including RSA, AES, and Hash algorithms such as MD5, SHA1, SHA256, and SHA512. Data security is very important for sensitive services, such as payment services, which have high requirements on data security. How to ensure data security at the application level? That is, even if an attacker obtains the request interface and transmission packet, he cannot attack our application. Therefore, data needs to be encrypted. Note: HTTPS joins SSL on the basis of HTTP to ensure data security during network transmission, but it cannot guarantee data security at the application layer. Common algorithms for data security: hashing algorithm and encryption algorithm:

Hash algorithm based on Java

package algorithm.hash;

import algorithm.hash.vo.HMACEncryptType;
import algorithm.hash.vo.SHAEncryptType;
import org.apache.commons.codec.binary.Hex;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/ * * *@author lyq
 * @FuctionHash algorithm, irreversible, encrypted data length fixed *@MethodHash encryption, Mac-based hash encryption */

public class HashAlgorithmUtils {

    /** * Hash encryption algorithm *@paramString Target character string *@paramType Encryption type {@link HashEncryptType}
     */
    public static String hashEncryption(String string, HashEncryptType type) {
        if (string==null || "".equals(string.trim())) return "";
        if (type==null) type = SHAEncryptType.SHA512;
        try {
            MessageDigest md = MessageDigest.getInstance(type.value);
            byte[] bytes = md.digest((string).getBytes());
            byte[] hex = new Hex().encode(bytes);
            return new String(hex);
        } catch (NoSuchAlgorithmException e) {

        }
        return "";
    }


    /** * Mac-based encryption algorithm *@param data
     * @param key
     * @param type {@link HMACEncryptType}
     * @return* /
    public static String hmacHashEncryption(String data, String key, HMACEncryptType type) {
        String result = "";
        byte[] bytesKey = key.getBytes();
        final SecretKeySpec secretKey = new SecretKeySpec(bytesKey, type.value);
        try {
            Mac mac = Mac.getInstance(type.value);
            mac.init(secretKey);
            final byte[] macData = mac.doFinal(data.getBytes());
            byte[] hex = new Hex().encode(macData);
            result = new String(hex);
        } catch (Exception e) {
            e.printStackTrace();
        }
        returnresult; }}Copy the code
package algorithm.hash.vo;

import lombok.AllArgsConstructor;

@AllArgsConstructor
public enum HMACEncryptType {

    HMACMD5("HmacMD5"),
    HMACSHA1("HmacSHA1"),
    HMACSHA256("HmacSHA256"),
    HMACSHA512("HmacSHA512");

    public String value;
}

Copy the code
package algorithm.hash.vo;

import lombok.AllArgsConstructor;

@AllArgsConstructor
public enum HashEncryptType {
    MD5("md5"),
    SHA1("sha-1"),
    SHA224("sha-224"),
    SHA256("sha-256"),
    SHA384("sha-384"),
    SHA512("sha-512"),;public String value;

}

Copy the code
package algorithm.hash;


import algorithm.hash.vo.HMACEncryptType;
import algorithm.hash.vo.SHAEncryptType;

public class TestHash {

    public static void main(String[] args) {
        //test hmac
        String key = "sk_live_2270105ca7e207b3b3e5a56dd04c493549cef964";
        String data = "{\"event\":\"charge.success\",\"data\":{\"id\":1183164216,\"domain\":\"live\",\"status\":\"success\",\"reference\":\"21 0621070140pch92309406\",\"amount\":50000,\"message\":null,\"gateway_response\":\"Approved by Financial Paid_at Institution \ ", \ "\", \ "the 2021-06-21 T07:01:47. 000 z \" and \ "created_at \", \ "the 2021-06-21 T07:01:41. 000 z \" and \ "channel \" : \ "card \", \ "currency \" : \ "NGN \", \ "ip_address \" : \ "52.49.173.169 \", \ "metadata \" : \ "\", \ "the log \" : null, \ "feeds \" : 400, \ "fees_split \", null, \" authorization\":{\"authorization_code\":\"AUTH_jify7ej4xz\",\"bin\":\"506105\",\"last4\":\"9270\",\"exp_month\":\"06\",\ "exp_year\":\"2022\",\"channel\":\"card\",\"card_type\":\"verve DEBIT\",\"bank\":\"First Bank of Nigeria\",\"country_code\":\"NG\",\"brand\":\"verve\",\"reusable\":true,\"signature\":\"SIG_KIrnvMgIz4nevoGpB22A\",\"acc ount_name\":\"OWANIYI LAWRENCE GBENGA\"},\"customer\":{\"id\":29527446,\"first_name\":null,\"last_name\":null,\"email\":\"200908163344puid22741529@mspo rt.com\",\"customer_code\":\"CUS_ka3zv3h6j99d2x7\",\"phone\":null,\"metadata\":null,\"risk_action\":\"default\",\"intern ational_format_phone\":null},\"plan\":{},\"subaccount\":{},\"split\":{},\"order_id\":null,\"paidAt\":\"2021-06-21T07:01: 47.000 Z \ ", \ "requested_amount \ pos_transaction_data" : 50000, \ "\", null, \ "source \" : {\ "type \" : \ "API \", \ "source \" : \ "merchant_ap i\",\"identifier\":null}},\"order\":null,\"business_name\":\"MSPORT\"}";
        String result = HashAlgorithmUtils.hmacHashEncryption(data, key, HMACEncryptType.HMACSHA512);
        System.out.println(result);

        //test hash
        String str = "abc123"; String md5 = HashAlgorithmUtils.hashEncryption(str, SHAEncryptType.MD5); String sha1 = HashAlgorithmUtils.hashEncryption(str, SHAEncryptType.SHA1); String sha256 = HashAlgorithmUtils.hashEncryption(str, SHAEncryptType.SHA256); String sha512 = HashAlgorithmUtils.hashEncryption(str, SHAEncryptType.SHA512); System.out.println(md5); System.out.println(sha1); System.out.println(sha256); System.out.println(sha512); }}Copy the code
package algorithm.hash;


import algorithm.hash.vo.HMACEncryptType;
import algorithm.hash.vo.SHAEncryptType;

public class TestHash {

    public static void main(String[] args) {
        //test hmac
        String key = "sk_live_2270105ca7e207b3b3e5a56dd04c493549cef964";
        String data = "{\"event\":\"charge.success\",\"data\":{\"id\":1183164216,\"domain\":\"live\",\"status\":\"success\",\"reference\":\"21 0621070140pch92309406\",\"amount\":50000,\"message\":null,\"gateway_response\":\"Approved by Financial Paid_at Institution \ ", \ "\", \ "the 2021-06-21 T07:01:47. 000 z \" and \ "created_at \", \ "the 2021-06-21 T07:01:41. 000 z \" and \ "channel \" : \ "card \", \ "currency \" : \ "NGN \", \ "ip_address \" : \ "52.49.173.169 \", \ "metadata \" : \ "\", \ "the log \" : null, \ "feeds \" : 400, \ "fees_split \", null, \" authorization\":{\"authorization_code\":\"AUTH_jify7ej4xz\",\"bin\":\"506105\",\"last4\":\"9270\",\"exp_month\":\"06\",\ "exp_year\":\"2022\",\"channel\":\"card\",\"card_type\":\"verve DEBIT\",\"bank\":\"First Bank of Nigeria\",\"country_code\":\"NG\",\"brand\":\"verve\",\"reusable\":true,\"signature\":\"SIG_KIrnvMgIz4nevoGpB22A\",\"acc ount_name\":\"OWANIYI LAWRENCE GBENGA\"},\"customer\":{\"id\":29527446,\"first_name\":null,\"last_name\":null,\"email\":\"200908163344puid22741529@mspo rt.com\",\"customer_code\":\"CUS_ka3zv3h6j99d2x7\",\"phone\":null,\"metadata\":null,\"risk_action\":\"default\",\"intern ational_format_phone\":null},\"plan\":{},\"subaccount\":{},\"split\":{},\"order_id\":null,\"paidAt\":\"2021-06-21T07:01: 47.000 Z \ ", \ "requested_amount \ pos_transaction_data" : 50000, \ "\", null, \ "source \" : {\ "type \" : \ "API \", \ "source \" : \ "merchant_ap i\",\"identifier\":null}},\"order\":null,\"business_name\":\"MSPORT\"}";
        String result = HashAlgorithmUtils.hmacHashEncryption(data, key, HMACEncryptType.HMACSHA512);
        System.out.println(result);

        //test hash
        String str = "abc123"; String md5 = HashAlgorithmUtils.hashEncryption(str, SHAEncryptType.MD5); String sha1 = HashAlgorithmUtils.hashEncryption(str, SHAEncryptType.SHA1); String sha256 = HashAlgorithmUtils.hashEncryption(str, SHAEncryptType.SHA256); String sha512 = HashAlgorithmUtils.hashEncryption(str, SHAEncryptType.SHA512); System.out.println(md5); System.out.println(sha1); System.out.println(sha256); System.out.println(sha512); }}Copy the code