public bool ValidatePasswordEncryptedWithSalt(byte[] clearSalt, byte[] sessionKey, _SAMPR_ENCRYPTED_USER_PASSWORD_NEW target)
 {
     _SAMPR_ENCRYPTED_USER_PASSWORD_NEW expected = GetPasswordEncryptedWithSalt(clearSalt, sessionKey);
     bool isSame = ObjectUtility.DeepCompare(expected, target);
     return isSame;
 }
        public _SAMPR_ENCRYPTED_USER_PASSWORD_NEW GetPasswordEncryptedWithSalt(byte[] clearSalt, byte[] sessionKey)
        {
            if (clearSalt == null)
            {
                // Will be changed to StackSdkException after StackSdkxception class is updated
                throw new ArgumentNullException("clearSalt");
            }

            if (sessionKey == null)
            {
                throw new ArgumentNullException("sessionKey");
            }

            byte[] digestData = MergeBlocks(clearSalt, sessionKey);

            // Compute digest for clearSalt and sessionKey, this digest is the key for RC4 encryption
            MD5 md5 = MD5.Create();
            byte[] digest = md5.ComputeHash(digestData);

            _SAMPR_ENCRYPTED_USER_PASSWORD_NEW encryptedPwd = new _SAMPR_ENCRYPTED_USER_PASSWORD_NEW();
            encryptedPwd.Buffer = new byte[encryptedPwdSize + pwdLenSize + pwdEncryptionKeySize];

            // Copy password bytes to the tail of the encryptedPwdSize bytes of buffer
            byte[] existingPwdBuffer = Encoding.Unicode.GetBytes(existingPwd);
            Array.Copy(existingPwdBuffer, 0, encryptedPwd.Buffer,
                encryptedPwdSize - existingPwdBuffer.Length, existingPwdBuffer.Length);

            // Set password length for the last pwdLenSize bytes of (the encryptedPwdSize + pwdLenSize) bytes
            byte[] lengthBuffer = BitConverter.GetBytes(existingPwdBuffer.Length);
            Array.Copy(lengthBuffer, 0, encryptedPwd.Buffer, encryptedPwdSize, lengthBuffer.Length);
            // Copy clear salt to the last pwdEncryptionKeySize bytes of the buffer
            Array.Copy(clearSalt, 0, encryptedPwd.Buffer, encryptedPwdSize + pwdLenSize, clearSalt.Length);
            // Do RC4 encryption, the last pwdEncryptionKeySize bytes are intact
            byte[] rc4Result = RC4Encrypt(encryptedPwd.Buffer, 0,
                encryptedPwd.Buffer.Length - pwdEncryptionKeySize, digest);
            Array.Copy(rc4Result, encryptedPwd.Buffer, rc4Result.Length);

            return encryptedPwd;
        }