Example #1
0
        public static byte[] GenerateSHA1Hash(byte[] namespaceId, byte[] data)
        {
            IDigest digest = new Sha1Digest();

            digest.BlockUpdate(namespaceId, 0, namespaceId.Length);
            digest.BlockUpdate(data, 0, data.Length);
            var output = new byte[128];

            digest.DoFinal(output, 0);
            return(output);
        }
Example #2
0
        protected override void PostConnect()
        {
            var sha1 = new Sha1Digest();

            sha1.BlockUpdate(EncryptionKey, 0, EncryptionKey.Length);
            sha1.BlockUpdate(BitConverter.GetBytes(Timestamp), 0, 8);
            var hash = new byte[sha1.GetDigestSize()];

            sha1.DoFinal(hash, 0);
            var req = new GC2WS_ASK_WORLD_ENTER(AccountID, hash);

            SendPacket(req);
            SetEncryptionKey(EncryptionKey);
        }
Example #3
0
        private void btnFirmaMasiva_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fbd     = new FolderBrowserDialog();
            DialogResult        dresult = fbd.ShowDialog();

            if (dresult == DialogResult.OK)
            {
                foreach (string file in Directory.GetFiles(fbd.SelectedPath))
                {
                    FileInfo fInfo             = new FileInfo(file);
                    AsymmetricKeyParameter akp = ReadPrivateKey(lblFirma.Text);
                    Sha1Digest             dig = new Sha1Digest();
                    byte[] msgBytes            = File.ReadAllBytes(file);
                    dig.BlockUpdate(msgBytes, 0, msgBytes.Length);
                    byte[] result = new byte[dig.GetDigestSize()];
                    dig.DoFinal(result, 0);
                    Org.BouncyCastle.Crypto.Signers.RsaDigestSigner signer = new Org.BouncyCastle.Crypto.Signers.RsaDigestSigner(dig);
                    signer.Init(true, akp);
                    signer.BlockUpdate(result, 0, result.Length);
                    byte[] sig        = signer.GenerateSignature();
                    string signString = Convert.ToBase64String(sig);
                    string signPath   = string.Format("{0}{1}", fInfo.FullName, "sgn");
                    File.WriteAllText(signPath, signString, System.Text.Encoding.UTF8);
                }

                MessageBox.Show("Firma finalizada.");
            }
        }
 private static byte[] GetDigestInByteArray(
     FileInfo file,
     CancellationToken cancellationToken)
 {
     using (var readStream = file.OpenRead())
     {
         var digest = new Sha1Digest();
         var output = new byte[digest.GetDigestSize()];
         var buffer = new byte[BufferSizeInByte];
         int read;
         while ((read = readStream.Read(
                     buffer,
                     0,
                     buffer.Length)) > 0)
         {
             cancellationToken.ThrowIfCancellationRequested();
             digest.BlockUpdate(
                 buffer,
                 0,
                 read
                 );
         }
         digest.DoFinal(
             output,
             0
             );
         return(output);
     }
 }
Example #5
0
        private static string FromStream_BOUNCY(Stream stream)
        {
            int BUFFER_SIZE = 5 * 1024 * 1024;

            byte[] buffer = new byte[BUFFER_SIZE];

            int        total_read = 0;
            int        num_read   = 0;
            Sha1Digest sha1       = new Sha1Digest();

            while (0 < (num_read = stream.Read(buffer, 0, BUFFER_SIZE)))
            {
                total_read += num_read;
                sha1.BlockUpdate(buffer, 0, num_read);
            }

            byte[] hash = new byte[sha1.GetDigestSize()];
            sha1.DoFinal(hash, 0);

            // Convert to string
            StringBuilder buff = new StringBuilder();

            foreach (byte hash_byte in hash)
            {
                buff.Append(String.Format("{0:X1}", hash_byte));
            }

            return(buff.ToString());
        }
Example #6
0
 private static async Task <byte[]> GetDigestInByteArrayAsync(
     FileInfo file,
     CancellationToken cancellationToken)
 {
     using (var readStream = file.OpenRead())
     {
         var digest = new Sha1Digest();
         var output = new byte[digest.GetDigestSize()];
         var buffer = new byte[BufferSizeInByte];
         int read;
         while ((read = await readStream.ReadAsync(
                     buffer,
                     0,
                     buffer.Length,
                     cancellationToken).ConfigureAwait(false)) > 0)
         {
             cancellationToken.ThrowIfCancellationRequested();
             digest.BlockUpdate(
                 buffer,
                 0,
                 read
                 );
         }
         digest.DoFinal(
             output,
             0
             );
         return(output);
     }
 }
