当前位置: 首页>>代码示例>>C#>>正文


C# SHA256Managed.TransformBlock方法代码示例

本文整理汇总了C#中System.Security.Cryptography.SHA256Managed.TransformBlock方法的典型用法代码示例。如果您正苦于以下问题:C# SHA256Managed.TransformBlock方法的具体用法?C# SHA256Managed.TransformBlock怎么用?C# SHA256Managed.TransformBlock使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Security.Cryptography.SHA256Managed的用法示例。


在下文中一共展示了SHA256Managed.TransformBlock方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: ComputeHash

        public static UInt256 ComputeHash(IChainState chainState)
        {
            using (var sha256 = new SHA256Managed())
            {
                // add each unspent tx to hash
                foreach (var unspentTx in chainState.ReadUnspentTransactions())
                {
                    var unspentTxBytes = DataEncoder.EncodeUnspentTx(unspentTx);
                    sha256.TransformBlock(unspentTxBytes, 0, unspentTxBytes.Length, unspentTxBytes, 0);
                }
                // finalize hash
                sha256.TransformFinalBlock(new byte[0], 0, 0);

                // hash again to return double-hashed utxo committment
                return new UInt256(SHA256Static.ComputeHash(sha256.Hash));
            }
        }
开发者ID:cole2295,项目名称:BitSharp,代码行数:17,代码来源:UtxoCommitment.cs

示例2: Calculate_Hash

        public static string Calculate_Hash(string fileName)
        {
            long read = 0;
            var r = -1;
            const long bytesToRead = 100 * 1024 * 1024;
            const int bufferSize = 4096;
            var buffer = new byte[bufferSize];
            var sha = new SHA256Managed();

            using (var stream = new FileStream(fileName, FileMode.Open, FileAccess.Read))
            {
                while (read <= bytesToRead && r != 0)
                {
                    read += (r = stream.Read(buffer, 0, bufferSize));
                    sha.TransformBlock(buffer, 0, r, null, 0);
                }
            }
            sha.TransformFinalBlock(buffer, 0, 0);
            return string.Join("", sha.Hash.Select(x => x.ToString("x2")));
        }
开发者ID:cdadmin,项目名称:clonedeploy,代码行数:20,代码来源:Image.cs

示例3: generatePacket


//.........这里部分代码省略.........

                    packetcount++;

                    packet[5] = gcssysid;
                    packet[6] = (byte)MAV_COMPONENT.MAV_COMP_ID_MISSIONPLANNER;
                    packet[7] = (byte)(messageType & 0xff);
                    packet[8] = (byte)((messageType >> 8) & 0xff);
                    packet[9] = (byte)((messageType >> 16) & 0xff);

                    i = 10;
                    foreach (byte b in data)
                    {
                        packet[i] = b;
                        i++;
                    }

                    ushort checksum = MavlinkCRC.crc_calculate(packet, packet[1] + MAVLINK_NUM_HEADER_BYTES);

                    checksum = MavlinkCRC.crc_accumulate(MAVLINK_MESSAGE_CRCS[(uint)messageType], checksum);

                    byte ck_a = (byte)(checksum & 0xFF); ///< High byte
                    byte ck_b = (byte)(checksum >> 8); ///< Low byte

                    packet[i] = ck_a;
                    i += 1;
                    packet[i] = ck_b;
                    i += 1;

                    if (MAV.signing)
                    {
                        //https://docs.google.com/document/d/1ETle6qQRcaNWAmpG2wz0oOpFKSF_bcTmYMQvtTGI8ns/edit

                        /*
                        8 bits of link ID
                        48 bits of timestamp
                        48 bits of signature
                        */

                        // signature = sha256_48(secret_key + header + payload + CRC + link-ID + timestamp)

                        var timestamp = (UInt64) ((DateTime.UtcNow - new DateTime(2015, 1, 1)).TotalMilliseconds*1000);
                        var timebytes = BitConverter.GetBytes(timestamp);

                        var sig = new byte[7]; // 13 includes the outgoing hash
                        sig[0] = 0;
                        Array.Copy(timebytes, 0, sig, 1, 6); // timestamp

                        //Console.WriteLine("gen linkid {0}, time {1} {2} {3} {4} {5} {6}", sig[0], sig[1], sig[2], sig[3], sig[4], sig[5], sig[6]);

                        using (SHA256Managed signit = new SHA256Managed())
                        {

                            signit.TransformBlock(signingKey, 0, signingKey.Length, null, 0);
                            signit.TransformBlock(packet, 0, i, null, 0);
                            signit.TransformFinalBlock(sig, 0, sig.Length);
                            var ctx = signit.Hash;
                            // trim to 48
                            Array.Resize(ref ctx, 6);

                            foreach (byte b in sig)
                            {
                                packet[i] = b;
                                i++;
                            }

                            foreach (byte b in ctx)
                            {
                                packet[i] = b;
                                i++;
                            }
                        }
                    }
                }

                if (BaseStream.IsOpen)
                {
                    BaseStream.Write(packet, 0, i);
                    _bytesSentSubj.OnNext(i);
                }

                try
                {
                    if (logfile != null && logfile.CanWrite)
                    {
                        lock (logfile)
                        {
                            byte[] datearray =
                                BitConverter.GetBytes(
                                    (UInt64) ((DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalMilliseconds*1000));
                            Array.Reverse(datearray);
                            logfile.Write(datearray, 0, datearray.Length);
                            logfile.Write(packet, 0, i);
                        }
                    }
                }
                catch
                {
                }
            }
        }
