Code Encryption DEMO

Take JAVA language as an example, the Sha1 encryption algorithm is as follows. The code below can be tested using the following command on a machine configured with a java development environment (jdk>=1.8):

  • javac Sha1Demo.java

  • java Sha1Demo

After executing the above command, we get the output: 0354980F3F1162CFD25C4BD3DE69E6D686D60722

The Sha1Demo.java code is as follows:


import javax.xml.bind.DatatypeConverter;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;


public class Sha1Demo {

    public String sha1(String input) {
        String sha1 = null;
        try {
            MessageDigest msdDigest = MessageDigest.getInstance("SHA-1");
            msdDigest.update(input.getBytes("UTF-8"), 0, input.length());
            sha1 = DatatypeConverter.printHexBinary(msdDigest.digest());
        } catch (UnsupportedEncodingException | NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return sha1;
    }

    public static void main(String[] args) {
        // Service order json example
        Map<String,String> map = new HashMap<>();
        // Message ID 
        map.put("msgId","1887f9fa2c23455b9501902ff3e89142");
        // Signature hash
        map.put("msgSignature","0354980F3F1162CFD25C4BD3DE69E6D686D60722");
        // Message Type 
        map.put("msgType","event.autox3.order.receivebase.create");
        // Staff Info 
        map.put("staffInfo","{\"areaCode\":\"+44\",\"brandId\":134,\"mobile\":\"13601190376\",\"staffId\":400,\"staffName\":\"James Anderson\",\"storeId\":91}");
        // Push time 
        map.put("createTime","1622452772343");
        // Message content
        map.put("content","{\"carInfo\":{\"carBrandName\":\"Alpha Romeo\",\"carNum\":\"JS73RHK\",\"carOwnerFirstName\":\"\",\"carOwnerLastName\":\"Thomson\",\"carOwnerMobile\":\"\",\"carProductionDate\":1325347200000,\"carStyleName\":\"ALFA 156\",\"carVin\":\"\",\"salutation\":\"Mr.\"},\"preServiceInspection\":{\"mileage\":1653.0,\"personalItemList\":[]},\"serviceOrderId\":7591}");

        String appSecret = "084336629F874E793035255E12E88DAE87556274D860432BBB21A93951B9F98E";
        String text = map.get("msgId") + map.get("msgType") + map.get("staffInfo")
                + map.get("createTime") +  map.get("content") + appSecret;
        System.out.println(new Sha1Demo().sha1(text));
    }
}

Last updated

Was this helpful?