# 加密代码DEMO

以JAVA语言为例，Sha1加密算法举例如下。以下代码，可以在配置了java开发环境(jdk>=1.8)的机器上使用如下命令测试:

* javac Sha1Demo.java&#x20;
* java Sha1Demo

执行以上命令后，我们得到输出:   0354980F3F1162CFD25C4BD3DE69E6D686D60722

Sha1Demo.java 代码如下所示：

```

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) {
        // 接车单json示例
        Map<String,String> map = new HashMap<>();
        // 消息id
        map.put("msgId","1887f9fa2c23455b9501902ff3e89142");
        // 签名hash
        map.put("msgSignature","0354980F3F1162CFD25C4BD3DE69E6D686D60722");
        // 消息类型
        map.put("msgType","event.autox3.order.receivebase.create");
        // 员工信息
        map.put("staffInfo","{\"areaCode\":\"+44\",\"brandId\":134,\"mobile\":\"13601190376\",\"staffId\":400,\"staffName\":\"园 梨\",\"storeId\":91}");
        // 推送时间
        map.put("createTime","1622452772343");
        // 消息内容
        map.put("content","{\"carInfo\":{\"carBrandName\":\"阿尔法-罗密欧\",\"carNum\":\"京M17832\",\"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));
    }
}
```









