Esempio n. 1
0
 public TokenManager()
 {
     sha1           = HashAlgoFactory.SHA1();
     random         = new RNGCryptoServiceProvider();
     currentSecret  = new byte[10];
     previousSecret = new byte[10];
     random.GetNonZeroBytes(currentSecret);
     random.GetNonZeroBytes(previousSecret);
 }
Esempio n. 2
0
        protected EncryptedSocket(IList <EncryptionType> allowedEncryption)
        {
            random = RandomNumberGenerator.Create();
            hasher = HashAlgoFactory.SHA1();

            X = new byte[20];
            random.GetBytes(X);
            Y = ModuloCalculator.Calculate(ModuloCalculator.TWO, X);

            SetMinCryptoAllowed(allowedEncryption);
        }
Esempio n. 3
0
        internal Torrent GetTorrent()
        {
            byte[] calculatedInfoHash;
            using (SHA1 sha = HashAlgoFactory.SHA1())
                calculatedInfoHash = sha.ComputeHash(Stream.ToArray());
            if (!Manager.InfoHash.Equals(calculatedInfoHash))
            {
                throw new Exception("invalid metadata"); //restart ?
            }
            var d    = BEncodedValue.Decode(Stream);
            var dict = new BEncodedDictionary {
                { "info", d }
            };

            return(Torrent.LoadCore(dict));
        }
Esempio n. 4
0
 public IncrementalHashData()
 {
     Hasher = HashAlgoFactory.SHA1();
     Initialise();
 }
Esempio n. 5
0
        protected override void HandleLtMetadataMessage(PeerId id, LTMetadata message)
        {
            base.HandleLtMetadataMessage(id, message);

            switch (message.MetadataMessageType)
            {
            case LTMetadata.eMessageType.Data:
                // If we've already received everything successfully, do nothing!
                if (bitField.AllTrue)
                {
                    return;
                }

                if (Stream == null)
                {
                    throw new Exception("Need extention handshake before ut_metadata message.");
                }

                Stream.Seek(message.Piece * LTMetadata.BlockSize, SeekOrigin.Begin);
                Stream.Write(message.MetadataPiece, 0, message.MetadataPiece.Length);
                bitField[message.Piece] = true;
                if (bitField.AllTrue)
                {
                    byte[] hash;
                    Stream.Position = 0;
                    using (SHA1 hasher = HashAlgoFactory.SHA1())
                        hash = hasher.ComputeHash(Stream);

                    if (!Manager.InfoHash.Equals(hash))
                    {
                        bitField.SetAll(false);
                    }
                    else
                    {
                        Stream.Position = 0;
                        BEncodedDictionary dict = new BEncodedDictionary();
                        dict.Add("info", BEncodedValue.Decode(Stream));

                        if (Manager.TrackerManager.Tiers != null && Manager.TrackerManager.Tiers.Count > 0)
                        {
                            BEncodedList announceTrackers = new BEncodedList();
                            foreach (var tier in Manager.TrackerManager.Tiers)
                            {
                                BEncodedList announceUrls = new BEncodedList();

                                foreach (var tracker in tier.Trackers)
                                {
                                    announceUrls.Add(new BEncodedString(tracker.Uri.OriginalString));
                                }

                                announceTrackers.Add(announceUrls);
                            }

                            dict.Add("announce-list", announceTrackers);
                        }
                        var rawData = dict.Encode();
                        if (Torrent.TryLoad(rawData, out Torrent t))
                        {
                            if (stopWhenDone)
                            {
                                Manager.RaiseMetadataReceived(rawData);
                                return;
                            }

                            try {
                                if (this.Settings.AutoSaveLoadMagnetLinkMetadata)
                                {
                                    if (!Directory.Exists(Path.GetDirectoryName(savePath)))
                                    {
                                        Directory.CreateDirectory(Path.GetDirectoryName(savePath));
                                    }
                                    File.Delete(savePath);
                                    File.WriteAllBytes(savePath, dict.Encode());
                                }
                            } catch (Exception ex) {
                                logger.ExceptionFormated(ex, "Cannot write metadata to path '{0}'", savePath);
                                Manager.TrySetError(Reason.WriteFailure, ex);
                                return;
                            }
                            Manager.SetMetadata(t);
                            _ = Manager.StartAsync();
                            Manager.RaiseMetadataReceived(rawData);
                        }
                        else
                        {
                            bitField.SetAll(false);
                        }
                    }
                }
                RequestNextNeededPiece(id);
                break;

            case LTMetadata.eMessageType.Reject:
                //TODO
                //Think to what we do in this situation
                //for moment nothing ;)
                //reject or flood?
                break;

            case LTMetadata.eMessageType.Request:    //ever done in base class but needed to avoid default
                break;

            default:
                throw new MessageException($"Invalid messagetype in LTMetadata: {message.MetadataMessageType}");
            }
        }
Esempio n. 6
0
 public IncrementalHashData()
 {
     Hasher = HashAlgoFactory.SHA1();
     Locker = new ReusableExclusiveSemaphore();
     Initialise();
 }