开发者ID:ChukRhodes,项目名称:MissionPlanner,代码行数:101,代码来源:MAVLinkInterface.cs

示例4: readPacket


//.........这里部分代码省略.........
                        string messagehb =
                            "Mavlink 0.9 Heartbeat, Please upgrade your AP, This planner is for Mavlink 1.0\n\n";
                        Console.WriteLine(messagehb);
                        if (logreadmode)
                            logplaybackfile.BaseStream.Seek(0, SeekOrigin.End);
                        throw new Exception(messagehb);
                    }
                    return new MAVLinkMessage();
                }
            }

            // check crc
            if ((message.crc16 >> 8) != (crc >> 8) ||
                (message.crc16 & 0xff) != (crc & 0xff))
            {
                if (message.msgid != -1 && buffer.Length > 5 && msginfo.name != null)
                    log.InfoFormat("Mavlink Bad Packet (crc fail) len {0} crc {1} vs {4} pkno {2} {3}", buffer.Length,
                        crc, message.msgid, msginfo.name.ToString(),
                        message.crc16);
                if (logreadmode)
                    log.InfoFormat("bad packet pos {0} ", logplaybackfile.BaseStream.Position);
                return new MAVLinkMessage();
            }

            byte sysid = message.sysid;
            byte compid = message.compid;
            byte packetSeqNo = message.seq;

            //check if sig was included in packet, and we are not ignoring the signature (signing isnt checked else we wont enable signing)
            if (message.sig != null && !MAVlist[sysid,compid].signingignore)
            {
                using (SHA256Managed signit = new SHA256Managed())
                {
                    signit.TransformBlock(signingKey, 0, signingKey.Length, null, 0);
                    signit.TransformFinalBlock(message.buffer, 0, message.Length - MAVLINK_SIGNATURE_BLOCK_LEN + 7);
                    var ctx = signit.Hash;
                    // trim to 48
                    Array.Resize(ref ctx, 6);

                    //Console.WriteLine("linkid {0}, time {1} {2} {3} {4} {5} {6} - {7}", message.sig[0], message.sig[1], message.sig[2], message.sig[3], message.sig[4], message.sig[5], message.sig[6], message.sigTimestamp);

                    for (int i = 0; i < ctx.Length; i++)
                    {
                        if (ctx[i] != message.sig[7 + i])
                        {
                            log.InfoFormat("Packet failed signature but passed crc");
                            return new MAVLinkMessage();
                        }
                    }

                    MAVlist[sysid, compid].linkid = message.sig[0];

                    enableSigning();
                }
            }

            // packet is now verified

            // update packet loss statistics
            if (!logreadmode && MAVlist[sysid, compid].packetlosttimer.AddSeconds(5) < DateTime.Now)
            {
                MAVlist[sysid, compid].packetlosttimer = DateTime.Now;
                MAVlist[sysid, compid].packetslost = (MAVlist[sysid, compid].packetslost*0.8f);
                MAVlist[sysid, compid].packetsnotlost = (MAVlist[sysid, compid].packetsnotlost*0.8f);
            }
            else if (logreadmode && MAVlist[sysid, compid].packetlosttimer.AddSeconds(5) < lastlogread)
开发者ID:ChukRhodes,项目名称:MissionPlanner,代码行数:67,代码来源:MAVLinkInterface.cs

