Пример #1
0
        public async Task <bool> Authenticate(string email, string password)
        {
            var hashedPassword = _hashProvider.Hash(password);

            var entity =
                await
                _context.Users.FirstOrDefaultAsync(u => u.Email.Equals(email) && u.Password.Equals(hashedPassword));

            return(entity != null);
        }
Пример #2
0
        public static FileItem Create(FileInfo file, IHashProvider hasher)
        {
            var item = new FileItem
            {
                Source         = FileService.Local,
                IsFolder       = false,
                IsDeleted      = true,
                Name           = file.Name,
                Path           = file.FullName.Replace("\\", "/"),
                Id             = string.Empty,
                ServerRev      = string.Empty,
                Hash           = null,
                Size           = 0,
                LastModified   = file.LastWriteTimeUtc,
                ClientModified = file.LastWriteTimeUtc,
                Object         = file
            };

            if (file.Exists)
            {
                item.IsDeleted = false;
                item.Hash      = hasher.Hash(file);
                item.Size      = (ulong)file.Length;
            }

            return(item);
        }
Пример #3
0
        /// <summary>
        /// Verifica lo stato di un file su disco.
        /// </summary>
        /// <param name="filename">Percorso del file da verificare</param>
        /// <param name="hash">Hash atteso per questo file</param>
        /// <param name="provider">Specifica l'algoritmo di hashing da utilizzare</param>
        /// <param name="usePersistentFile">Legge, crea o aggiorna un file di testo con l'hash calcolato per il file.
        /// Può rendere piu veloci i controlli ripetuti.</param>
        /// <returns></returns>
        public static FileStatus VerifyHash(string filename, string hash, IHashProvider provider, bool usePersistentFile = false)
        {
            try
            {
                if (!File.Exists(filename))
                {
                    return(FileStatus.Missing);
                }

                string calucatedHash;

                if (usePersistentFile)
                {
                    var hashfile = GetHashFileName(filename, provider);
                    // utilizza un file temporaneo con l'hash calcolato per velocizzare i controlli successivi
                    if (File.Exists(hashfile) && File.GetLastWriteTime(hashfile) >= File.GetLastWriteTime(filename))
                    {
                        calucatedHash = File.ReadAllText(hashfile);
                    }
                    else
                    {
                        calucatedHash = provider.Hash(new FileInfo(filename)).ToString(default(BinaryRepresentation));
                        if (File.Exists(hashfile))
                        {
                            // If File is hidden File.WriteAllText throws UnauthorizedAccessException
                            File.SetAttributes(hashfile, FileAttributes.Normal);
                        }
                        File.WriteAllText(hashfile, calucatedHash);
                        File.SetAttributes(hashfile, FileAttributes.Hidden | FileAttributes.NotContentIndexed);
                    }
                }
                else
                {
                    calucatedHash = provider.Hash(new FileInfo(filename)).ToString(default(BinaryRepresentation));
                }

                return(calucatedHash.Equals(hash)
                    ? FileStatus.Present
                    : FileStatus.Corrupted);
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.ToString());
                return(FileStatus.Unknown);
            }
        }
        /// <summary>
        ///     Starting with the innermost nodes, replace all lookups with identities
        /// </summary>
        /// <param name="xElement"></param>
        private void ReplaceReferenceLookupsWithIdentities(XElement xElement)
        {
            XElement newIdentity = null;

            var ns = xElement.Name.Namespace;

            if (_regex.IsMatch(xElement.Name.LocalName))
            {
                var typeName = _regex.Match(xElement.Name.LocalName).Groups["TypeName"].Value;
                var lookup   = xElement.Element(ns + $"{typeName}Lookup");
                var identity = xElement.Element(ns + $"{typeName}Identity");

                if (lookup != null && identity == null)
                {
                    var hashBytes = _hashProvider.Hash(lookup.ToString());
                    var hash      = _hashProvider.BytesToStr(hashBytes);

                    if (_hashIdentities.ContainsKey(hash) && _hashIdentities[hash] != null)
                    {
                        newIdentity = XElement.Parse(_hashIdentities[hash].ToString());

                        lookup.AddBeforeSelf(
                            new XComment(
                                $"This {typeName}Lookup entity was used to populate its {typeName}Identity sibling"));
                    }
                    else
                    {
                        lookup.AddBeforeSelf(
                            new XComment(
                                $"No {typeName}Identity could be retrieved for this {typeName}Lookup entity"));
                    }
                }

                foreach (var element in xElement.Elements())
                {
                    ReplaceReferenceLookupsWithIdentities(element);
                }

                if (newIdentity == null)
                {
                    return;
                }

                xElement.Add(newIdentity);

                newIdentity.AddBeforeSelf(
                    new XComment(
                        $"This {typeName}Identity entity was retrieved from the API using the provided {typeName}Lookup information"));
            }
            else
            {
                foreach (var element in xElement.Elements())
                {
                    ReplaceReferenceLookupsWithIdentities(element);
                }
            }
        }
Пример #5
0
        private void SetPassword(string password)
        {
            if (string.IsNullOrWhiteSpace(password))
            {
                throw new PasswordRequiredException();
            }
            if (password.Length < 6)
            {
                throw new PasswordLengthLessThanSixException();
            }

            this.Password = hashProvider.Hash(password, Id.ToString());
        }
Пример #6
0
        public static void Test(Dictionary <string, string> testVectors,
                                IHashProvider hashFunction)
        {
            foreach (var(input, expectedOutput) in testVectors.Tuples())
            {
                byte[] inputArray = Encoding.StringToByteArray(input);

                string actualOutput = Encoding.ByteArrayToHexString(
                    hashFunction.Hash(inputArray)
                    );

                Assert.AreEqual <string>(expectedOutput, actualOutput);
            }
        }
Пример #7
0
        public byte[] Hash(byte[] input, HashProviders provider = HashProviders.Default)
        {
            IHashProvider p = null;

            if (provider == HashProviders.UserAccountSecurity)
            {
                p = _resolver.Resolve <IHashProvider>("MURMUR3");
            }
            else
            {
                p = _resolver.Resolve <IHashProvider>();
            }

            return(p.Hash(input));
        }
Пример #8
0
        public bool IsValidPassword(string password, IHashProvider hashProvider)
        {
            var hash = hashProvider.Hash(Encoding.UTF8.GetBytes(password), Salt);

            if (PasswordHash.Length != hash.Length)
            {
                return(false);
            }

            for (int i = 0; i < PasswordHash.Length; i++)
            {
                if (PasswordHash[i] != hash[i])
                {
                    return(false);
                }
            }

            return(true);
        }