public HashedArmorTokenParser(HashingMode hashingMode)
        {
            switch (hashingMode)
            {
            case HashingMode.HMACSHA256:
                hashSize = 32;
                break;

            case HashingMode.HMACSHA512:
                hashSize = 64;
                break;
            }
        }
Esempio n. 2
0
        public void WhenIVerifyTheHashedArmorTokenSHMACSHASignature(int p0)
        {
            switch (p0)
            {
            case 256:
                hashingMode = HashingMode.HMACSHA256;
                break;

            case 512:
                hashingMode = HashingMode.HMACSHA512;
                break;

            default:
                throw new NotImplementedException("Invalid Hashing Mode.");
            }

            var hashedArmorTokenParser = new HashedArmorTokenParser(hashingMode, Convert.FromBase64String(hashedArmorToken));

            hashedArmorTokenParser.Execute();

            byte[] hash;

            switch (p0)
            {
            case 256:
                using (var hmac = new HMACSHA256(hashingKey)) hash = hmac.ComputeHash(hashedArmorTokenParser.ParsedArmorToken.ArmorToken);
                break;

            case 512:
                using (var hmac = new HMACSHA512(hashingKey)) hash = hmac.ComputeHash(hashedArmorTokenParser.ParsedArmorToken.ArmorToken);
                break;

            default:
                throw new NotImplementedException("Invalid Hashing Mode.");
            }

            Assert.AreEqual(hashedArmorTokenParser.ParsedArmorToken.Hash, hash);
            var encryptionMechanismFactory = new RijndaelDecryptionMechanismFactory(encryptionKey, hashedArmorTokenParser.ParsedArmorToken.ArmorToken);

            var armorTokenDecryptor = new ArmorTokenEncryptor(encryptionMechanismFactory);

            armorTokenDecryptor.Execute();

            var decrypted = armorTokenDecryptor.Output;

            var armorTokenDeserialisor = new ArmorTokenDeserialisor(decrypted);

            armorTokenDeserialisor.Execute();

            deserialisedArmorToken = armorTokenDeserialisor.DeserialisedArmorToken;
        }
Esempio n. 3
0
        /// <summary>
        ///     Starts a hashcheck. If forceFullScan is false, the library will attempt to load fastresume data
        ///     before performing a full scan, otherwise fast resume data will be ignored and a full scan will be started
        /// </summary>
        /// <param name="autoStart">if set to <c>true</c> [automatic start].</param>
        public void HashCheck(bool autoStart)
        {
            ClientEngine.MainLoop.QueueWait(delegate
            {
                if (!Mode.CanHashCheck)
                {
                    throw new TorrentException(
                        string.Format("A hashcheck can only be performed when the manager is stopped. State is: {0}",
                                      State));
                }

                CheckRegisteredAndDisposed();
                this.StartTime = DateTime.Now;
                Mode           = new HashingMode(this, autoStart);
                Engine.Start();
            });
        }
        public static string Hash(string text, HashingMode mode)
        {
            var textBytes = Encoding.UTF8.GetBytes(text);

            byte[] hashBytes;
            switch (mode)
            {
            case HashingMode.SHA_1:
                var SHA1 = new SHA1CryptoServiceProvider();
                hashBytes = SHA1.ComputeHash(textBytes);
                break;

            case HashingMode.SHA_2_256:
                var SHA2_256 = new SHA256CryptoServiceProvider();
                hashBytes = SHA2_256.ComputeHash(textBytes);
                break;

            case HashingMode.SHA_2_512:
                var SHA2_512 = new SHA512CryptoServiceProvider();
                hashBytes = SHA2_512.ComputeHash(textBytes);
                break;

            //case HashingMode.SHA_3_256:
            //    var SHA3_256 = new Sha3Digest(256);
            //    SHA3_256.Update(Convert.ToByte(textBytes));
            //    SHA3_256.DoFinal(hashBytes);
            //    break;
            //case HashingMode.SHA_3_256:
            //    var SHA3_512 = new Sha3Digest(512);
            //    break;
            default:
                var SHA_default = new SHA1CryptoServiceProvider();
                hashBytes = SHA_default.ComputeHash(textBytes);
                break;
            }

            var hash = HelperFunctions.FromByteToHex(hashBytes);

            return(hash);
        }
        public static void CreateDigitalSignature_FromFile(string inputFile, string RSAprivateKeyFile, string outputFile, HashingMode mode)
        {
            var plainText = FileManager.ReadFile_String(inputFile);

            var hash = SHA.Hash(plainText, mode);

            var privateKey = FileManager.Read_RSAKey(RSAprivateKeyFile);
            var signature  = RSA.Encrypt(hash, privateKey.Modulus, privateKey.Exponent);

            var signatureBytes = Convert.FromBase64String(signature);

            signature = HelperFunctions.FromByteToHex(signatureBytes);

            FileManager.Write_Signature(outputFile, signature, privateKey.Modulus.Length * 4, mode);
        }
        public static void CheckDigitalSignature_FromString(string text, string signatureFile, string RSApublicKeyFile, TextBox outputTextBox, HashingMode mode)
        {
            var signature = FileManager.Read_Signature(signatureFile);

            var publicKey      = FileManager.Read_RSAKey(RSApublicKeyFile);
            var signatureBytes = HelperFunctions.FromHexToByte(signature);

            var decoded = RSA.Decrypt(Convert.ToBase64String(signatureBytes), publicKey.Modulus, publicKey.Exponent);
            var hash    = SHA.Hash(text, mode);

            outputTextBox.Text = hash == decoded ? "Potpis je valjan!" : "Potpis nije valjan!";
        }
 public HashedArmorTokenParser(HashingMode hashingMode, byte[] hashedArmorToken) : this(hashingMode) {
     HashedArmorToken = hashedArmorToken;
 }
Esempio n. 8
0
        public static void CreateDigitalSeal(string inputFile, string RSApublicReciever, string RSAprivateSender, string envelopeFile, string signatureFile, EncryptionMode encryptionMode, HashingMode hashingMode, SymetricAlgorithm algorithm, KeySize keySize)
        {
            DigitalEnvelope.CreateDigitalEnvelope(inputFile, RSApublicReciever, envelopeFile, encryptionMode, algorithm, keySize);

            var envelope = FileManager.Read_Envelope(envelopeFile);

            var hash = SHA.Hash(envelope.Data + envelope.Key, hashingMode);

            DigitalSignature.CreateDigitalSignature_FromString(hash, RSAprivateSender, signatureFile, hashingMode);
        }
Esempio n. 9
0
        public static void CheckDigitalSeal(string outputFile, string RSApublicSender, string RSAprivateReciever, string envelopeFile, string signatureFile, TextBox sealCheck, EncryptionMode encryptionMode, HashingMode hashingMode, SymetricAlgorithm algorithm)
        {
            DigitalEnvelope.OpenDigitalEnvelope(envelopeFile, RSAprivateReciever, outputFile, encryptionMode, algorithm);

            var envelope = FileManager.Read_Envelope(envelopeFile);

            var hash = SHA.Hash(envelope.Data + envelope.Key, hashingMode);

            DigitalSignature.CheckDigitalSignature_FromString(hash, signatureFile, RSApublicSender, sealCheck, hashingMode);
        }
Esempio n. 10
0
        public static void Write_Signature(string file, string signature, int RSAKeyLength, HashingMode mode)
        {
            var streamWriter = new StreamWriter(Program.Direktorij + file);

            streamWriter.WriteLine("---BEGIN OS 2 CRYPTO DATA---");
            streamWriter.WriteLine();
            streamWriter.WriteLine("Description");
            streamWriter.WriteLine("    Singature");
            streamWriter.WriteLine();
            streamWriter.WriteLine("File name:");

            var breadCrumbs = file.Split('\\');

            streamWriter.WriteLine("    " + breadCrumbs[breadCrumbs.Length - 1]);
            streamWriter.WriteLine();

            streamWriter.WriteLine("Method:");
            int hashLenght;

            if (mode == HashingMode.SHA_1)
            {
                streamWriter.WriteLine("    SHA-1");
                hashLenght = 160;
            }
            else if (mode == HashingMode.SHA_2_256)
            {
                streamWriter.WriteLine("    SHA-2 (256)");
                hashLenght = 256;
            }
            else
            {
                streamWriter.WriteLine("    SHA-2 (512)");
                hashLenght = 512;
            }
            streamWriter.WriteLine("    RSA");
            streamWriter.WriteLine();
            streamWriter.WriteLine("Key length:");
            streamWriter.WriteLine("    " + HelperFunctions.FromIntToHex(hashLenght));
            streamWriter.WriteLine("    " + HelperFunctions.FromIntToHex(RSAKeyLength));
            streamWriter.WriteLine();
            streamWriter.WriteLine("Signature:");

            var NumLines = (double)signature.Length / 60;

            if (Math.Truncate(NumLines) < NumLines)
            {
                NumLines++;
            }

            for (var i = 0; i < Math.Truncate(NumLines); i++)
            {
                if (signature.Length - i * 60 < 60)
                {
                    streamWriter.WriteLine("    " + signature.Substring(i * 60, signature.Length - i * 60));
                }
                else
                {
                    streamWriter.WriteLine("    " + signature.Substring(i * 60, 60));
                }
            }

            streamWriter.WriteLine();
            streamWriter.WriteLine("---END OS2 CRYPTO DATA---");
            streamWriter.Close();
        }