Beispiel #1
0
 private static extern int xhash_text(ref Settings handle, byte[] base64_digest, byte[] password, byte[] user_salt, int free_after);
Beispiel #2
0
 private static extern int xhash_init_defaults(ref Settings handle, byte[] system_salt, int system_salt_len);
Beispiel #3
0
 private static extern int xhash_init(ref Settings handle, byte[] system_salt, int system_salt_len, int memory_bits, int additional_iterations);
Beispiel #4
0
 private static extern void xhash_free(ref Settings handle);
Beispiel #5
0
 private static extern int xhash(ref Settings handle, byte[] digest, byte[] data, int data_len, byte[] salt, int salt_len, int free_after);
Beispiel #6
0
        // Initialize the password hasher with the specified system salt, memoryBits, and additional iterations to perform.
        // Use GenerateSalt() to create the system salt and store it somewhere separate from the per-user salt.
        // The PasswordHasher will allocate (1 << memoryBits) bytes of memory and perform (1 << (memoryBits - 10)) iterations by default.
        // E.g., with the memoryBits default of 22, then 4 MB of memory is allocated and 4096 iterations are performed.
        public PasswordHasher(string systemSalt = "", int memoryBits = DefaultMemoryBits, int additionalIterations = 0)
        {
            byte[] systemSaltBytes = Encoding.ASCII.GetBytes(systemSalt);

            xhash_settings = new Settings();
            var error = (HashError)xhash_init(ref xhash_settings, systemSaltBytes, systemSaltBytes.Length, memoryBits, additionalIterations);

            switch(error)
            {
                case HashError.Success:           break;
                case HashError.InvalidMemoryBits: throw new MemoryBitsOutOfRangeException(memoryBits);
                case HashError.MallocFailed:      throw new InsufficientMemoryException("XHash.PasswordHasher was unable to allocate " + (1 << memoryBits) + " bytes of memory to perform the hashing.");
                default:                          throw new Exception("Error in PasswordHasher constructor: " + error);
            }
        }