Ejemplo n.º 1
0
 public void Sha1Bytes_ReturnsCorrectHash()
 {
     byte[] bytes = new Sha1Hasher().Sha1Bytes("hello world");
     string hex = BitConverter.ToString(bytes);
     string actual = hex.Replace("-", "");
     Assert.AreEqual("2AAE6C35C94FCFB415DBE95F408B9CE91EE846ED", actual);
 }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            Sha1Hasher hashObj = new Sha1Hasher();

            using (var client = new SshClient("shell1.sigmatic.fi", "shakkili", "2600_Chess_GM"))
            {
                string connectionStr = "server=mysql2.sigmatic.fi;user=shakkili_master;database=shakkili_avainsiirto;port=22;password=abckukko";
                client.Connect();
                if (client.IsConnected)
                {
                    Console.WriteLine("Connected to SSH Tunnel");
                    var portForwarded = new ForwardedPortLocal("127.0.0.1", 3306, "127.0.0.1", 22);
                    client.AddForwardedPort(portForwarded);
                    portForwarded.Start();
                    MySqlConnection conn = new MySqlConnection(connectionStr);

                    try
                    {
                        Console.WriteLine("Connecting to mySQL DB");
                        conn.Open();
                        Console.WriteLine("Managed to connect to DB");
                    }
                    catch (Exception exc)
                    {
                        Console.WriteLine(exc.ToString());
                    }

                    ConsoleKeyInfo a = Console.ReadKey();
                    conn.Close();
                }
                Console.WriteLine("Connection to SSH Tunnel failed");
            }
        }