示例5: GetBytes

        public static byte[] GetBytes(byte[] password, byte[] salt, int iterations, int howManyBytes)
        {
            // round up

            uint cBlocks = (uint)((howManyBytes+ HASH_SIZE_IN_BYTES-1)/HASH_SIZE_IN_BYTES);

            // seed for the pseudo-random fcn: salt + block index
            byte[] saltAndIndex = new byte[salt.Length + 4];
            Array.Copy(salt, 0, saltAndIndex, 0, salt.Length);

            byte[] output = new byte[cBlocks*HASH_SIZE_IN_BYTES];
            int outputOffset = 0;

            SHA256Managed innerHash = new SHA256Managed();
            SHA256Managed outerHash = new SHA256Managed();

            // HMAC says the key must be hashed or padded with zeros
            // so it fits into a single block of the hash in use
            if (password.Length > BLOCK_SIZE_IN_BYTES) {
                password = innerHash.ComputeHash(password);
            }
            byte[] key = new byte[BLOCK_SIZE_IN_BYTES];
            Array.Copy(password, 0, key, 0, password.Length);

            byte[] InnerKey = new byte[BLOCK_SIZE_IN_BYTES];
            byte[] OuterKey = new byte[BLOCK_SIZE_IN_BYTES];
            for (int i = 0; i < BLOCK_SIZE_IN_BYTES; ++i) {
                InnerKey[i] = (byte)(key[i] ^ IPAD);
                OuterKey[i] = (byte)(key[i] ^ OPAD);
            }

            // for each block of desired output
            for (int iBlock = 0; iBlock < cBlocks; ++iBlock) {
                // seed HMAC with salt & block index
                _incrementBigEndianIndex(saltAndIndex, salt.Length);
                byte[] U = saltAndIndex;

                for (int i = 0; i < iterations; ++i) {
                    // simple implementation of HMAC-SHA-256
                    innerHash.Initialize();
                    innerHash.TransformBlock(InnerKey, 0,
                        BLOCK_SIZE_IN_BYTES, InnerKey, 0);
                    innerHash.TransformFinalBlock(U, 0, U.Length);

                    byte[] temp = innerHash.Hash;

                    outerHash.Initialize();
                    outerHash.TransformBlock(OuterKey, 0,
                        BLOCK_SIZE_IN_BYTES, OuterKey, 0);
                    outerHash.TransformFinalBlock(temp, 0, temp.Length);

                    U = outerHash.Hash; // U = result of HMAC

                    // xor result into output buffer
                    _xorByteArray(U, 0, HASH_SIZE_IN_BYTES,
                        output, outputOffset);
                }
                outputOffset += HASH_SIZE_IN_BYTES;
            }
            byte[] result = new byte[howManyBytes];
            Array.Copy(output, 0, result, 0, howManyBytes);
            return result;
        }
开发者ID:roman87,项目名称:Dev,代码行数:63,代码来源:Crypto.cs

示例6: GeneratePlayReadyContentKey

        /// <summary>
        /// Generates the play ready content key.
        /// </summary>
        /// <param name="keySeed">The key seed.</param>
        /// <param name="keyId">The key id.</param>
        /// <returns>The content key.</returns>
        public static byte[] GeneratePlayReadyContentKey(byte[] keySeed, Guid keyId)
        {
            if (keySeed == null)
            {
                throw new ArgumentNullException("keySeed");
            }

            byte[] contentKey = new byte[EncryptionUtils.KeySizeInBytesForAes128];

            // Truncate the key seed to 30 bytes.
            byte[] truncatedKeySeed = new byte[30];
            if (keySeed.Length < truncatedKeySeed.Length)
            {
                throw new ArgumentOutOfRangeException("keySeed", "KeySeed must be at least 30 bytes in length");
            }

            Array.Copy(keySeed, truncatedKeySeed, truncatedKeySeed.Length);

            // Get the keyId as a byte array.
            byte[] keyIdAsBytes = keyId.ToByteArray();

            using (SHA256Managed sha_A = new SHA256Managed())
            using (SHA256Managed sha_B = new SHA256Managed())
            using (SHA256Managed sha_C = new SHA256Managed())
            {
                // Create sha_A_Output buffer.  It is the SHA of the truncatedKeySeed and the keyIdAsBytes.
                sha_A.TransformBlock(truncatedKeySeed, 0, truncatedKeySeed.Length, truncatedKeySeed, 0);
                sha_A.TransformFinalBlock(keyIdAsBytes, 0, keyIdAsBytes.Length);
                byte[] sha_A_Output = sha_A.Hash;

                // Create sha_B_Output buffer.  It is the SHA of the truncatedKeySeed, the keyIdAsBytes, and
                // the truncatedKeySeed again.
                sha_B.TransformBlock(truncatedKeySeed, 0, truncatedKeySeed.Length, truncatedKeySeed, 0);
                sha_B.TransformBlock(keyIdAsBytes, 0, keyIdAsBytes.Length, keyIdAsBytes, 0);
                sha_B.TransformFinalBlock(truncatedKeySeed, 0, truncatedKeySeed.Length);
                byte[] sha_B_Output = sha_B.Hash;

                // Create sha_C_Output buffer.  It is the SHA of the truncatedKeySeed, the keyIdAsBytes, 
                // the truncatedKeySeed again, and the keyIdAsBytes again.
                sha_C.TransformBlock(truncatedKeySeed, 0, truncatedKeySeed.Length, truncatedKeySeed, 0);
                sha_C.TransformBlock(keyIdAsBytes, 0, keyIdAsBytes.Length, keyIdAsBytes, 0);
                sha_C.TransformBlock(truncatedKeySeed, 0, truncatedKeySeed.Length, truncatedKeySeed, 0);
                sha_C.TransformFinalBlock(keyIdAsBytes, 0, keyIdAsBytes.Length);
                byte[] sha_C_Output = sha_C.Hash;

                for (int i = 0; i < EncryptionUtils.KeySizeInBytesForAes128; i++)
                {
                    contentKey[i] = Convert.ToByte(sha_A_Output[i] ^ sha_A_Output[i + EncryptionUtils.KeySizeInBytesForAes128]
                                                   ^ sha_B_Output[i] ^ sha_B_Output[i + EncryptionUtils.KeySizeInBytesForAes128]
                                                   ^ sha_C_Output[i] ^ sha_C_Output[i + EncryptionUtils.KeySizeInBytesForAes128]);
                }
            }

            return contentKey;
        }
