public static void ParseSalt(string salt, out byte[] saltBytes, out ulong N, out uint r, out uint p, out uint hashLengthBytes)
        {
            Exception error = SCryptAlgorithm.InternalTryParseSalt(salt, out saltBytes, out N, out r, out p, out hashLengthBytes);

            if (error != null)
            {
                throw error;
            }
        }
        public static string HashPassword(string password, string salt)
        {
            byte[] salt_data;
            ulong  N;
            uint   r;
            uint   p;
            uint   hashLengthBytes;

            SCryptAlgorithm.ParseSalt(salt, out salt_data, out N, out r, out p, out hashLengthBytes);
            byte[] password_data = Encoding.UTF8.GetBytes(password);
            byte[] hash_data     = SCryptAlgorithm.DeriveKey(password_data, salt_data, N, r, p, hashLengthBytes);
            return(salt.Substring(0, salt.LastIndexOf('$') + 1) + Convert.ToBase64String(hash_data));
        }
 private static void EnsureCrtInitialized()
 {
     if (!SCryptAlgorithm.expensiveCrtInitialization)
     {
         SCryptAlgorithm.EscapeExecutionContext <bool>(delegate
         {
             Replicon.Cryptography.SCrypt.MMA.SCrypt.ExpensiveCrtInitialization();
             //SCrypt.ExpensiveCrtInitialization();
             return(false);
         });
         SCryptAlgorithm.expensiveCrtInitialization = true;
     }
 }
 private static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
 {
     if (args.Name.StartsWith("Replicon.Cryptography.SCrypt.MMA,"))
     {
         lock (SCryptAlgorithm.hookupLock)
         {
             if (SCryptAlgorithm.tempPath == null)
             {
                 string root = Path.GetTempPath();
                 SCryptAlgorithm.tempPath = Path.Combine(root, Path.GetRandomFileName());
                 Directory.CreateDirectory(SCryptAlgorithm.tempPath);
                 string dll;
                 string pdb;
                 if (IntPtr.Size == 8)
                 {
                     dll = "Replicon.Cryptography.SCrypt.Replicon.Cryptography.SCrypt.MMA-x64.dll";
                     pdb = "Replicon.Cryptography.SCrypt.Replicon.Cryptography.SCrypt.MMA-x64.pdb";
                 }
                 else
                 {
                     dll = "Replicon.Cryptography.SCrypt.Replicon.Cryptography.SCrypt.MMA-win32.dll";
                     pdb = "Replicon.Cryptography.SCrypt.Replicon.Cryptography.SCrypt.MMA-win32.pdb";
                 }
                 using (Stream input = Assembly.GetExecutingAssembly().GetManifestResourceStream(dll))
                 {
                     using (Stream output = new FileStream(Path.Combine(SCryptAlgorithm.tempPath, "Replicon.Cryptography.SCrypt.MMA.dll"), FileMode.CreateNew))
                     {
                         SCryptAlgorithm.CopyStream(input, output);
                     }
                 }
                 using (Stream input2 = Assembly.GetExecutingAssembly().GetManifestResourceStream(pdb))
                 {
                     using (Stream output2 = new FileStream(Path.Combine(SCryptAlgorithm.tempPath, "Replicon.Cryptography.SCrypt.MMA.pdb"), FileMode.CreateNew))
                     {
                         SCryptAlgorithm.CopyStream(input2, output2);
                     }
                 }
             }
             return(Assembly.LoadFile(Path.Combine(SCryptAlgorithm.tempPath, "Replicon.Cryptography.SCrypt.MMA.dll")));
         }
     }
     return(null);
 }
        private static T EscapeExecutionContext <T>(Func <T> callback)
        {
            AsyncFlowControl suppressExecutionContextFlow = ExecutionContext.SuppressFlow();
            T retval2;

            try
            {
                T         retval          = default(T);
                Exception threadException = null;
                Thread    thread          = new Thread(new ThreadStart(delegate
                {
                    try
                    {
                        try
                        {
                            SCryptAlgorithm.SafeSetPrincipal(null);
                            retval = callback();
                        }
                        catch (Exception e)
                        {
                            threadException = e;
                        }
                    }
                    catch
                    {
                    }
                }));
                thread.Start();
                thread.Join();
                if (threadException != null)
                {
                    throw new TargetInvocationException(threadException);
                }
                retval2 = retval;
            }
            finally
            {
                suppressExecutionContextFlow.Undo();
            }
            return(retval2);
        }
 public static byte[] DeriveKey(byte[] password, byte[] salt, ulong N, uint r, uint p, uint derivedKeyLengthBytes)
 {
     SCryptAlgorithm.HookupAssemblyLoader();
     SCryptAlgorithm.EnsureCrtInitialized();
     return(SCryptAlgorithm.WrappedDeriveKey(password, salt, N, r, p, derivedKeyLengthBytes));
 }
 public static bool Verify(string password, string hash)
 {
     return(hash == SCryptAlgorithm.HashPassword(password, hash));
 }
        public static bool TryParseSalt(string salt, out byte[] saltBytes, out ulong N, out uint r, out uint p, out uint hashLengthBytes)
        {
            Exception error = SCryptAlgorithm.InternalTryParseSalt(salt, out saltBytes, out N, out r, out p, out hashLengthBytes);

            return(error == null);
        }
 public static string HashPassword(string password)
 {
     return(SCryptAlgorithm.HashPassword(password, SCryptAlgorithm.GenerateSalt()));
 }
 public static string HashPassword(string password, ulong iterations)
 {
     return(SCryptAlgorithm.HashPassword(password, SCryptAlgorithm.GenerateSalt(iterations)));
 }
 public static string GenerateSalt(ulong iterations)
 {
     return(SCryptAlgorithm.GenerateSalt(SCryptAlgorithm.DefaultSaltLengthBytes, iterations, SCryptAlgorithm.Default_r, SCryptAlgorithm.Default_p, SCryptAlgorithm.DefaultHashLengthBytes));
 }