Exemplo n.º 1
0
        // ファイルに書き込み (ハッシュ付ける)
        public void WriteToFileWithHash(string filename)
        {
            byte[] data = this.ByteData;
            byte[] hash = Secure.HashSHA1(data);

            Buf b = new Buf();

            b.Write(hash);
            b.Write(data);
            b.WriteToFile(filename);
        }
Exemplo n.º 2
0
        public static string GeneratePacket(Pack pack, Cert cert, Rsa key)
        {
            Buf b = new Buf();

            byte[] pack_data = pack.WriteToBuf().ByteData;
            WpcEntry.AddDataEntry(b, "PACK", pack_data);

            byte[] hash = Secure.HashSHA1(pack_data);
            WpcEntry.AddDataEntry(b, "HASH", hash);

            if (cert != null && key != null)
            {
                WpcEntry.AddDataEntry(b, "CERT", cert.ByteData);
                WpcEntry.AddDataEntry(b, "SIGN", key.SignData(hash));
            }

            return(Str.AsciiEncoding.GetString(b.ByteData));
        }
Exemplo n.º 3
0
        // ファイルから読み込み (ハッシュ調べる)
        public static Buf ReadFromFileWithHash(string filename)
        {
            byte[] filedata = IO.ReadFile(filename);
            if (filedata.Length < 20)
            {
                throw new ApplicationException("filedata.Length < 20");
            }
            byte[] hash  = Util.CopyByte(filedata, 0, 20);
            byte[] data  = Util.CopyByte(filedata, 20);
            byte[] hash2 = Secure.HashSHA1(data);

            if (Util.CompareByte(hash, hash2) == false)
            {
                throw new ApplicationException("hash mismatch");
            }

            return(new Buf(data));
        }
Exemplo n.º 4
0
        public static FullRouteIPInfoCache LoadFromBuf(Buf b)
        {
            b.Seek(b.Size - 20, SeekOrigin.Begin);
            byte[] hash = b.Read(20);
            b.SeekToBegin();
            byte[] hash2 = Secure.HashSHA1(Util.CopyByte(b.ByteData, 0, (int)b.Size - 20));

            if (Util.CompareByte(hash, hash2) == false)
            {
                throw new ApplicationException("Invalid Hash");
            }

            FullRouteIPInfoCache ret = new FullRouteIPInfoCache();

            ret.TimeStamp = new DateTime((long)b.ReadInt64());
            int num = (int)b.ReadInt();

            int i;

            for (i = 0; i < num; i++)
            {
                FullRouteIPInfoEntry e = new FullRouteIPInfoEntry();
                e.From        = b.ReadInt();
                e.To          = b.ReadInt();
                e.Registry    = b.ReadStr();
                e.Assigned    = b.ReadInt();
                e.Country2    = b.ReadStr();
                e.Country3    = b.ReadStr();
                e.CountryFull = DeleteSemi(b.ReadStr());
                ret.EntryList.Add(e);
            }

            ret.EntryList.Sort();

            ret.build_country_code_to_name_db();

            return(ret);
        }
Exemplo n.º 5
0
        public Buf SaveToBuf()
        {
            Buf b = new Buf();

            b.WriteInt64((ulong)this.TimeStamp.Ticks);
            b.WriteInt((uint)this.EntryList.Count);

            foreach (FullRouteIPInfoEntry e in this.EntryList)
            {
                b.WriteInt(e.From);
                b.WriteInt(e.To);
                b.WriteStr(e.Registry);
                b.WriteInt(e.Assigned);
                b.WriteStr(e.Country2);
                b.WriteStr(e.Country3);
                b.WriteStr(e.CountryFull);
            }

            b.Write(Secure.HashSHA1(b.ByteData));

            b.SeekToBegin();

            return(b);
        }
Exemplo n.º 6
0
 public bool VerifyData(byte[] data, byte[] sign)
 {
     byte[] hash = Secure.HashSHA1(data);
     return(VerifyHash(hash, sign));
 }
Exemplo n.º 7
0
 public byte[] SignData(byte[] data)
 {
     byte[] hash = Secure.HashSHA1(data);
     return(SignHash(hash));
 }
Exemplo n.º 8
0
        public static WpcPacket ParsePacket(string recvStr)
        {
            List <WpcEntry> o = WpcEntry.ParseDataEntry(recvStr);

            WpcEntry e;

            try
            {
                e = WpcEntry.FindEntry(o, "PACK");
                if (e != null)
                {
                    byte[] hash = Secure.HashSHA1(e.Data);
                    Pack   pack = null;

                    pack = Pack.CreateFromBuf(new Buf(e.Data));

                    e = WpcEntry.FindEntry(o, "HASH");

                    if (e != null)
                    {
                        byte[] hash2 = e.Data;

                        if (Util.CompareByte(hash, hash2))
                        {
                            e = WpcEntry.FindEntry(o, "CERT");
                            if (e != null)
                            {
                                Cert cert;

                                try
                                {
                                    cert = new Cert(e.Data);
                                }
                                catch
                                {
                                    return(null);
                                }

                                e = WpcEntry.FindEntry(o, "SIGN");
                                if (e != null)
                                {
                                    byte[] sign = e.Data;

                                    if (cert.RsaPublicKey.VerifyData(hash2, sign))
                                    {
                                        return(new WpcPacket(pack, hash2, cert, sign));
                                    }
                                }
                            }
                            else
                            {
                                return(new WpcPacket(pack, hash2));
                            }
                        }
                    }
                }
            }
            catch (OverflowException)
            {
                return(null);
            }

            return(null);
        }