开发者ID:gittehub,项目名称:azure-sdk-for-media-services,代码行数:61,代码来源:CommonEncryption.cs

示例7: GeneratePlayReadyContentKey

 static public byte[] GeneratePlayReadyContentKey(byte[] keySeed, Guid keyId)
 {
     const int DRM_AES_KEYSIZE_128 = 16;
     byte[] contentKey = new byte[DRM_AES_KEYSIZE_128];
     //
     // Truncate the key seed to 30 bytes, key seed must be at least 30 bytes long.
     //
     byte[] truncatedKeySeed = new byte[30];
     Array.Copy(keySeed, truncatedKeySeed, truncatedKeySeed.Length);
     //
     // Get the keyId as a byte array
     //
     byte[] keyIdAsBytes = keyId.ToByteArray();
     //
     // Create sha_A_Output buffer. It is the SHA of the truncatedKeySeed and the keyIdAsBytes
     //
     SHA256Managed sha_A = new SHA256Managed();
     sha_A.TransformBlock(truncatedKeySeed, 0, truncatedKeySeed.Length, truncatedKeySeed, 0);
     sha_A.TransformFinalBlock(keyIdAsBytes, 0, keyIdAsBytes.Length);
     byte[] sha_A_Output = sha_A.Hash;
     //
     // Create sha_B_Output buffer. It is the SHA of the truncatedKeySeed, the keyIdAsBytes, and
     // the truncatedKeySeed again.
     //
     SHA256Managed sha_B = new SHA256Managed();
     sha_B.TransformBlock(truncatedKeySeed, 0, truncatedKeySeed.Length, truncatedKeySeed, 0);
     sha_B.TransformBlock(keyIdAsBytes, 0, keyIdAsBytes.Length, keyIdAsBytes, 0);
     sha_B.TransformFinalBlock(truncatedKeySeed, 0, truncatedKeySeed.Length);
     byte[] sha_B_Output = sha_B.Hash;
     //
     // Create sha_C_Output buffer. It is the SHA of the truncatedKeySeed, the keyIdAsBytes,
     // the truncatedKeySeed again, and the keyIdAsBytes again.
     //
     SHA256Managed sha_C = new SHA256Managed();
     sha_C.TransformBlock(truncatedKeySeed, 0, truncatedKeySeed.Length, truncatedKeySeed, 0);
     sha_C.TransformBlock(keyIdAsBytes, 0, keyIdAsBytes.Length, keyIdAsBytes, 0);
     sha_C.TransformBlock(truncatedKeySeed, 0, truncatedKeySeed.Length, truncatedKeySeed, 0);
     sha_C.TransformFinalBlock(keyIdAsBytes, 0, keyIdAsBytes.Length);
     byte[] sha_C_Output = sha_C.Hash;
     for (int i = 0; i < DRM_AES_KEYSIZE_128; i++)
     {
         contentKey[i] = Convert.ToByte(sha_A_Output[i] ^ sha_A_Output[i + DRM_AES_KEYSIZE_128]
         ^ sha_B_Output[i] ^ sha_B_Output[i + DRM_AES_KEYSIZE_128]
         ^ sha_C_Output[i] ^ sha_C_Output[i + DRM_AES_KEYSIZE_128]);
     }
     return contentKey;
 }
开发者ID:sk-bln,项目名称:Azure-Media-Services-Explorer,代码行数:47,代码来源:DynamicEncryption.cs


注:本文中的System.Security.Cryptography.SHA256Managed.TransformBlock方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。