无法弄清楚如何使用Func <tresult>&#39;需要1个类型参数

时间:2017-08-17 14:51:11

标签: c#

它给了我:

  

/Users/asifahmed/Projects/test/test/CryptExtensions.cs(3,3):错误   CS0305:使用泛型类型&#39; Func&#39;需要1种类型   参数(CS0305)

代码:

using System;
using System.IO;
using System.Linq;
using System.Net;
using System.Security.Cryptography;
using System.Text;

public static class CryptExtensions
{
    public static string Decrypt(this string inputString, string password, bool removeUrlEncoding = true)
    {
        return inputString.Decrypt(password.PassToAESHash(), removeUrlEncoding);
    }

    public static string Decrypt(this string inputString, byte[] hash, bool removeUrlEncoding = true)
    {
        // Convert URL escape sequences back to their normal representations, then base64 decode
        if (removeUrlEncoding) { inputString = WebUtility.UrlDecode(inputString); }
        byte[] input = Convert.FromBase64String(inputString);

        using (RijndaelManaged aes = new RijndaelManaged
        {
            IV = new byte[16],
            Mode = CipherMode.CBC,
            Padding = PaddingMode.PKCS7,
            KeySize = 128,
            Key = hash
        })
        using (MemoryStream memStream = new MemoryStream(input))
        using (CryptoStream decryptStream = new CryptoStream(memStream, aes.CreateDecryptor(), CryptoStreamMode.Read))
        using (StreamReader plainTextReader = new StreamReader(decryptStream))
        {
            return plainTextReader.ReadToEnd();
        }

    }

    // See https://msdn.microsoft.com/en-us/library/windows/desktop/aa379916(v=vs.85).aspx
    public static byte[] PassToAESHash(this string pass, int keySize = 128)
    {
        SHA1 sha1 = SHA1.Create();
        byte[] hashedPass = sha1.ComputeHash(Encoding.UTF8.GetBytes(pass));


        Func<int, string> projection = x => "Value=" + x;
        int[] values = { 3, 7, 10 };
        var strings = values.Select(projection);

        // A lambda that will take a byte & position in the sequence, and XOR the supplied byte with the corresponding 
        // byte of the SHA-1 hashed pass.
        Func  XORWithHashedPass = (val, ix) => ix < hashedPass.Length ? (byte)(val ^ hashedPass[ix]) : val;

        return sha1.ComputeHash(           // First buffer per MSDN is 64 bytes of (0x36 ^ hashedPass[i])
                        Enumerable.Repeat(0x36, 64).Select(XORWithHashedPass).ToArray()
                    )
                    .Concat(
                        sha1.ComputeHash(  // Second buffer per MSDN is 64 bytes of (0x5C ^ hashedPass[i])
                                Enumerable.Repeat(0x5C, 64).Select(XORWithHashedPass).ToArray()
                            )
                    )
                    .Take(keySize / 8).ToArray();
    }
}

1 个答案:

答案 0 :(得分:0)

您需要在Func声明上指定泛型参数。最后一个泛型类型是函数的预期返回类型。这是一个例子:

Func<byte, int, byte> XORWithHashedPass = (val, ix) => { return ix < hashedPass.Length ? (byte)(val ^ hashedPass[ix]) : val; };

前两个通用参数(byte和int)是参数列表类型。

希望这会有所帮助:)