Пример #1
0
        public static async Task Run([PerperStreamTrigger] PerperStreamContext context,
                                     [Perper("ipfsGateway")] string ipfsGateway,
                                     [PerperStream("dataStream")] IAsyncEnumerable <object> dataStream,
                                     [PerperStream("outputStream")] IAsyncCollector <IHashed <object> > outputStream,
                                     ILogger logger)
        {
            var ipfs = new IpfsClient(ipfsGateway);

            await dataStream.ForEachAsync(async item =>
            {
                try
                {
                    var jToken = IpfsJsonSettings.JTokenFromObject(item);
                    var cid    = await ipfs.Dag.PutAsync(jToken, cancel: CancellationToken.None);
                    var hash   = new Hash {
                        Bytes = cid.ToArray()
                    };

                    logger.LogDebug("Saved {json} as {hash} in ipfs", jToken.ToString(Formatting.None), hash);

                    await outputStream.AddAsync(Hashed.Create(item, hash));
                }
                catch (Exception e)
                {
                    logger.LogError(e.ToString());
                }
            }, CancellationToken.None);
        }
Пример #2
0
        public static async Task Run([PerperStreamTrigger] PerperStreamContext context,
                                     [Perper("ipfsGateway")] string ipfsGateway,
                                     [PerperStream("hashStream")] IAsyncEnumerable <Hash> hashStream,
                                     [PerperStream("outputStream")] IAsyncCollector <IHashed <object> > outputStream,
                                     ILogger logger)
        {
            var ipfs = new IpfsClient(ipfsGateway);

            await hashStream.ForEachAsync(async hash =>
            {
                try
                {
                    // NOTE: Currently blocks other items on the stream and does not process them
                    // -- we should at least timeout

                    var jToken = await ipfs.Dag.GetAsync(Cid.Read(hash.Bytes), CancellationToken.None);
                    var item   = jToken.ToObject <object>(JsonSerializer.Create(IpfsJsonSettings.DefaultSettings));

                    await outputStream.AddAsync(Hashed.Create(item, hash));
                }
                catch (Exception e)
                {
                    logger.LogError(e.ToString());
                }
            }, CancellationToken.None);
        }
Пример #3
0
        public static string Decrypt(string xmlPivateKey, string xmlPublicKey, EncryptedAsymmetric input)
        {
            EncryptedSymmetric symmetric = new EncryptedSymmetric()
            {
                Name = input.SymmetricName,
                IV   = new byte[input.SymmetricIVLength],
                Key  = new byte[input.SymmetricKeyLength]
            };
            Hashed hashed = new Hashed()
            {
                Name = input.HashName,
                Data = new byte[input.HashLength]
            };

            byte[] asymmetricDataDecrypted;
            using (var cryptAlgorithm = CryptHandler.CreateAsymmetric(xmlPivateKey))
            {
                asymmetricDataDecrypted = cryptAlgorithm.Decrypt(input.AsymmetricData, RSAEncryptionPadding.OaepSHA256);
            }

            int sourceStartPosition = 0;

            Array.Copy(asymmetricDataDecrypted, sourceStartPosition, symmetric.IV, 0, input.SymmetricIVLength);
            sourceStartPosition += input.SymmetricIVLength;
            Array.Copy(asymmetricDataDecrypted, sourceStartPosition, symmetric.Key, 0, input.SymmetricKeyLength);
            sourceStartPosition += input.SymmetricKeyLength;
            Array.Copy(asymmetricDataDecrypted, sourceStartPosition, hashed.Data, 0, input.HashLength);

            using (var cryptAlgorithm = CryptHandler.CreateAsymmetric(xmlPublicKey))
            {
                //byte[] dataToVerify = new byte[input.SymmetricIVLength + input.SymmetricKeyLength + input.HashLength];
                //Array.Copy(asymmetricDataDecrypted, 0, dataToVerify, 0, dataToVerify.Length);

                bool isDataVerified = cryptAlgorithm.VerifyData(asymmetricDataDecrypted, input.AsymmetricSignature, new HashAlgorithmName(input.HashName), padding);
                if (!isDataVerified)
                {
                    throw new Exception("Crypt data can't be verified.");
                }

                //bool isHashVerified = cryptAlgorithm.VerifyHash(hashed.Data, input.AsymmetricSignature, new HashAlgorithmName(input.HashName), padding);
                //if (!isHashVerified)
                //{
                //    throw new Exception("Crypt hash can't be verified.");
                //}
            }

            var symmetricDataDecrypted = DecryptSymmetric(input.SymmetricData, symmetric.IV, symmetric.Key);
            var hashedToCompare        = Hash(CryptHandler.Encoding.GetBytes(symmetricDataDecrypted));
            var isHashVerified         = hashedToCompare.Data.SequenceEqual(hashed.Data);

            if (!isHashVerified)
            {
                throw new Exception("Hash can't be verified.");
            }
            return(symmetricDataDecrypted);
        }
Пример #4
0
        byte [] CalcPiecesHash(List <TorrentFile> files, IPieceWriter writer, CancellationToken token)
        {
            int  bufferRead  = 0;
            long fileRead    = 0;
            long overallRead = 0;
            MD5  md5Hasher   = null;

            var shaHasher     = HashAlgoFactory.Create <SHA1> ();
            var torrentHashes = new List <byte> ();
            var overallTotal  = files.Sum(t => t.Length);
            var buffer        = new byte [PieceLength];

            if (StoreMD5)
            {
                md5Hasher = HashAlgoFactory.Create <MD5> ();
            }

            try {
                foreach (TorrentFile file in files)
                {
                    fileRead = 0;
                    md5Hasher?.Initialize();

                    while (fileRead < file.Length)
                    {
                        int toRead = (int)Math.Min(buffer.Length - bufferRead, file.Length - fileRead);
                        int read   = writer.Read(file, fileRead, buffer, bufferRead, toRead);
                        token.ThrowIfCancellationRequested();

                        md5Hasher?.TransformBlock(buffer, bufferRead, read, buffer, bufferRead);
                        shaHasher.TransformBlock(buffer, bufferRead, read, buffer, bufferRead);

                        bufferRead  += read;
                        fileRead    += read;
                        overallRead += read;

                        if (bufferRead == buffer.Length)
                        {
                            bufferRead = 0;
                            shaHasher.TransformFinalBlock(buffer, 0, 0);
                            torrentHashes.AddRange(shaHasher.Hash);
                            shaHasher.Initialize();
                        }
                        Hashed?.InvokeAsync(this, new TorrentCreatorEventArgs(file.Path, fileRead, file.Length, overallRead, overallTotal));
                    }

                    md5Hasher?.TransformFinalBlock(buffer, 0, 0);
                    md5Hasher?.Initialize();
                    file.MD5 = md5Hasher?.Hash;
                }
                if (bufferRead > 0)
                {
                    shaHasher.TransformFinalBlock(buffer, 0, 0);
                    torrentHashes.AddRange(shaHasher.Hash);
                }
            } finally {
                shaHasher.Dispose();
                md5Hasher?.Dispose();
            }
            return(torrentHashes.ToArray());
        }