Example #7
0
        private string ExecuteHash()
        {
            IDigest hash;

            switch (this.HashFunctionComboBox.Text)
            {
            case "Blake2b - 256 bit":
                hash = new Blake2bDigest(256);
                break;

            case "Blake2b - 512 bit":
                hash = new Blake2bDigest(512);
                break;

            case "SHA-1":
                hash = new Sha1Digest();
                break;

            case "SHA-2 256 bit":
                hash = new Sha256Digest();
                break;

            case "SHA-2 512 bit":
                hash = new Sha512Digest();
                break;

            case "SHA-3 256 bit":
                hash = new Sha3Digest(256);
                break;

            case "SHA-3 512 bit":
                hash = new Sha3Digest(512);
                break;

            default:
                hash = new Sha1Digest();
                break;
            }

            byte[] result = new byte[hash.GetDigestSize()];

            using (Stream source = File.OpenRead(this.LocationTextBox.Text))
            {
                int BytesRead;
                while ((BytesRead = source.Read(this._Data, 0, this._Data.Length)) > 0)
                {
                    hash.BlockUpdate(this._Data, 0, BytesRead);
                }
            }

            hash.DoFinal(result, 0);

            Array.Clear(this._Data, 0, this._Data.Length);

            return(Entities.HashFunctionList.ByteArrayToHexString(result));
        }
        private static byte[] GetDigest(SubjectPublicKeyInfo spki)
        {
            IDigest digest = new Sha1Digest();

            byte[] array = new byte[digest.GetDigestSize()];
            byte[] bytes = spki.PublicKeyData.GetBytes();
            digest.BlockUpdate(bytes, 0, bytes.Length);
            digest.DoFinal(array, 0);
            return(array);
        }
Example #9
0
        public static void Send(WorldClient cclient)
        {
            PacketOut Out = new PacketOut((uint)Opcodes.ANS_DISTRICT_ENTER);

            if (cclient.Reserved != null)
            {
                Out.WriteInt32((int)ResponseCodes.RC_SUCCESS);
                string[] result = cclient.Reserved.IP.Split('.');
                foreach (string s in result)
                {
                    Out.WriteByte(Convert.ToByte(s));
                }
                Out.WriteUInt16Reverse(cclient.Reserved.Port);
                ulong timestamp = (ulong)TCPManager.GetTimeStamp();
                Out.WriteUInt64Reverse(timestamp);

                var timestampBytes = BitConverter.GetBytes(timestamp);
                var sha1           = new Sha1Digest();
                sha1.BlockUpdate(cclient.Crypto.Key, 0, cclient.Crypto.Key.Length);
                sha1.BlockUpdate(timestampBytes, 0, timestampBytes.Length);
                var handshakeHash = new byte[sha1.GetDigestSize()];
                sha1.DoFinal(handshakeHash, 0);

                byte[] hash = new byte[handshakeHash.Length];
                Buffer.BlockCopy(handshakeHash, 0, hash, 0, handshakeHash.Length);

                sha1 = new Sha1Digest();
                sha1.BlockUpdate(cclient.Crypto.Key, 0, cclient.Crypto.Key.Length);
                sha1.BlockUpdate(handshakeHash, 0, handshakeHash.Length);
                var encryptionHash = new byte[sha1.GetDigestSize()];
                sha1.DoFinal(encryptionHash, 0);
                var encryptionKey = new byte[16];
                Buffer.BlockCopy(encryptionHash, 0, encryptionKey, 0, 16);

                cclient.Reserved.tcp.Client.Send(new byte[] { 0x31, Convert.ToByte(cclient.Account.Index) });
                cclient.Reserved.tcp.Client.Send(encryptionKey);
            }
            else
            {
                Out.WriteUInt32Reverse((uint)ResponseCodes.RC_DISTRICT_RESERVE_DISTRICT_OFFLINE);
            }
            cclient.Send(Out);
        }
Example #10
0
        static Asn1OctetString CreateDigestFromBytes(byte[] bytes)
        {
            var digest = new Sha1Digest();

            digest.BlockUpdate(bytes, 0, bytes.Length);
            var digestBytes = new byte[digest.GetDigestSize()];

            digest.DoFinal(digestBytes, 0);
            return(new DerOctetString(digestBytes));
        }
Example #11
0
        /// <summary>
        /// Compute the Hash of <paramref name="data"/> using the SHA-1 algorithm.
        /// </summary>
        /// <remarks>The approved algorithm for hashing is SHA-1 as specified in ISO/IEC 10118-3 [5].</remarks>
        /// <param name="data">Data to hash.</param>
        /// <returns>Hash value.</returns>
        public static byte[] ComputeHashSha1(this byte[] data)
        {
            IDigest sha1 = new Sha1Digest();
            var     h    = new byte[sha1.GetDigestSize()];

            sha1.BlockUpdate(data, 0, data.Length);
            sha1.DoFinal(h, 0);

            return(h);
        }
Example #12
0
        /// <summary>
        /// sha1算签名, 代付回调验签
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        public static string ComputeSha1(string text)
        {
            Sha1Digest sha1Digest = new Sha1Digest();
            var        retValue   = new byte[sha1Digest.GetDigestSize()];
            var        bs         = Encoding.UTF8.GetBytes(text);

            sha1Digest.BlockUpdate(bs, 0, bs.Length);
            sha1Digest.DoFinal(retValue, 0);
            return(BitConverter.ToString(retValue).Replace("-", ""));
        }
Example #13
0
        public byte[] ComputeSHA1(byte[] data, int offset, int count)
        {
            var digest = new Sha1Digest();

            digest.BlockUpdate(data, offset, count);

            var output = new byte[20];

            digest.DoFinal(output, 0);
            return(output);
        }
Example #14
0
        public override string hash(string pw)
        {
            Sha1Digest digest = new Sha1Digest();

            byte[] password = Encoding.UTF8.GetBytes(pw);
            digest.BlockUpdate(password, 0, password.Length);
            int digestSize = digest.GetDigestSize();

            byte[] hashData = new byte[digestSize];
            digest.DoFinal(hashData, 0);

            return("{SHA}" + Convert.ToBase64String(hashData));
        }