Ejemplo n.º 3
0
        public void RsaTest()
        {
            var text = "Hello NStandard.";
            var data = text.Bytes();

            var encryptRsa = new RsaProvider();

            encryptRsa.FromXmlString(@"<RSAKeyValue>
    <Modulus>wyL0Tl0oPhililRHagPFIlKjWotnDKeK84p9sMjCPUfQqTb9zTne/K7KdpNnHSE5zmjUNWPGFSFxkdec/XVrLz0ZD0euTJo8ZN7Wx1eVYGczh5seLIOazf6zMgEW5jqwVx69Zs0K39alJDPqOYF7Q/2bBD9/BWvhRymkfKlSI5Z9LNi0i5ZRzuXqz6RcvPR35O/FdzWypKQsuRU73szDEwStkwu1ZgsSdyV50RoMAWodnvPDhJnVNdLtpeE6d2EDXKSnmmh+64h4HAK753UlCgjPuJ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91K/KOpu24TF0MTHrXro9vh/Ry0+TVY67twQ7GmO6McgdXYtieZihdSky7xsRp8BB+L/y9G/TvXzjUdoCPNC6VJ5n/fWaxgK/T7xujQVKWa0MEY0BtPma0tqWb1t0UxGseuG7Kb2JyYZDKc3sCr4ILkvpL9+nXkG8jS8kZcYyNEzEKhwOTU4SyMcHHq0tyuE9Iq6KEnHDcAekaXRvZnG/Sw1SDH7Yg44NOtdfZxhUxIOP8Gj1yFiDy/QJUsxAW7V60+2UwFb6DnEGuQ==</D>
</RSAKeyValue>");
            var cipher        = encryptRsa.Encrypt(data);
            var signature     = encryptRsa.SignData(data, HashAlgorithmName.SHA1);
            var hash          = Sha1Hasher.Hash(data);
            var hashSignature = encryptRsa.SignHash(hash, HashAlgorithmName.SHA1);

            var decryptRsa = new RsaProvider();

            decryptRsa.FromPemString(@"-----BEGIN PRIVATE KEY-----
MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQDDIvROXSg+GKWK
VEdqA8UiUqNai2cMp4rzin2wyMI9R9CpNv3NOd78rsp2k2cdITnOaNQ1Y8YVIXGR
15z9dWsvPRkPR65Mmjxk3tbHV5VgZzOHmx4sg5rN/rMyARbmOrBXHr1mzQrf1qUk
M+o5gXtD/ZsEP38Fa+FHKaR8qVIjln0s2LSLllHO5erPpFy89Hfk78V3NbKkpCy5
FTvezMMTBK2TC7VmCxJ3JXnRGgwBah2e88OEmdU10u2l4Tp3YQNcpKeaaH7riHgc
ArvndSUKCM+4n+MHFKK95I7rI6nXwezX+0sd9luWDPwEze3Mw+Cs9TfE0TcIde6N
hJUqLjOxAgMBAAECggEBAIWsBitASXbwL0YpIZqP5pTygxBdekQC6X6Xmi5l+Bk+
fsPL9eSGBiiWG/9KTlJ4Fj89NFE2fdSvyjqbtuExdDEx6166Pb4f0ctPk1WOu7cE
OxpjujHIHV2LYnmYoXUpMu8bEafAQfi/8vRv071841HaAjzQulSeZ/31msYCv0+8
bo0FSlmtDBGNAbT5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her = decryptRsa.Decrypt(cipher).String();

            Assert.Equal(text, decipher);
            Assert.True(encryptRsa.VerifyData(data, signature, HashAlgorithmName.SHA1));
            Assert.True(encryptRsa.VerifyHash(hash, hashSignature, HashAlgorithmName.SHA1));
            Assert.True(encryptRsa.ToXmlString(true) == decryptRsa.ToXmlString(true));
            Assert.True(encryptRsa.ToPemString(true) == decryptRsa.ToPemString(true));
        }
Ejemplo n.º 4
0
        public ActionResult RegisterUser(RegisterUserViewModel model)
        {
            var isModelValid = ModelState.IsValid;

            if (!isModelValid)
            {
                return(View("Register", model));
            }
            if (isModelValid)
            {
                if (model.Password != model.ConfirmPassword)
                {
                    ModelState.AddModelError("Fehler", "Passwort muss gleich Passwortbestätigung sein!");
                }
                var pwdHash = Sha1Hasher.Hash(model.Password.ToString());


                var part = new Participant
                {
                    Email     = model.Email,
                    Firstname = model.Firstname,
                    Lastname  = model.Username,
                    Company   = model.Company,
                    Website   = model.Website,
                    Password  = pwdHash
                };

                this._repository.Insert(part);
            }

            return(RedirectToAction("Login"));
        }
Ejemplo n.º 5
0
        public void ShouldReturnSameHashWhenGivenSameSourceAndSalt()
        {
            var hasher = new Sha1Hasher();
            var hash   = hasher.Generate("TEST", "SALT");

            Assert.AreEqual("TEgfK06VHp44cC+mq+SkeTvg9Cg=", hash);
        }
Ejemplo n.º 6
0
        public void Sha1Bytes_ReturnsCorrectHash()
        {
            byte[] bytes  = new Sha1Hasher().Sha1Bytes("hello world");
            String hex    = BitConverter.ToString(bytes);
            String actual = hex.Replace("-", "");

            Assert.AreEqual("2AAE6C35C94FCFB415DBE95F408B9CE91EE846ED", actual);
        }
Ejemplo n.º 7
0
        /// <summary>
        /// The entry point of the program, where the program control starts and ends.
        /// </summary>
        /// <param name="args">The command-line arguments.</param>
        static void Main(string[] args)
        {
            String path;
            Console.Write("File to create torrent for <absolute path>: ");
            path = Console.ReadLine();
            while (!(File.Exists(path)))
            {
                Console.WriteLine("File does not exist, try again.");
                Console.Write("File to create torrent for <absolute path>: ");
                path = Console.ReadLine();
            }

            Sha1Hasher sha1Hasher = new Sha1Hasher();
            FileReader fileReader = new FileReader(path, sha1Hasher.inputChannel, PIECE_SIZE);

            string torrentPath = path + ".torrent";
            FileStream torrentFile = new FileStream(torrentPath, FileMode.Create);
            int pieces = (int)Math.Ceiling((double)fileReader.fileBytes.Length / (double)PIECE_SIZE);
            TorrentFileWriter torrentFileWriter = new TorrentFileWriter(torrentFile, pieces, sha1Hasher.outputChannel);

            torrentFileWriter.Start();

            // Create the header of the torrent file
            StringBuilder torrentHeader = new StringBuilder();
            torrentHeader.Append("d8:announce44:udp://tracker.openbittorrent.com:80/announce");
            torrentHeader.Append("8:encoding5:UTF-8");
            torrentHeader.Append("4:info");
            torrentHeader.Append("d6:lengthi" + fileReader.fileBytes.Length + "e");
            string filename = path.Substring(path.LastIndexOf('/') + 1);
            torrentHeader.Append("4:name" + filename.Length + ":" + filename);
            torrentHeader.Append("12:piece lengthi" + PIECE_SIZE + "e");
            torrentHeader.Append("6:pieces" + pieces * 20 + ":");
            torrentFileWriter.inputChannel.Put(Encoding.UTF8.GetBytes(torrentHeader.ToString()));

            sha1Hasher.Start();

            fileReader.Start();

            // Wait until initial writing has completed
            while (!torrentFileWriter.WritingComplete()){}

            // Append the bencoding ends
            string end = "ee";
            torrentFileWriter.inputChannel.Put(Encoding.UTF8.GetBytes(end));

            // Wait until writing has completed again
            while (!torrentFileWriter.WritingComplete()){}

            // Stop the file writer
            torrentFileWriter.Stop();

            // Close the torrent file
            torrentFile.Close();

            Console.WriteLine("Torrent file created: \"" + torrentPath + "\"");
        }
        public HashedPassword(string clearPassword)
        {
            if (string.IsNullOrEmpty(clearPassword))
            {
                throw new ArgumentException("Password is null or empty.");
            }

            ClearPassword = clearPassword;
            Hash          = Sha1Hasher.Hash(clearPassword);
        }
        public override bool ValidateUser(string username, string password)
        {
            var repo        = new Repository <Participant>();
            var participant = repo.Query.SingleOrDefault(p => p.Email == username);

            if (participant == null)
            {
                return(false);
            }

            var hasher         = new Sha1Hasher();
            var hashedPassword = hasher.ComputeHash(password);

            var isPasswordValid = hashedPassword == participant.Password;

            return(isPasswordValid);
        }
Ejemplo n.º 10
0
 public void HmacHash_ReturnsCorrectHash()
 {
     string actual = new Sha1Hasher().HmacHash("secretKey", "hello world");
     Assert.AreEqual("D503D7A1A6ADBA1E6474E9FF2C4167F9DFDF4247", actual);
 }
Ejemplo n.º 11
0
 public AccountController()
 {
     _repository = new Repository <Participant>();
     _sha1Hasher = new Sha1Hasher();
 }
Ejemplo n.º 12
0
        public void HmacHash_ReturnsCorrectHash()
        {
            String actual = new Sha1Hasher().HmacHash("secretKey", "hello world");

            Assert.AreEqual("D503D7A1A6ADBA1E6474E9FF2C4167F9DFDF4247", actual);
        }
 protected override string Transform(string text)
 {
     return(Sha1Hasher.Hash(text));
 }
Ejemplo n.º 14
0
 public static byte[] SHA1(byte[] bytes) => Sha1Hasher.Hash(bytes);
Ejemplo n.º 15
0
            public virtual void CalcHashes(Hashes hashFlags, Validations validationFlags)
            {
                if (IssueModel.Data.HasFatal)
                {
                    return;
                }

                bool hitCache = Data.fBuf != null && Data.FileSize < Int32.MaxValue;

                if ((hashFlags & Hashes.FileMD5) != 0 && Data.fileMD5 == null)
                {
                    var hasher = new Md5Hasher();
                    if (hitCache)
                    {
                        hasher.Append(Data.fBuf, 0, Data.fBuf.Length);
                    }
                    else
                    {
                        hasher.Append(Data.fbs);
                    }
                    Data.fileMD5 = hasher.GetHashAndReset();
                }

                if ((hashFlags & Hashes.FileSHA1) != 0 && Data.fileSHA1 == null)
                {
                    var hasher = new Sha1Hasher();
                    if (hitCache)
                    {
                        hasher.Append(Data.fBuf, 0, Data.fBuf.Length);
                    }
                    else
                    {
                        hasher.Append(Data.fbs);
                    }
                    Data.fileSHA1 = hasher.GetHashAndReset();
                }

                if ((hashFlags & Hashes.FileSHA256) != 0 && Data.fileSHA256 == null)
                {
                    var hasher = new Sha256Hasher();
                    if (hitCache)
                    {
                        hasher.Append(Data.fBuf, 0, Data.fBuf.Length);
                    }
                    else
                    {
                        hasher.Append(Data.fbs);
                    }
                    Data.fileSHA256 = hasher.GetHashAndReset();
                }

                if ((hashFlags & Hashes.MediaSHA1) != 0 && Data.mediaSHA1 == null)
                {
                    if (Data.MediaCount == Data.FileSize && Data.fileSHA1 != null)
                    {
                        System.Diagnostics.Debug.Assert(Data.mediaPosition == 0);
                        Data.mediaSHA1 = Data.fileSHA1;
                    }
                    else
                    {
                        var hasher = new Sha1Hasher();
                        if (hitCache)
                        {
                            hasher.Append(Data.fBuf, (int)Data.mediaPosition, (int)Data.MediaCount);
                        }
                        else
                        {
                            hasher.Append(Data.fbs, Data.mediaPosition, Data.MediaCount);
                        }
                        Data.mediaSHA1 = hasher.GetHashAndReset();
                    }
                }

                if ((hashFlags & Hashes.MetaSHA1) != 0 && Data.metaSHA1 == null)
                {
                    var hasher    = new Sha1Hasher();
                    var suffixPos = Data.mediaPosition + Data.MediaCount;

                    if (hitCache)
                    {
                        hasher.Append(Data.fBuf, 0, (int)Data.mediaPosition, (int)suffixPos, (int)(Data.FileSize - suffixPos));
                    }
                    else
                    {
                        hasher.Append(Data.fbs, 0, Data.mediaPosition, suffixPos, Data.FileSize - suffixPos);
                    }

                    Data.metaSHA1 = hasher.GetHashAndReset();
                }
            }
Ejemplo n.º 16
0
        public void ShouldReturnSaltWithSpecifiedLength()
        {
            var hasher = new Sha1Hasher();

            Assert.AreEqual(8, hasher.GetSalt(8).Length);
        }