public void ToData(byte[] _data) { _data[0] = tag1; _data[1] = tag2; var d = BitConverter.GetBytes(msgid); _data[2] = d[0]; _data[3] = d[1]; for (var i = 4; i < 62; i++) { var seek = i - 4; if (seek < this.data.Length) { _data[i] = this.data[seek]; } else { _data[i] = 0; } } var crc = SignTool.ComputeCRC32(_data, 0, 62); _data[62] = crc[0]; _data[63] = crc[1]; }
public void SendDataBlock(DataBlock data) { Message msg = new Message(); msg.tag1 = 0x01; //0字节 msg.tag2 = 0x01; //1字节 msg.msgid = SignTool.RandomShort(); // 2 3 字节 //4~7 字节 var byteLength = BitConverter.GetBytes((UInt32)data.data.Length); for (var i = 0; i < 4; i++) { msg.data[i] = byteLength[i]; } var sha = SignTool.HexString2Bytes(data.sha256); //8 到39 字节 for (var i = 0; i < 32; i++) { msg.data[i + 4] = sha[i]; } SendMessage(msg, true); }
public void Load() { if (System.IO.File.Exists("address.sim.save.txt")) { try { var lines = System.IO.File.ReadAllText("address.sim.save.txt").Split('\n'); foreach (var line in lines) { var lp = line.Split(new string[] { "||" }, StringSplitOptions.None); NeoDun.Address a = new Address(); a.type = (AddressType)Enum.Parse(typeof(AddressType), lp[0]); a.AddressText = lp[1]; a.privatekey = SignTool.DecodeBase58(lp[2]); this.addresses.Add(a); } Console.WriteLine("addresses isEmpty" + this.addresses.IsEmpty); } catch { } } else { Console.WriteLine("address.sim.save.txt is null"); } }
//发送包 public void SendPackage(byte[] data) { byte[] sha = SignTool.ComputeSHA256(data, 0, data.Length); string key = SignTool.Hex2String(sha, 0, sha.Length); datapool[key] = data; Message msg = new Message(); msg.tag1 = 0x01; //0字节 msg.tag2 = 0x01; //1字节 msg.msgid = SignTool.RandomShort(); // 2 3 字节 //4~7 字节 var byteLength = BitConverter.GetBytes((UInt32)data.Length); for (var i = 0; i < 4; i++) { msg.data[i] = byteLength[i]; } //8 到39 字节 for (var i = 0; i < 32; i++) { msg.data[i + 4] = sha[i]; } SendMessage(msg, true); }
public byte[] GetAddbytes() { if (type == AddressType.Neo) { return(SignTool.DecodeBase58(this.AddressText)); } return(null); }
public void writeHash256(int pos, string hash) { var b = SignTool.HexString2Bytes(hash); for (var i = 0; i < b.Length; i++) { data[pos + i] = b[i]; } }
public bool Check() { byte[] hashdata = SignTool.HexString2Bytes(sha256); var hash = SignTool.ComputeSHA256(data, 0, data.Length); var hashstr = SignTool.Bytes2HexString(hash, 0, hash.Length); match = (hashstr.ToUpper() == sha256.ToUpper()); return(match); }
public void FromData(byte[] _data) { this.tag1 = _data[0]; this.tag2 = _data[1]; this.msgid = BitConverter.ToUInt16(_data, 2); for (var i = 4; i < 62; i++) { this.data[i - 4] = _data[i]; } this.crc1 = _data[62]; this.crc2 = _data[63]; var crc = SignTool.ComputeCRC32(_data, 0, 62); if (this.crc1 != crc[0] || this.crc2 != crc[1]) { throw new Error_MsgHashNotMatch("the message hash not match"); } }
void OnMsg(Message msg) { Message srcmsg = null; if (msg.tag1 == 0x00 || msg.tag2 >= 0xa0)//如果是一条回复消息,找原始消息 { if (this.needBackMessage.ContainsKey(msg.msgid)) { srcmsg = this.needBackMessage[msg.msgid]; } else { return; } } System.Threading.ThreadPool.QueueUserWorkItem(async(_state) => { #region 0x01 if (msg.tag1 == 0x01 && msg.tag2 == 0x01) { //recv a file var len = msg.readUInt32(0); var hash = msg.readHash256(4); var block = dataTable.newOrGet(hash, len, NeoDun.DataBlockFrom.FromSigner); var piececount = len / 50; if (len % 50 > 0) { piececount++; } await System.Threading.Tasks.Task.Delay(50);//add bu hkh 1216 System.Threading.ThreadPool.QueueUserWorkItem((__state) => { NeoDun.Message _msg = new NeoDun.Message(); _msg.tag1 = 0x01; _msg.tag2 = 0x10; _msg.msgid = NeoDun.SignTool.RandomShort(); _msg.writeUInt16(0, 0); _msg.writeUInt16(2, (UInt16)(piececount - 1)); _msg.writeHash256(4, hash); SendMessage(_msg, true); }); } if (msg.tag1 == 0x01 && msg.tag2 == 0x10)//请求数据 { int blockbegin = BitConverter.ToUInt16(msg.data, 0); int blockend = BitConverter.ToUInt16(msg.data, 2); string hash = SignTool.Bytes2HexString(msg.data, 4, 32); var block = dataTable.getBlockBySha256(hash); if (block == null) { //发送错误回应,下一步再说 } else { for (var i = blockbegin; i < blockend + 1; i++) { Message msg1 = new Message(); block.FillPieceMessage(msg1, msg.msgid, (UInt16)i); SendMessage(msg1, false); } //Send0103 Message msg2 = new Message(); msg2.tag1 = 0x01; msg2.tag2 = 0xa3; msg2.msgid = msg.msgid;//这是一条回复消息 SendMessage(msg2, false); } } if (msg.tag1 == 0x01 && msg.tag2 == 0x11)//给数据分配编号 { var remoteid = msg.readUInt32(0); var hash = msg.readHash256(4); var block = dataTable.getBlockBySha256(hash); block.dataidRemote = (uint)remoteid; } if (msg.tag1 == 0x01 && msg.tag2 == 0x12) { var remoteid = msg.readUInt32(0); var hash = msg.readHash256(4); var block = dataTable.getBlockBySha256(hash); block.dataidRemote = (uint)remoteid; } if (msg.tag1 == 0x01 && msg.tag2 == 0xa2)//收到一个分片 { var hash = srcmsg.readHash256(4); var data = dataTable.getBlockBySha256(hash); data.FromPieceMsg(msg); } if (msg.tag1 == 0x01 && msg.tag2 == 0xa3)//接收完毕 { var hash = srcmsg.readHash256(4); var data = dataTable.getBlockBySha256(hash); bool bcheck = data.Check(); if (bcheck) {//数据接收完整 System.Threading.ThreadPool.QueueUserWorkItem((__state) => { NeoDun.Message _msg = new NeoDun.Message(); _msg.tag1 = 0x01; _msg.tag2 = 0x11; _msg.msgid = NeoDun.SignTool.RandomShort(); _msg.writeUInt32(0, data.dataid); _msg.writeHash256(4, hash); SendMessage(_msg, false); }); } else {//数据接收完毕,但是hash256 不匹配 System.Threading.ThreadPool.QueueUserWorkItem((__state) => { NeoDun.Message _msg = new NeoDun.Message(); _msg.tag1 = 0x01; _msg.tag2 = 0x12; _msg.msgid = NeoDun.SignTool.RandomShort(); _msg.writeUInt32(0, data.dataid); _msg.writeHash256(4, hash); SendMessage(_msg, false); }); } } #endregion //设置地址名称失败 if (msg.tag1 == 0x02 && msg.tag2 == 0xe2) { EnumErrorCode errorCode = (EnumErrorCode)msg.readUInt16(0); eventHandler(EnumControl.SetName, EnumMsgCode.SetNameFailed, errorCode); } //删除地址失败 if (msg.tag1 == 0x02 && msg.tag2 == 0xe3) { EnumErrorCode errorCode = (EnumErrorCode)msg.readUInt16(0); eventHandler(EnumControl.DelAddress, EnumMsgCode.DeleteNameFailed, errorCode); } //增加地址失败 if (msg.tag1 == 0x02 && msg.tag2 == 0xe4) { EnumErrorCode errorCode = (EnumErrorCode)msg.readUInt16(0); eventHandler(EnumControl.AddAddress, EnumMsgCode.AddAddressFailed, errorCode); } //签名失败 if (msg.tag1 == 0x02 && msg.tag2 == 0xe5) { EnumErrorCode errorCode = (EnumErrorCode)msg.readUInt16(0); eventHandler(EnumControl.SignData, EnumMsgCode.SignFailed, errorCode); } //安装失败 if (msg.tag1 == 0x03 && msg.tag2 == 0xe1) { EnumErrorCode errorCode = (EnumErrorCode)msg.readUInt16(0); eventHandler(EnumControl.Common, EnumMsgCode.InstallFailed, errorCode); } //拒绝更新固件 if (msg.tag1 == 0x03 && msg.tag2 == 0xe2) { eventHandler(EnumControl.ApplyInstallFramework, EnumMsgCode.RefuseUpdate, EnumErrorCode.NoError); } //卸载失败 if (msg.tag1 == 0x03 && msg.tag2 == 0xe3) { EnumErrorCode errorCode = (EnumErrorCode)msg.readUInt16(0); eventHandler(EnumControl.UninstallPlugin, EnumMsgCode.UninstallFailed, errorCode); } //收到地址,//加进地址池子里 if (msg.tag1 == 0x02 && msg.tag2 == 0xa0) { var epoch = (DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000000; var pos = msg.readUInt16(0); var count = msg.readUInt16(2); var type = (NeoDun.AddressType)msg.readUInt16(4); var add = new NeoDun.Address(); add.type = type; add.AddressText = SignTool.EncodeBase58(msg.data, 6, 25); add.name = System.Text.Encoding.UTF8.GetString(msg.data, 32, 6).Replace("\0", ""); addressPool.addresses.Add(add); } //地址接受完毕 if (msg.tag1 == 0x02 && msg.tag2 == 0xa1) { eventHandler(EnumControl.GetAddress, EnumMsgCode.GetAddressSuc, EnumErrorCode.NoError); } //设置地址名称成功 if (msg.tag1 == 0x02 && msg.tag2 == 0xa2) { eventHandler(EnumControl.SetName, EnumMsgCode.SetNameSuc, EnumErrorCode.NoError); } //删除地址成功 if (msg.tag1 == 0x02 && msg.tag2 == 0xa3) { eventHandler(EnumControl.DelAddress, EnumMsgCode.DeleteNameSuc, EnumErrorCode.NoError); } //增加地址成功 if (msg.tag1 == 0x02 && msg.tag2 == 0xa4) { eventHandler(EnumControl.AddAddress, EnumMsgCode.AddAddressSuc, EnumErrorCode.NoError); } //签名成功 if (msg.tag1 == 0x02 && msg.tag2 == 0xa5) { byte[] outdata = null; string outdatahash = null; outdatahash = msg.readHash256(4); //轮询直到reciveid的数据被收到 while (true) { await Task.Delay(5); var __block = dataTable.getBlockBySha256(outdatahash); if (__block != null && __block.Check()) { outdata = __block.data; break; } } eventHandler(EnumControl.SignData, EnumMsgCode.SignSuc, EnumErrorCode.NoError, outdata); } //安装成功 if (msg.tag1 == 0x03 && msg.tag2 == 0xa1) { eventHandler(EnumControl.Common, EnumMsgCode.InstallSuc, EnumErrorCode.NoError); } //同意更新固件 if (msg.tag1 == 0x03 && msg.tag2 == 0xa2) { eventHandler(EnumControl.ApplyInstallFramework, EnumMsgCode.AgreeUpdate, EnumErrorCode.NoError); } //卸载成功 if (msg.tag1 == 0x03 && msg.tag2 == 0xa3) { eventHandler(EnumControl.UninstallPlugin, EnumMsgCode.UninstallSuc, EnumErrorCode.NoError); } //查询固件插件版本回复 if (msg.tag1 == 0x03 && msg.tag2 == 0xa4) { byte[] outdata = null; outdata = msg.data; eventHandler(EnumControl.GetPackage, EnumMsgCode.GetMessageSuc, EnumErrorCode.NoError, outdata); } //下位机请求更新固件 if (msg.tag1 == 0x03 && msg.tag2 == 0x11) { deleInstallFramework(); } //安全通道回复 if (msg.tag1 == 0x04 && msg.tag2 == 0xa1) { byte[] outdata = null; string outdatahash = null; outdatahash = msg.readHash256(4); //轮询直到reciveid的数据被收到 while (true) { await Task.Delay(5); var __block = dataTable.getBlockBySha256(outdatahash); if (__block != null && __block.Check()) { outdata = __block.data; break; } } eventHandler(EnumControl.SecurityChannel, EnumMsgCode.BuildSecurityChannelSuc, EnumErrorCode.NoError, outdata); } }); watcherColl.OnRecv(msg, srcmsg); //if (userHandleRecv != null) //{ // userHandleRecv(msg, srcmsg); //} }
public string readHash256(int pos, int len = 32) { return(SignTool.Bytes2HexString(data, pos, len)); }
void OnMsg(Message msg) { Message srcmsg = null; if (msg.tag1 == 0x00 || msg.tag2 >= 0xa0)//如果是一条回复消息,找原始消息 { srcmsg = this.needBackMessage[msg.msgid]; } System.Threading.ThreadPool.QueueUserWorkItem((_state) => { if (msg.tag1 == 0x01 && msg.tag2 == 0x10)//请求数据 { int blockbegin = BitConverter.ToUInt16(msg.data, 0); int blockend = BitConverter.ToUInt16(msg.data, 2); string hash = SignTool.Hex2String(msg.data, 4, 32); if (datapool.ContainsKey(hash) == false) { //发送错误回应,下一步再说 } else { for (var i = blockbegin; i < blockend; i++) { int begin = i * 50; int end = (i + 1) * 50; //Send0102 Message msg1 = new Message(); msg1.tag1 = 0x01; msg1.tag2 = 0x02; msg1.msgid = msg.msgid;//这是一条回复消息 var bytepiece = BitConverter.GetBytes((ushort)i); msg1.data[0] = bytepiece[0]; msg1.data[1] = bytepiece[1]; var srcdata = datapool[hash]; for (var s = begin; s < end; s++) { if (s < srcdata.Length) { msg1.data[s - begin + 2] = srcdata[s]; } else { msg1.data[s - begin + 2] = 0; } } SendMessage(msg1, false); } //Send0103 Message msg2 = new Message(); msg2.tag1 = 0x01; msg2.tag2 = 0x03; msg2.msgid = msg.msgid;//这是一条回复消息 SendMessage(msg2, false); } } if (userHandleRecv != null) { userHandleRecv(msg, srcmsg); } }); }