跳转至

自定义算法


概述

自定义几个算法,crc8、bcd、hmac_sha1、hmac_md5等,还有HJ212和MODBUS协议的组包,这两个协议也可以直接用库的函数。


代码


//CRC8算法
function crc8(data)
{
    var res = 0;
    for(var i = 0; i < data.length; i++)
    {
        res = (res ^ data[i]) & 0xFF;
        for(var j = 0; j < 8; j++)
        {
            if(res & 0x80) res = (res << 1) ^ 0xE5;
            else res <<= 1;
        }
    }
    return res & 0xFF;
}
var test = Bytes.create("B32725106500C05523000000002011000147171800", "hex");
print(Coder.num(crc8(test), "%02X"));


//一个仪表的BCD算法
function bcd(n)
{
    var d = Bytes.create(5);
    d.seti(((n * 100) % 10) << 4 | ((n * 1000) % 10), 0);
    d.seti((n % 10) << 4 | ((n * 100) % 10), 1);
    d.seti(((n / 100) % 10) << 4 | ((n / 10) % 10), 2);
    d.seti(((n / 10000) % 10) << 4 | ((n / 1000) % 10), 3);
    d.seti((n > 0 ? 0 : 0xF) << 4 | ((n / 100000) % 10), 4);
    return d;
}
print(bcd(49.098).hex());

//阿里云平台用到的密码算法
function hmac_sha1(key, data)
{
    if(key.length > 64) key = Coder.sha1(key);
    else key = Bytes.create(key);

    var hkey = Bytes.create(64);
    hkey.copy(key);

  var ipad = Bytes.create(64 + data.length);
  var opad = Bytes.create(64 + 20);
  for(var i = 0; i < 64; i++)
  {
    ipad[i] = hkey[i] ^ 0x36;
    opad[i] = hkey[i] ^ 0x5C;
  }
  ipad.sets(data, 64);
  var hash = Coder.sha1(ipad);
  opad.copy(hash, 64);
  return Coder.sha1(opad, 64);
}
var productKey = "g61qSAcgCqv";
var deviceName = "XY-MD02";
var deviceSecret = "56d882cdd2f802a8a2475e70041e465e";
var clientname = "1234";
var content = "clientId" + clientname + "deviceName" + deviceName + "productKey" + productKey;
var password = hmac_sha1(deviceSecret, content).hex(true);
print(password);

function hmac_md5(key, data)
{
    if(key.length > 64) key = Coder.md5(key);
    else key = Bytes.create(key);

    var hkey = Bytes.create(64);
    hkey.copy(key);

  var ipad = Bytes.create(64 + data.length);
  var opad = Bytes.create(64 + 16);
  for(var i = 0; i < 64; i++)
  {
    ipad[i] = hkey[i] ^ 0x36;
    opad[i] = hkey[i] ^ 0x5C;
  }
  ipad.sets(data, 64);
  var hash = Coder.md5(ipad);
  opad.copy(hash, 64);
  return Coder.md5(opad);
}
content = "clientId" + clientname + "deviceName" + deviceName + "productKey" + productKey;
password = hmac_md5(deviceSecret, content).hex(true);
print(password);

//HJ212的组包
var data = "DataTime=20200520091500;w01001-Rtd=7.000,w01001-Flag=D;w01102-Rtd=0.21,w01102-Flag=D;w01101-Rtd=1.330,w01101-Flag=D";
var key = "1234567890123456";
var data_enc = Coder.sm4e(data, key);
var data_field = "QN=20200520091501761;ST=32;CN=2011;PW=123456;MN=660000001000001;Flag=5;CP=&&" + data_enc.hex(true) + "&&";
//var data_field = "QN=20200520091501761;ST=32;CN=2011;PW=123456;MN=660000001000001;Flag=5;CP=&&" + data + "&&";
var len_str = "" + data_field.length;
while(len_str.length < 4) len_str = "0" + len_str;
var crc_str = Coder.hex(Coder.crc16(data_field, "ansi"), "2B");
var send_str = "##" + len_str + data_field + crc_str + "\r\n";
print(send_str);
print(Packet.HJ212.packet(data_field)); //库函数版本

//MODBUS的组包
function readModbus(addr, cmd, reg, count)
{
    var d = Bytes.create(8);
    d.seti(addr, 0);
    d.seti(cmd, 1);
    d.seti(reg, 2, "2b");
    d.seti(count, 4, "2b");
    d.seti(Coder.crc16(d.sub(0, 6), "modbus"), 6, "2l");
    return d;
}
print(readModbus(1, 3, 4, 2).hex());
print(Packet.Modbus.packet(1, 3, 4, 2).hex());  //库函数版本


下载代码(右键另存为):custom.js