private 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; }
public async Task HashFile(bool useMemoryStream, bool offset = false) { Output = string.Empty; this.StateHasChanged(); var nl = Environment.NewLine; foreach (var file in await fileReaderService.CreateReference(inputElement).EnumerateFilesAsync()) { IFileInfo fileInfo = null; fileInfo = await file.ReadFileInfoAsync(); if (DebugOutput) { Output += $"{nameof(IFileInfo)}.{nameof(fileInfo.Name)}: {fileInfo.Name}{nl}"; Output += $"{nameof(IFileInfo)}.{nameof(fileInfo.Size)}: {fileInfo.Size}{nl}"; Output += $"{nameof(IFileInfo)}.{nameof(fileInfo.Type)}: {fileInfo.Type}{nl}"; Output += $"{nameof(IFileInfo)}.{nameof(fileInfo.LastModifiedDate)}: {fileInfo.LastModifiedDate?.ToString(CultureInfo.InvariantCulture) ?? "(N/A)"}{nl}"; Output += $"Reading file..."; this.StateHasChanged(); } var stopWatch = new System.Diagnostics.Stopwatch(); stopWatch.Start(); var outputBuffer = new StringBuilder(); using (var hash = new SHA256Managed()) { if (useMemoryStream) { using (var fs = UseBufferSize ? await file.CreateMemoryStreamAsync(BufferSize) : await file.CreateMemoryStreamAsync()) { hash.ComputeHash(fs); } } else { using (var fs = await file.OpenReadAsync()) { var bufferSize = UseBufferSize ? BufferSize : 4096 * 8; if (DebugOutput) { outputBuffer.AppendLine($"Using chunks of size {bufferSize}"); } var targetBufferSize = bufferSize; if (offset) { targetBufferSize = (int)fileInfo?.Size; } var buffer = new byte[targetBufferSize]; int count = 0; var targetOffset = 0; var len = offset ? Math.Min(bufferSize, (int)fileInfo.Size) : buffer.Length; while ((count = await fs.ReadAsync(buffer, targetOffset, len)) != 0) { if (offset) { targetOffset += count; } if (DebugOutput) { outputBuffer.AppendLine($"Hashing {count} bytes. {fs.Position} / {fs.Length}"); } if (!offset) { hash.TransformBlock(buffer, 0, count, buffer, 0); } if (OutputString) { outputBuffer.AppendLine("BEGIN BUFFER DUMP"); outputBuffer.AppendLine(Encoding.UTF8.GetString(buffer)); outputBuffer.AppendLine("END BUFFER DUMP"); } } if (!offset) { hash.TransformFinalBlock(buffer, 0, count); } else { hash.ComputeHash(buffer); } } } var sb = new StringBuilder(hash.HashSize / 4); foreach (var b in hash.Hash) { sb.AppendFormat("{0:x2}", b); } stopWatch.Stop(); if (DebugOutput) { Output += $"Done hashing file {fileInfo.Name}.{nl}"; } Output += sb.ToString(); if (outputBuffer.Length > 0) { Output += $"{nl}{nl}Debug output:{nl}"; Output += outputBuffer.ToString(); } } } Output += $"{nl}--DONE"; this.StateHasChanged(); }
public void TransformFinalBlock(byte[] inputBuffer, int inputOffset, int inputCount) { _sha256.TransformFinalBlock(inputBuffer, inputOffset, inputCount); Hash = _sha256.Hash; }
byte[] IHashAlgorithm.GetRunningHash () { var copy = new SHA256Managed (this); copy.TransformFinalBlock (empty, 0, 0); return copy.Hash; }
private void InitRead() { CipherInfo Cipher1 = null; CipherInfo Cipher2 = null; byte[] Key2 = null; byte[] IV2 = null; try { // File format version has been read var Subversion = m_sBaseStream.ReadByte(); // Subversion if (Subversion > SUB_VERSION) { throw new InvalidDataException("Invalid sub version, please check for newer MultiCipher Plugin"); } if (Subversion != SUB_VERSION) { MessageService.ShowWarning("MultiCipher Plugin:", string.Format("You are opening Version 2.{0} of MultiCipher Keepass Database, a one way upgrade will be performed to version 2.2", Subversion), "Once saved, you will not be able to open the database in an older version of the plugin."); } m_Config.Algorithm1 = (SymAlgoCode)m_sBaseStream.ReadByte(); Cipher1 = new CipherInfo(m_Config.Algorithm1); Cipher1.SetKey(m_1Key32, m_1IV16); m_Config.Algorithm2 = (SymAlgoCode)m_sBaseStream.ReadByte(); Cipher2 = new CipherInfo(m_Config.Algorithm2); m_Config.KeyOption = (KeyOption)m_sBaseStream.ReadByte(); if (m_Config.KeyOption == KeyOption.Yubikey_HMAC_SHA1) { m_Config.YubikeySlot = (byte)m_sBaseStream.ReadByte(); m_Config.YubikeyChallengeLength = (byte)m_sBaseStream.ReadByte(); m_Config.YubikeyChallenge = new byte[64]; m_sBaseStream.Read(m_Config.YubikeyChallenge, 0, 64); } m_sBaseStream.ReadByte(); // Derivation Method ignore for now var MasterSeed = new byte[32]; m_sBaseStream.Read(MasterSeed, 0, 32); var TransformSeed = new byte[32]; m_sBaseStream.Read(TransformSeed, 0, 32); IV2 = new byte[Cipher2.IVSizeInBytes]; m_sBaseStream.Read(IV2, 0, (int)Cipher2.IVSizeInBytes); byte[] NumRoundByteArray = new byte[8]; m_sBaseStream.Read(NumRoundByteArray, 0, 8); ulong NumRounds = Extensions.ToLittleEndianUInt64(NumRoundByteArray); m_Config.Key2Transformations = NumRounds; byte[] PlainTextLengthBytes = new byte[4]; m_sBaseStream.Read(PlainTextLengthBytes, 0, 4); var PlainTextLength = Extensions.ToLittleEndianInt32(PlainTextLengthBytes); int ContentBufferLength = Get64BlockAlignSize(PlainTextLength); byte[] PlainTextBuffer = new byte[ContentBufferLength]; int read = m_sBaseStream.Read(PlainTextBuffer, 0, ContentBufferLength); if (read != ContentBufferLength) { throw new InvalidDataException("Invalid Data length"); } using (var Transformer = Cipher1.GetCipherTransformer()) Transformer.Decrypt(PlainTextBuffer, 0, ContentBufferLength); var PlainTextHash32 = new SHA256Managed(); PlainTextHash32.TransformFinalBlock(PlainTextBuffer, 0, PlainTextLength); byte[] ContentBuffer2 = new byte[ContentBufferLength]; read = m_sBaseStream.Read(ContentBuffer2, 0, ContentBufferLength); if (read != ContentBufferLength) { throw new InvalidDataException("Invalid Data length 2"); } Key2 = m_Config.Get2ndKey32(PlainTextHash32.Hash, MasterSeed, TransformSeed); Cipher2.SetKey(Key2, IV2); try { using (var Transformer2 = Cipher2.GetCipherTransformer()) Transformer2.Decrypt(ContentBuffer2, 0, ContentBufferLength); for (int i = 0; i < PlainTextLength; i++) { PlainTextBuffer[i] ^= ContentBuffer2[i]; } } finally { Array.Clear(ContentBuffer2, 0, ContentBuffer2.Length); } m_sBaseStream.Read(ContentBuffer2, 0, 1); // read past end m_ReadPlainTextStream = new MemoryStream(PlainTextBuffer, 0, PlainTextLength, false, true); } finally { if (Key2 != null) { MemUtil.ZeroByteArray(Key2); } if (IV2 != null) { MemUtil.ZeroArray(IV2); } if (Cipher1 != null) { Cipher1.Dispose(); } if (Cipher2 != null) { Cipher2.Dispose(); } } }
public static byte[] GetBytes(byte[] payload, 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 (payload.Length > BLOCK_SIZE_IN_BYTES) { payload = innerHash.ComputeHash(payload); } byte[] key = new byte[BLOCK_SIZE_IN_BYTES]; Array.Copy(payload, 0, key, 0, payload.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); }
public static void SHA256(this ReadOnlySpan <byte> data, Span <byte> hash) { using (var sha = new SHA256Managed()) { sha.TransformFinalBlock(data, hash); } }
public byte[] GenerateMAVLinkPacket20(MAVLINK_MSG_ID messageType, object indata, bool sign = false) { byte[] data; data = MavlinkUtil.StructureToByteArray(indata); MavlinkUtil.trim_payload(ref data); int extra = 0; if (sign) extra = MAVLINK_SIGNATURE_BLOCK_LEN; byte[] packet = new byte[data.Length + MAVLINK_NUM_NON_PAYLOAD_BYTES + extra]; packet[0] = MAVLINK_STX; packet[1] = (byte)data.Length; packet[2] = 0;//incompat signing if (sign) packet[2] |= MAVLINK_IFLAG_SIGNED; packet[3] = 0;//compat packet[4] = (byte)packetcount; packetcount++; packet[5] = 255; // this is always 255 - MYGCS packet[6] = (byte)MAV_COMPONENT.MAV_COMP_ID_MISSIONPLANNER; packet[7] = (byte)((UInt32)messageType); packet[8] = (byte)((UInt32)messageType >> 8); packet[9] = (byte)((UInt32)messageType >> 16); int i = MAVLINK_NUM_HEADER_BYTES; foreach (byte b in data) { packet[i] = b; i++; } ushort checksum = MavlinkCRC.crc_calculate(packet, data.Length + MAVLINK_NUM_HEADER_BYTES); checksum = MavlinkCRC.crc_accumulate(MAVLINK_MESSAGE_INFOS.GetMessageInfo((uint)messageType).crc, 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 (sign) { //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 * 100); if (timestamp == lasttimestamp) timestamp++; lasttimestamp = timestamp; var timebytes = BitConverter.GetBytes(timestamp); var sig = new byte[7]; // 13 includes the outgoing hash sig[0] = sendlinkid; Array.Copy(timebytes, 0, sig, 1, 6); // timestamp //Console.WriteLine("gen linkid {0}, time {1} {2} {3} {4} {5} {6} {7}", sig[0], sig[1], sig[2], sig[3], sig[4], sig[5], sig[6], timestamp); if (signingKey == null || signingKey.Length != 32) { signingKey = new byte[32]; } 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++; } } } return packet; }
public byte[] TransformFinalBlock(byte[] inputBuffer, int inputOffset, int inputCount) { sha1.TransformFinalBlock(inputBuffer, inputOffset, inputCount); return(sha256.TransformFinalBlock(inputBuffer, inputOffset, inputCount)); }
/// <summary> /// Processes the BLOB an copy to s3. /// </summary> /// <returns>Returns the result of copy from BLOB to S3.</returns> private async Task <BlobS3HandlerResult> ProcessBlobAndCopyToS3Async() { var result = new BlobS3HandlerResult(); var validationResult = await this.ValidateRequestAsync().ConfigureAwait(false); if (!validationResult.Item1) { return(validationResult.Item2); } var blobToCopy = validationResult.Item3; await blobToCopy.FetchAttributesAsync().ConfigureAwait(false); var remainingBytes = blobToCopy.Properties.Length; long readPosition = 0; // To be used offset / position from where to start reading from BLOB. var initiateMultipartUploadRequest = new InitiateMultipartUploadRequest { BucketName = this.TargetS3Bucket, Key = this.TargetS3File }; // Will use UploadId from this response. var initiateMultipartUploadResponse = this.S3Client.InitiateMultipartUpload(initiateMultipartUploadRequest); var uploadPartResponses = new List <UploadPartResponse>(); try { var partCounter = 0; // To increment on each read of parts and use it as part number. var sha256 = new SHA256Managed(); while (remainingBytes > 0) { // Determine the size when final block reached as it might be less than Part size. // Will be PartSize except final block. var bytesToCopy = Math.Min(PartSize, remainingBytes); using (MemoryStream memoryStream = new MemoryStream()) { // To download part from BLOB. await blobToCopy.DownloadRangeToStreamAsync(memoryStream, readPosition, bytesToCopy).ConfigureAwait(false); memoryStream.Position = 0; partCounter++; var uploadRequest = new UploadPartRequest { BucketName = this.TargetS3Bucket, Key = this.TargetS3File, UploadId = initiateMultipartUploadResponse.UploadId, PartNumber = partCounter, PartSize = bytesToCopy, InputStream = memoryStream }; var uploadPartResponse = this.S3Client.UploadPart(uploadRequest); uploadPartResponses.Add(uploadPartResponse); remainingBytes -= bytesToCopy; readPosition += bytesToCopy; // $"Uploaded part with part number {partCounter}, size {bytesToCopy}bytes and remaining {remainingBytes}bytes to read.") // Calculate the checksum value. if (remainingBytes <= 0) { sha256.TransformFinalBlock(memoryStream.ToArray(), 0, (int)bytesToCopy); } else { var bytesToSend = memoryStream.ToArray(); sha256.TransformBlock(bytesToSend, 0, (int)bytesToCopy, bytesToSend, 0); } } } result.Sha256CheckSum = BitConverter.ToString(sha256.Hash).Replace("-", string.Empty); var completeMultipartUploadRequest = new CompleteMultipartUploadRequest { BucketName = this.TargetS3Bucket, Key = this.TargetS3File, UploadId = initiateMultipartUploadResponse.UploadId }; completeMultipartUploadRequest.AddPartETags(uploadPartResponses); var completeMultipartUploadResponse = await this.S3Client.CompleteMultipartUploadAsync(completeMultipartUploadRequest).ConfigureAwait(false); result.HasSucceeded = true; result.S3Path = completeMultipartUploadResponse.Location; } catch (Exception exception) { result.HasSucceeded = false; result.Message = exception.Message; var abortMultipartUploadRequest = new AbortMultipartUploadRequest { BucketName = this.TargetS3Bucket, Key = this.TargetS3File, UploadId = initiateMultipartUploadResponse.UploadId }; await this.S3Client.AbortMultipartUploadAsync(abortMultipartUploadRequest).ConfigureAwait(false); } return(result); }
/// <summary> /// Encrypts a string using the SHA256 algorithm. /// </summary> private static string Encrypt(string plainMessage) { byte[] data = Encoding.UTF8.GetBytes(plainMessage); using (HashAlgorithm sha = new SHA256Managed()) { byte[] encryptedBytes = sha.TransformFinalBlock(data, 0, data.Length); return Convert.ToBase64String(sha.Hash); } }
/// <summary> /// Parses a Bitcoin transaction. /// </summary> /// <param name="blockMemoryStreamReader"> /// Provides access to a section of the Bitcoin blockchain file. /// </param> /// <returns> /// The Bitcoin transaction that was parsed. /// </returns> private static Transaction ParseTransaction(BlockMemoryStreamReader blockMemoryStreamReader) { Transaction transaction = new Transaction(); int positionInBaseStreamAtTransactionStart = (int)blockMemoryStreamReader.BaseStream.Position; transaction.TransactionVersion = blockMemoryStreamReader.ReadUInt32(); int inputsCount = (int)blockMemoryStreamReader.ReadVariableLengthInteger(); bool isSegWit = false; if (inputsCount == 0) { byte flag = blockMemoryStreamReader.ReadByte(); if (flag != 0x01) { throw new InvalidBlockchainContentException(string.Format(CultureInfo.InvariantCulture, "Unknown transaction serialization. No input transactions, but SegWit flag was {0} instead of 1.", flag)); } inputsCount = (int)blockMemoryStreamReader.ReadVariableLengthInteger(); isSegWit = true; } for (int inputIndex = 0; inputIndex < inputsCount; inputIndex++) { TransactionInput transactionInput = BlockchainParser.ParseTransactionInput(blockMemoryStreamReader); transaction.AddInput(transactionInput); } int outputsCount = (int)blockMemoryStreamReader.ReadVariableLengthInteger(); for (int outputIndex = 0; outputIndex < outputsCount; outputIndex++) { TransactionOutput transactionOutput = BlockchainParser.ParseTransactionOutput(blockMemoryStreamReader); transaction.AddOutput(transactionOutput); } int positionInBaseStreamAfterTxOuts = (int)blockMemoryStreamReader.BaseStream.Position; if (isSegWit) { for (int inputIndex = 0; inputIndex < inputsCount; inputIndex++) { Witness witness = BlockchainParser.ParseWitness(blockMemoryStreamReader); transaction.AddWitness(witness); } } // TODO: Need to find out more details about the semantic of TransactionLockTime. transaction.TransactionLockTime = blockMemoryStreamReader.ReadUInt32(); int positionInBaseStreamAfterTransactionEnd = (int)blockMemoryStreamReader.BaseStream.Position; using (SHA256Managed sha256 = new SHA256Managed()) { //// We need to calculate the double SHA256 hash of this transaction. //// We need to access the buffer that contains the transaction that we jut read through. //// Here we take advantage of the fact that the entire block was loaded as an in-memory buffer. //// The base stream of blockMemoryStreamReader is that in-memory buffer. //byte[] baseBuffer = blockMemoryStreamReader.GetBuffer(); //int transactionBufferSize = positionInBaseStreamAfterTransactionEnd - positionInBaseStreamAtTransactionStart; byte[] baseBuffer = blockMemoryStreamReader.GetBuffer(), hash1 = null; if (isSegWit) { using (SHA256Managed innerSHA256 = new SHA256Managed()) { //// SegWit transactions are still identified by their txid, which is double SHA256 of the old //// serialization format (i.e. no marker, flag, or witness). So, we need to calculate the txid by //// recreating the old format as the input to the hash algorithm. // First, the version number innerSHA256.TransformBlock(baseBuffer, positionInBaseStreamAtTransactionStart, 4, baseBuffer, positionInBaseStreamAtTransactionStart); // Skip the marker and flag (each one byte), then read in txins and txouts (starting with txin count) int txStart = positionInBaseStreamAtTransactionStart + 6; int txSize = positionInBaseStreamAfterTxOuts - txStart; innerSHA256.TransformBlock(baseBuffer, txStart, txSize, baseBuffer, txStart); ///// After the transactions comes the segregated witness data, which is not included in the txid. ///// The only thing left to add to calcualte the txid is nLockTime located in the last 4 bytes int lockTimeStart = positionInBaseStreamAfterTransactionEnd - 4; innerSHA256.TransformFinalBlock(baseBuffer, lockTimeStart, 4); hash1 = innerSHA256.Hash; } } else { int transactionBufferSize = positionInBaseStreamAfterTransactionEnd - positionInBaseStreamAtTransactionStart; hash1 = sha256.ComputeHash(baseBuffer, positionInBaseStreamAtTransactionStart, transactionBufferSize); } // byte[] hash1 = sha256.ComputeHash(baseBuffer, positionInBaseStreamAtTransactionStart, transactionBufferSize); transaction.TransactionHash = new ByteArray(sha256.ComputeHash(hash1).ReverseByteArray()); } return(transaction); }
/// <summary> /// Download Large File as chunk and upload as chunk into BLOB. /// </summary> public async Task ProcessLargeFile() { // Create Storage account reference. CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageAccount")); // Create the blob client. CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient(); // Retrieve reference to a container. CloudBlobContainer container = blobClient.GetContainerReference(CloudConfigurationManager.GetSetting("ContainerName")); container.CreateIfNotExists(); // Create Blob reference. CloudBlockBlob blob = container.GetBlockBlobReference(CloudConfigurationManager.GetSetting("BlobFileName")); string urlToDownload = CloudConfigurationManager.GetSetting("DownloadURL"); // Provide valid URL from where the large file can be downloaded. Stopwatch stopwatch = Stopwatch.StartNew(); try { using (HttpClient httpClient = new HttpClient()) { var httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, new Uri(urlToDownload)) { // To avoid error related to 'An existing connection was forcibly closed by the remote host'. Use Http1.0 instead of Http1.1. Version = HttpVersion.Version10 }; using (HttpResponseMessage response = await httpClient.SendAsync(httpRequestMessage, HttpCompletionOption.ResponseHeadersRead).ConfigureAwait(false)) { using (Stream stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false)) { const int pageSizeInBytes = 104857600; // 100MB. As Blob chunk max size is 100MB as of now. var blockIds = new List <string>(); var sha256 = new SHA256Managed(); var bytesRemaing = response.Content.Headers.ContentLength.Value; // Read Total file size from the header. int blockIdentifier = 0; while (bytesRemaing > 0) { blockIdentifier++; var bytesToCopy = (int)Math.Min(bytesRemaing, pageSizeInBytes); var bytesToSend = new byte[bytesToCopy]; var bytesCountRead = await ReadStreamAndAccumulate(stream, bytesToSend, bytesToCopy); // Instead of calculating bytes remaining to exit the While loop, we can use bytesCountRead as bytesCountRead will be 0 when there are no more bytes to read form the stream. bytesRemaing -= bytesCountRead; this.logger.WriteLine($"bytes read: {bytesCountRead}"); this.logger.WriteLine($"bytes remaining: {bytesRemaing}"); string base64BlockId = Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes(string.Format("largefile1BlockId{0}", blockIdentifier.ToString("0000000")))); blockIds.Add(base64BlockId); // Calculate the checksum value. if (bytesRemaing <= 0) { sha256.TransformFinalBlock(bytesToSend, 0, bytesCountRead); } else { sha256.TransformBlock(bytesToSend, 0, bytesCountRead, bytesToSend, 0); } await blob.PutBlockAsync(base64BlockId, new MemoryStream(bytesToSend), null); } var checksum = BitConverter.ToString(sha256.Hash).Replace("-", string.Empty); this.logger.WriteLine($"Hash value is : {checksum}"); await blob.PutBlockListAsync(blockIds); await Task.FromResult(0); } } } } catch (Exception ex) { this.logger.WriteLine(ex.Message); throw; } finally { stopwatch.Stop(); this.logger.WriteLine($"Execution time in mins: {stopwatch.Elapsed.TotalMinutes}"); } }
public AES3DESStream(Stream sbaseStream, bool bWriting, byte[] AESKey, byte[] AESIV, CompositeKey Level2Key) : base(sbaseStream, bWriting) { m_RandomStream = new MemoryStream(); m_ContentLength = 0; m_hash = new SHA256Managed(); m_2Key = Level2Key; ICryptoTransform AESTransformer; RijndaelManaged r = new RijndaelManaged(); r.BlockSize = 128; r.IV = AESIV; r.KeySize = 256; r.Key = AESKey; r.Mode = CipherMode.CBC; r.Padding = PaddingMode.None; // We are taking care of the padding to make sure it is within 32 byte boundary if (bWriting) { CryptoRandom cr = CryptoRandom.Instance; m_MasterSeed = cr.GetRandomBytes(32); m_TransformSeed = cr.GetRandomBytes(32); m_3DESIV = cr.GetRandomBytes(8); m_NumRounds = 10000; m_sBaseStream.WriteByte(0); m_sBaseStream.Write(m_MasterSeed, 0, 32); m_sBaseStream.Write(m_TransformSeed, 0, 32); m_sBaseStream.Write(m_3DESIV, 0, 8); m_sBaseStream.Write(Extensions.GetLittleEndianBytes(m_NumRounds), 0, 8); m_AESStream = new MemoryStream(); AESTransformer = r.CreateEncryptor(); m_CryptoStreamAES = new CryptoStream(m_AESStream, AESTransformer, CryptoStreamMode.Write); } else { // File format version and algorithm has been read m_MasterSeed = new byte[32]; sbaseStream.Read(m_MasterSeed, 0, 32); m_TransformSeed = new byte[32]; sbaseStream.Read(m_TransformSeed, 0, 32); m_3DESIV = new byte[8]; sbaseStream.Read(m_3DESIV, 0, 8); byte[] buffer = new byte[8]; sbaseStream.Read(buffer, 0, 8); m_NumRounds = Extensions.ToLittleEndianUInt64(buffer); byte[] len = new byte[4]; m_sBaseStream.Read(len, 0, 4); m_ContentLength = Extensions.ToLittleEndianInt32(len); int remainder = m_ContentLength % 32; int buflen = m_ContentLength + 32; if (remainder > 0) { buflen -= remainder; } else { buflen -= 32; } byte[] AESBuffer = new byte[buflen]; AESTransformer = r.CreateDecryptor(); m_CryptoStreamAES = new CryptoStream(m_sBaseStream, AESTransformer, CryptoStreamMode.Read); int read = m_CryptoStreamAES.Read(AESBuffer, 0, buflen); if (read != buflen) { throw new InvalidDataException("Invalid Data length"); } byte[] DES3Buffer = new byte[buflen]; m_hash.TransformFinalBlock(AESBuffer, 0, buflen); CryptoStream Stream3DES = new CryptoStream(m_sBaseStream, Get3DES().CreateDecryptor(), CryptoStreamMode.Read); read = Stream3DES.Read(DES3Buffer, 0, buflen); if (read != buflen) { throw new InvalidDataException("Invalid Data length 2"); } for (int i = 0; i < m_ContentLength; i++) { AESBuffer[i] ^= DES3Buffer[i]; } m_AESStream = new MemoryStream(AESBuffer, 0, m_ContentLength); } }
private async Task <SetStatus> SetValueCoreAsyncCore(TKey key, Stream value) { ulong totalBytesRead = 0; const long bufferSize = 4096; var tmpFileName = Path.Combine(CachePath.FullName, Guid.NewGuid().ToString()); string hash = null; using (var shaHasher = new SHA256Managed()) { using (var writer = File.OpenWrite(tmpFileName)) { byte[] oldBuffer; int oldBytesRead; var buffer = new byte[bufferSize]; var bytesRead = await value.ReadAsync(buffer, 0, buffer.Length).ConfigureAwait(false); totalBytesRead += Convert.ToUInt32(bytesRead); do { oldBytesRead = bytesRead; oldBuffer = buffer; buffer = new byte[bufferSize]; bytesRead = await value.ReadAsync(buffer, 0, buffer.Length).ConfigureAwait(false); totalBytesRead += Convert.ToUInt32(bytesRead); if (bytesRead == 0) { shaHasher.TransformFinalBlock(oldBuffer, 0, oldBytesRead); await writer.WriteAsync(oldBuffer, 0, oldBytesRead).ConfigureAwait(false); } else { shaHasher.TransformBlock(oldBuffer, 0, oldBytesRead, oldBuffer, 0); await writer.WriteAsync(oldBuffer, 0, oldBytesRead).ConfigureAwait(false); } }while (bytesRead != 0 && totalBytesRead <= MaximumStorageCapacity); } if (totalBytesRead > MaximumStorageCapacity) { File.Delete(tmpFileName); // remove the file, we can't keep it anyway return(SetStatus.DataTooLarge); } var shaHashBytes = shaHasher.Hash; hash = BitConverter.ToString(shaHashBytes).Replace("-", string.Empty); } var isNew = !await ContainsKeyAsync(key).ConfigureAwait(false); var cachePath = GetPath(hash); var cachePathDir = Path.GetDirectoryName(cachePath); if (!Directory.Exists(cachePathDir)) { Directory.CreateDirectory(cachePathDir); } File.Move(tmpFileName, cachePath); var cacheFileInfo = new FileInfo(cachePath); _fileLookup[key] = new FileEntry <TKey>(key, cachePath); //cachePath; var cacheEntry = new CacheEntry <TKey>(key, Convert.ToUInt64(cacheFileInfo.Length)); _entryLookup[key] = cacheEntry; if (isNew) { EntryAdded?.Invoke(this, cacheEntry); } else { EntryUpdated?.Invoke(this, cacheEntry); } ApplyCachePolicy(); return(SetStatus.Success); }
public static void Upload(string vaultName, string fileToUpload, Amazon.RegionEndpoint awsRegion) { try { if (string.IsNullOrEmpty(vaultName)) { throw new Exception("No vault specified"); } if (string.IsNullOrEmpty(fileToUpload) || !File.Exists(fileToUpload)) { throw new Exception($"Invalid file '{fileToUpload}'"); } // Verify that the dynamo table exists InitializeDynamo(awsRegion, dynamoTableName); string archiveName = Path.GetFileName(fileToUpload); fileSize = new FileInfo(fileToUpload).Length; using (var glacier = new AmazonGlacierClient(awsRegion)) using (FileStream fs = File.OpenRead(fileToUpload)) using (SHA256Managed sha = new SHA256Managed()) { // Do the SHA256 hash in a background worker to avoid blocking ThreadPool.QueueUserWorkItem(unused => { byte[] lastBuffer = new byte[] { }; while (!buffersDone || !hashBuffers.IsEmpty) { Tuple <byte[], int> chunkTohash; if (hashBuffers.TryDequeue(out chunkTohash)) { sha.TransformBlock(chunkTohash.Item1, 0, chunkTohash.Item2, null, 0); lastBuffer = chunkTohash.Item1; } Thread.Sleep(10); } sha.TransformFinalBlock(lastBuffer, 0, 0); hashComplete = true; }); long partSize = 128 * 1024 * 1024; var initUploadRequest = new Amazon.Glacier.Model.InitiateMultipartUploadRequest(); initUploadRequest.ArchiveDescription = archiveName; initUploadRequest.PartSize = partSize; initUploadRequest.VaultName = vaultName; var initResponse = glacier.InitiateMultipartUpload(initUploadRequest); long position = 0; fs.Seek(0, SeekOrigin.Begin); List <byte[]> treeHashes = new List <byte[]>(); while (true) { byte[] buffer = new byte[partSize]; int bytesRead = fs.Read(buffer, 0, (int)partSize); if (bytesRead == 0) { break; } using (MemoryStream ms = new MemoryStream(buffer)) { ms.Seek(0, SeekOrigin.Begin); ms.SetLength(bytesRead); byte[] treeHash = HashUtil.ComputeSHA256TreeHash(buffer, bytesRead); treeHashes.Add(treeHash); ms.Seek(0, SeekOrigin.Begin); var uploadRequest = new Amazon.Glacier.Model.UploadMultipartPartRequest(); uploadRequest.Body = ms; uploadRequest.UploadId = initResponse.UploadId; uploadRequest.VaultName = vaultName; uploadRequest.StreamTransferProgress += OnTransferProgress; uploadRequest.Checksum = BitConverter.ToString(treeHash).Replace("-", "").ToLower(); long firstByte = position; long lastByte = position + bytesRead - 1; uploadRequest.Range = $"bytes {firstByte}-{lastByte}/{fileSize}"; var uploadResponse = glacier.UploadMultipartPart(uploadRequest); } hashBuffers.Enqueue(new Tuple <byte[], int>(buffer, bytesRead)); position += bytesRead; transferredBytes += bytesRead; } buffersDone = true; while (!hashComplete) { Thread.Sleep(10); } var completeUploadRequest = new Amazon.Glacier.Model.CompleteMultipartUploadRequest(); completeUploadRequest.ArchiveSize = fileSize.ToString(); completeUploadRequest.UploadId = initResponse.UploadId; completeUploadRequest.VaultName = vaultName; byte[] fullTreeHash = HashUtil.ComputeSHA256TreeHash(treeHashes.ToArray()); completeUploadRequest.Checksum = BitConverter.ToString(fullTreeHash).Replace("-", "").ToLower(); var completeUploadResponse = glacier.CompleteMultipartUpload(completeUploadRequest); string fileHash = BitConverter.ToString(sha.Hash).Replace("-", String.Empty); Console.WriteLine("File hash: " + fileHash); WriteArchiveToDynamo(completeUploadResponse.ArchiveId, awsRegion, vaultName, archiveName, fileSize, fileHash, completeUploadResponse.Location); Console.WriteLine("Copy and save the following Archive ID for the next step."); Console.WriteLine("Archive ID: {0}", initResponse.UploadId); Console.WriteLine("To continue, press Enter"); Console.ReadKey(); } } catch (AmazonGlacierException e) { Console.WriteLine(e.Message); } catch (AmazonServiceException e) { Console.WriteLine(e.Message); } catch (Exception e) { Console.WriteLine(e.Message); } Console.ReadKey(); }
/// <summary> /// Generate a Mavlink Packet and write to serial /// </summary> /// <param name="messageType">type number = MAVLINK_MSG_ID</param> /// <param name="indata">struct of data</param> public static byte[] GeneratePacket(IDrone drone, int messageType, object indata, bool forcemavlink2 = false, bool forcesigning = false) { lock (objLock) { var data = MavlinkUtil.StructureToByteArray(indata); var packet = new byte[0]; int i = 0; // are we mavlink2 enabled for this sysid/compid if (!drone.MavLink2 && messageType < 256 && !forcemavlink2) { var info = MAVLink.MAVLINK_MESSAGE_INFOS.SingleOrDefault(p => p.MsgId == messageType); if (data.Length != info.minlength) { Array.Resize(ref data, (int)info.minlength); } //Console.WriteLine(DateTime.Now + " PC Doing req "+ messageType + " " + this.BytesToRead); packet = new byte[data.Length + 6 + 2]; packet[0] = MAVLINK1_STX; packet[1] = (byte)data.Length; packet[2] = (byte)_packetCount; _packetCount++; packet[3] = gcssysid; packet[4] = (byte)MAV_COMPONENT.MAV_COMP_ID_MISSIONPLANNER; packet[5] = (byte)messageType; i = 6; foreach (byte b in data) { packet[i] = b; i++; } ushort checksum = MavlinkCRC.crc_calculate(packet, packet[1] + 6); checksum = MavlinkCRC.crc_accumulate(MAVLINK_MESSAGE_INFOS.GetMessageInfo((uint)messageType).crc, 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; } else { // trim packet for mavlink2 MavlinkUtil.TrimePayload(ref data); packet = new byte[data.Length + MAVLINK_NUM_HEADER_BYTES + MAVLINK_NUM_CHECKSUM_BYTES + MAVLINK_SIGNATURE_BLOCK_LEN]; packet[0] = MAVLINK2_STX; packet[1] = (byte)data.Length; packet[2] = 0; // incompat if (drone.Signing || forcesigning) // current mav { packet[2] |= MAVLINK_IFLAG_SIGNED; } packet[3] = 0; // compat packet[4] = (byte)_packetCount; _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_INFOS.GetMessageInfo((uint)messageType).crc, 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 (drone.Signing || forcesigning) { //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 * 100); if (timestamp == drone.TimeStamp) { timestamp++; } drone.TimeStamp = timestamp; var timebytes = BitConverter.GetBytes(timestamp); var sig = new byte[7]; // 13 includes the outgoing hash sig[0] = drone.SendLinkId; Array.Copy(timebytes, 0, sig, 1, 6); // timestamp //Console.WriteLine("gen linkid {0}, time {1} {2} {3} {4} {5} {6} {7}", sig[0], sig[1], sig[2], sig[3], sig[4], sig[5], sig[6], timestamp); var signingKey = drone.SigningKey; if (signingKey == null || signingKey.Length != 32) { signingKey = new byte[32]; } 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++; } } } } return(packet); } }
public static void Sha256(this ReadOnlyByteSequence data, ByteSpan hash) { using var sha = new SHA256Managed(); sha.TransformFinalBlock(data, hash); }
/// <summary> /// Returns true if there are more chunks remaining, false if we've reached the end of the file /// </summary> private bool UploadChunk(Stream fileStream) { string sha256; bool finalChunk; long bytesReadTotal = 0; var chunkRequest = CreateChunkUploadRequest(); using (var chunkRequestStream = CreateRequestStream(chunkRequest)) { // Write the header data always at full speed chunkRequestStream.Write(_headerData, 0, _headerData.Length); var bufferSize = MaxBufferSize; using (var chunkSha265 = new SHA256Managed()) { do { if (_chunkSize > 0) { // Clamp buffer size down if this last Read() of the chunk bufferSize = Math.Min(bufferSize, (int)(_chunkSize - bytesReadTotal)); } var bytesRead = fileStream.Read(Data, 0, bufferSize); if (bytesRead == 0) { chunkSha265.TransformFinalBlock(Data, 0, bytesRead); break; } bytesReadTotal += bytesRead; // Hash data chunkSha265.TransformBlock(Data, 0, bytesRead, null, 0); // Send chunk data chunkRequestStream.Write(Data, 0, bytesRead); }while (true); sha256 = bytesReadTotal > 0 ? ConvertByteArrayToHashString(chunkSha265.Hash) : ""; WriteSha256Data(chunkRequestStream, "sha256", bytesReadTotal > 0 ? chunkSha265.Hash : null); WriteSessionKey(chunkRequestStream); WriteVirtualFolderId(chunkRequestStream, _syncpointId); WriteFileDates(chunkRequestStream); finalChunk = fileStream.Position == fileStream.Length; if (finalChunk) { WriteFormValuePair(chunkRequestStream, "fileDone", string.Empty); } } WriteMultipartBodyTerminator(chunkRequestStream); } if (finalChunk) { // We've reached the end of the file, time to call our callback Console.WriteLine($"Uploading file {_localFilePath} with hash {sha256} to {chunkRequest.Address}"); Console.WriteLine("ChunkRequest Headers:"); Console.WriteLine(chunkRequest.Headers); Debug.WriteLine($"Uploading file {_localFilePath} with hash {sha256} to {chunkRequest.Address}"); Debug.WriteLine("ChunkRequest Headers:"); Debug.WriteLine(chunkRequest.Headers); chunkRequest.BeginGetResponse(_callback, CreateAsyncInfo(chunkRequest)); } else { GetResponseAndUpdateChunkUploadStateFields(chunkRequest); } return(!finalChunk); }
public (string, string) HashFile(bool useMemoryStream, bool debugOutput, int?bufferSize) { var tempFile = System.IO.Path.GetTempFileName(); Disposables.Add(() => File.Delete(tempFile)); var Output = ""; File.WriteAllText(tempFile, "Test file contents!"); var nl = Environment.NewLine; if (debugOutput) { var fileInfo = new FileInfo(tempFile); Output += $"IFileInfo.Name: {fileInfo.Name}{nl}"; Output += $"IFileInfo.Size: {fileInfo.Length}{nl}"; Output += $"IFileInfo.Type: {nl}"; Output += $"IFileInfo.LastModifiedDate: {fileInfo.LastWriteTime.ToUniversalTime().ToString(CultureInfo.InvariantCulture) ?? "(N/A)"}{nl}"; Output += $"Reading file..."; } var outputBuffer = new StringBuilder(); using (var hash = new SHA256Managed()) { if (useMemoryStream) { using (var fs = new MemoryStream(File.ReadAllBytes(tempFile))) { hash.ComputeHash(fs); } } else { using (var fs = File.OpenRead(tempFile)) { var bufferSizeToUse = bufferSize ?? 4096 * 8; if (debugOutput) { outputBuffer.AppendLine($"Using chunks of size {bufferSizeToUse}"); } var buffer = new byte[bufferSizeToUse]; int count; while ((count = fs.Read(buffer, 0, buffer.Length)) != 0) { if (debugOutput) { outputBuffer.AppendLine($"Hashing {count} bytes. {fs.Position} / {fs.Length}"); } hash.TransformBlock(buffer, 0, count, buffer, 0); } hash.TransformFinalBlock(buffer, 0, count); } } var sb = new StringBuilder(hash.HashSize / 4); foreach (var b in hash.Hash) { sb.AppendFormat("{0:x2}", b); } if (debugOutput) { Output += $"Done hashing file {Path.GetFileName(tempFile)}.{nl}"; } Output += sb.ToString(); if (outputBuffer.Length > 0) { Output += $"{nl}{nl}Debug output:{nl}"; Output += outputBuffer.ToString(); } } Output += $"{nl}--DONE"; return(tempFile, Output); }
public byte[] GenerateMAVLinkPacket20(MAVLINK_MSG_ID messageType, object indata, bool sign = false) { byte[] data; data = MavlinkUtil.StructureToByteArray(indata); MavlinkUtil.trim_payload(ref data); int extra = 0; if (sign) { extra = MAVLINK_SIGNATURE_BLOCK_LEN; } byte[] packet = new byte[data.Length + MAVLINK_NUM_NON_PAYLOAD_BYTES + extra]; packet[0] = MAVLINK_STX; packet[1] = (byte)data.Length; packet[2] = 0;//incompat signing if (sign) { packet[2] |= MAVLINK_IFLAG_SIGNED; } packet[3] = 0;//compat packet[4] = (byte)packetcount; packetcount++; packet[5] = 255; // this is always 255 - MYGCS packet[6] = (byte)MAV_COMPONENT.MAV_COMP_ID_MISSIONPLANNER; packet[7] = (byte)((UInt32)messageType); packet[8] = (byte)((UInt32)messageType >> 8); packet[9] = (byte)((UInt32)messageType >> 16); int i = MAVLINK_NUM_HEADER_BYTES; foreach (byte b in data) { packet[i] = b; i++; } ushort checksum = MavlinkCRC.crc_calculate(packet, data.Length + MAVLINK_NUM_HEADER_BYTES); checksum = MavlinkCRC.crc_accumulate(MAVLINK_MESSAGE_INFOS.GetMessageInfo((uint)messageType).crc, 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 (sign) { //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 * 100); if (timestamp == lasttimestamp) { timestamp++; } lasttimestamp = timestamp; var timebytes = BitConverter.GetBytes(timestamp); var sig = new byte[7]; // 13 includes the outgoing hash sig[0] = sendlinkid; Array.Copy(timebytes, 0, sig, 1, 6); // timestamp //Console.WriteLine("gen linkid {0}, time {1} {2} {3} {4} {5} {6} {7}", sig[0], sig[1], sig[2], sig[3], sig[4], sig[5], sig[6], timestamp); if (signingKey == null || signingKey.Length != 32) { signingKey = new byte[32]; } 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++; } } } return(packet); }
public override int Read(byte[] buffer, int offset, int count) { if (_pMode != CryptoStreamMode.Read) { throw new InvalidOperationException(); } if (_pStream == null) { throw new ObjectDisposedException(GetType().FullName); } if (_pLength >= 0L && checked (_pPosition + count) > _pLength) { count = checked ((int)(_pLength - _pPosition)); } if (count < 0) { throw new ArgumentOutOfRangeException(nameof(count)); } if (count == 0) { return(0); } int num1 = 0; label_11: int num2 = count; if (num2 > _pDecryptBuf.Length) { num2 = _pDecryptBuf.Length; } Array.Copy(_pDecryptBuf, 0, buffer, offset, num2); checked { count -= num2; offset += num2; num1 += num2; } _pPosition = checked (_pPosition + num2); byte[] numArray1 = new byte[checked (_pDecryptBuf.Length - num2 - 1 + 1)]; Array.Copy(_pDecryptBuf, num2, numArray1, 0, checked (_pDecryptBuf.Length - num2)); _pDecryptBuf = numArray1; if (count == 0) { return(num1); } int num3 = checked (count + 15) & -16; byte[] numArray2 = new byte[checked (num3 - 1 + 1)]; int num4 = 0; while (num4 < numArray2.Length) { int num5 = _pStream.Read(numArray2, num4, checked (numArray2.Length - num4)); if (num5 == 0) { byte[] inputBuffer = new byte[checked (_pReadBuf.Length + num4 - 1 + 1)]; Array.Copy(_pReadBuf, 0, inputBuffer, 0, _pReadBuf.Length); Array.Copy(numArray2, 0, inputBuffer, _pReadBuf.Length, num4); byte[] numArray3 = new byte[64]; Array.Copy(inputBuffer, checked (inputBuffer.Length - numArray3.Length), numArray3, 0, numArray3.Length); byte[] numArray4 = _pTransform.TransformFinalBlock(inputBuffer, 0, checked (num4 + 15) & -16); _pDecryptBuf = new byte[checked (num4 - 1 + 1)]; Array.Copy(numArray4, 0, _pDecryptBuf, 0, _pDecryptBuf.Length); _pSha.TransformFinalBlock(_pDecryptBuf, 0, _pDecryptBuf.Length); string s = ""; int num6 = 0; int num7 = checked (_pSha.Hash.Length - 1); int index1 = num6; while (index1 <= num7) { s += _pSha.Hash[index1].ToString("X2"); checked { ++index1; } } byte[] bytes = Encoding.UTF8.GetBytes(s); if (bytes.Length != numArray3.Length) { throw new IOException("File hash doesn't match."); } int num8 = 0; int num9 = checked (bytes.Length - 1); int index2 = num8; while (index2 <= num9) { if (bytes[index2] != numArray3[index2]) { throw new IOException("File hash doesn't match."); } checked { ++index2; } } if (_pLength < 0L) { _pLength = checked (_pPosition + num4); } else if (_pLength > checked (_pPosition + num4)) { throw new InvalidRangeException(); } if (count > num4) { count = num4; } goto label_11; } checked { num4 += num5; } } byte[] numArray5 = new byte[checked (num3 - 1 + 1)]; if (num3 < _pReadBuf.Length) { numArray5 = new byte[checked (num3 - 1 + 1)]; _pTransform.TransformBlock(_pReadBuf, 0, num3, numArray5, 0); Array.Copy(_pReadBuf, num3, _pReadBuf, 0, checked (_pReadBuf.Length - num3)); Array.Copy(numArray2, 0, _pReadBuf, checked (_pReadBuf.Length - num3), num3); } else { _pTransform.TransformBlock(_pReadBuf, 0, _pReadBuf.Length, numArray5, 0); _pTransform.TransformBlock(numArray2, 0, checked (numArray2.Length - _pReadBuf.Length), numArray5, _pReadBuf.Length); Array.Copy(numArray2, checked (numArray2.Length - _pReadBuf.Length), _pReadBuf, 0, _pReadBuf.Length); } _pSha.TransformBlock(numArray5, 0, numArray2.Length, null, 0); Array.Copy(numArray5, 0, buffer, offset, count); _pPosition = checked (_pPosition + count); int num10 = checked (num1 + count); _pDecryptBuf = new byte[checked (num3 - count - 1 + 1)]; Array.Copy(numArray5, count, _pDecryptBuf, 0, _pDecryptBuf.Length); return(num10); }
protected override void Dispose(bool disposing) { if (disposing && !Is_Disposed) { if (m_bWriting) { byte[] Key2 = null; byte[] IV2 = null; CipherInfo Cipher1 = null; CipherInfo Cipher2 = null; try { if (m_sBaseStream == null) { throw new IOException("Stream not open"); } // First encryption Cipher1 = new CipherInfo(m_Config.Algorithm1); Cipher1.SetKey(m_1Key32, m_1IV16); int BufferSize = Get64BlockAlignSize(m_WriteBytesLength); // Make sure we have a buffer that is 64 byte aligned byte[] PlainTextBuffer = new byte[BufferSize]; int Pos = 0; // Dump the collected data to this buffer foreach (byte[] Bytes in m_WriteDataBytesList) { Array.Copy(Bytes, 0, PlainTextBuffer, Pos, Bytes.Length); Array.Clear(Bytes, 0, Bytes.Length); Pos += Bytes.Length; } m_WriteDataBytesList = null; var RndGenerator = CryptoRandom.Instance; byte[] RandomDataBytes = RndGenerator.GetRandomBytes((uint)BufferSize); // XOR the plaintext with random data for (int i = 0; i < BufferSize; i++) { PlainTextBuffer[i] ^= RandomDataBytes[i]; } // Calculate hash of data part of XORred buffer var XORedTextHash32 = new SHA256Managed(); XORedTextHash32.TransformFinalBlock(PlainTextBuffer, 0, m_WriteBytesLength); // First encrypted buffer var Encrypted1Buffer = PlainTextBuffer; using (var Transformer = Cipher1.GetCipherTransformer()) Transformer.Encrypt(Encrypted1Buffer, 0, BufferSize); // 2nd Cipher initialization information Cipher2 = new CipherInfo(m_Config.Algorithm2); IV2 = RndGenerator.GetRandomBytes(Cipher2.IVSizeInBytes); Key2 = m_Config.Get2ndKey32(XORedTextHash32.Hash); Cipher2.SetKey(Key2, IV2); /////// Start writing to base stream ////////// m_sBaseStream.WriteByte(2); // File Version 2 m_sBaseStream.WriteByte(SUB_VERSION); // Sub Version m_sBaseStream.WriteByte((byte)m_Config.Algorithm1); m_sBaseStream.WriteByte((byte)m_Config.Algorithm2); m_sBaseStream.WriteByte((byte)m_Config.KeyOption); if (m_Config.KeyOption == KeyOption.Yubikey_HMAC_SHA1) // Is Yubikey option? { m_sBaseStream.WriteByte(m_Config.YubikeySlot); // Yubikey Slot m_sBaseStream.WriteByte(m_Config.YubikeyChallengeLength); m_sBaseStream.Write(m_Config.YubikeyChallenge, 0, 64); // Yubikey Challenge } m_sBaseStream.WriteByte((byte)m_Config.KeyDerivation); // Key derivation method m_sBaseStream.Write(m_Config.MasterSeed, 0, 32); m_sBaseStream.Write(m_Config.TransformSeed, 0, 32); m_sBaseStream.Write(IV2, 0, IV2.Length); m_sBaseStream.Write(Extensions.GetLittleEndianBytes(m_Config.Key2Transformations), 0, 8); m_sBaseStream.Write(Extensions.GetLittleEndianBytes(m_WriteBytesLength), 0, 4); // Write the length m_sBaseStream.Write(Encrypted1Buffer, 0, BufferSize); using (var Transformer = Cipher2.GetCipherTransformer()) Transformer.Encrypt(RandomDataBytes, 0, BufferSize); m_sBaseStream.Write(RandomDataBytes, 0, BufferSize); } finally { if (Key2 != null) { MemUtil.ZeroByteArray(Key2); } if (IV2 != null) { MemUtil.ZeroArray(IV2); } if (Cipher1 != null) { Cipher1.Dispose(); } if (Cipher2 != null) { Cipher2.Dispose(); } if (m_WriteDataBytesList != null) { foreach (byte[] Bytes in m_WriteDataBytesList) { Array.Clear(Bytes, 0, Bytes.Length); } } } } else { if (m_ReadPlainTextStream != null) { // Have to clear the underlying buffer byte[] buf = m_ReadPlainTextStream.GetBuffer(); if (buf != null) { MemUtil.ZeroByteArray(buf); } m_ReadPlainTextStream.Dispose(); } } Is_Disposed = true; } base.Dispose(disposing); }