Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

do you have C# example? how to verify digital-signatures in C# what do you think about this ? #3

Open
nyghtrocker opened this issue Jul 23, 2020 · 0 comments

Comments

@nyghtrocker
Copy link

`public bool VerifySignature(string encodedPublicKey, string signature, string payload)
{
byte[] decodedPublicKey = Convert.FromBase64String(encodedPublicKey);
var decodedSignature = Convert.FromBase64String(signature);
var decodedPayload = Convert.FromBase64String(payload);

        AsymmetricKeyParameter asymmetricKeyParameter = PublicKeyFactory.CreateKey(decodedPublicKey);
        RsaKeyParameters rsaKeyParameters = (RsaKeyParameters)asymmetricKeyParameter;
        RSAParameters rsaParameters = new RSAParameters
        {
            Modulus = rsaKeyParameters.Modulus.ToByteArrayUnsigned(),
            Exponent = rsaKeyParameters.Exponent.ToByteArrayUnsigned()
        };
       
        RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
        rsa.ImportParameters(rsaParameters);

        SHA256 sha256 = SHA256.Create();
        byte[] hash = sha256.ComputeHash(decodedPayload);

        RSAPKCS1SignatureDeformatter rsaDeformatter = new RSAPKCS1SignatureDeformatter(rsa);
        rsaDeformatter.SetHashAlgorithm("SHA256");
        if (!rsaDeformatter.VerifySignature(hash, decodedSignature))
            throw new ApplicationException(string.Format("Invalid signature"));            
        return true;
    }`
@nyghtrocker nyghtrocker changed the title do you have C# example? how to digital-signatures in C# what do you think about this ? do you have C# example? how to verify digital-signatures in C# what do you think about this ? Jul 23, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant