本文整理汇总了C#中RSACryptoServiceProvider.SignHash方法的典型用法代码示例。如果您正苦于以下问题:C# RSACryptoServiceProvider.SignHash方法的具体用法?C# RSACryptoServiceProvider.SignHash怎么用?C# RSACryptoServiceProvider.SignHash使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类RSACryptoServiceProvider
的用法示例。
在下文中一共展示了RSACryptoServiceProvider.SignHash方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SignHash_NullArray
public static void SignHash_NullArray()
{
using (var rsa = new RSACryptoServiceProvider())
{
Assert.Throws<ArgumentNullException>(() => rsa.SignHash(null, "SHA384"));
}
}
示例2: SignHash_WrongSizeHash
public static void SignHash_WrongSizeHash()
{
using (var rsa = new RSACryptoServiceProvider())
{
Assert.ThrowsAny<CryptographicException>(() => rsa.SignHash(Array.Empty<byte>(), "SHA384"));
}
}
示例3: SignHash_BadAlgorithm
public static void SignHash_BadAlgorithm()
{
using (var rsa = new RSACryptoServiceProvider())
{
Assert.Throws<CryptographicException>(() => rsa.SignHash(Array.Empty<byte>(), "SHA384-9000"));
}
}
示例4: SignHash_PublicKey
public static void SignHash_PublicKey()
{
using (var rsa = new RSACryptoServiceProvider())
{
RSAParameters publicParams = new RSAParameters
{
Modulus = TestData.RSA1024Params.Modulus,
Exponent = TestData.RSA1024Params.Exponent,
};
rsa.ImportParameters(publicParams);
Assert.Throws<CryptographicException>(() => rsa.SignHash(Array.Empty<byte>(), "SHA384"));
}
}
示例5: ExpectHashSignature
private static void ExpectHashSignature(
byte[] expectedSignature,
byte[] dataHash,
string hashAlgorithmName,
RSAParameters rsaParameters)
{
// RSA signatures use PKCS 1.5 EMSA encoding (encoding method, signature algorithm).
// EMSA specifies a fixed filler type of { 0x01, 0xFF, 0xFF ... 0xFF, 0x00 } whose length
// is as long as it needs to be to match the block size. Since the filler is deterministic,
// the signature is deterministic, so we can safely verify it here.
byte[] signature;
using (var rsa = new RSACryptoServiceProvider())
{
rsa.ImportParameters(rsaParameters);
signature = rsa.SignHash(dataHash, hashAlgorithmName);
}
Assert.Equal(expectedSignature, signature);
}
示例6: VerifyLegacySignVerifyHash
// (false, false) is not required, that would be equivalent to the RSA AlgorithmImplementation suite.
public static void VerifyLegacySignVerifyHash(bool useLegacySign, bool useLegacyVerify)
{
byte[] dataHash, signature;
using (HashAlgorithm hash = SHA256.Create())
{
dataHash = hash.ComputeHash(TestData.HelloBytes);
}
using (var rsa = new RSACryptoServiceProvider())
{
rsa.ImportParameters(TestData.RSA2048Params);
signature = useLegacySign ?
rsa.SignHash(dataHash, "SHA256") :
rsa.SignHash(dataHash, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
}
bool verified;
using (var rsa = new RSACryptoServiceProvider())
{
rsa.ImportParameters(
new RSAParameters
{
Modulus = TestData.RSA2048Params.Modulus,
Exponent = TestData.RSA2048Params.Exponent,
});
verified = useLegacyVerify ?
rsa.VerifyHash(dataHash, "SHA256", signature) :
rsa.VerifyHash(dataHash, signature, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
}
Assert.True(verified);
}
示例7: UpdateRevocationList
public bool UpdateRevocationList(string group_name)
{
if(!Context.Request.IsLocal) {
throw new Exception("Call must be made locally!");
}
IDbConnection dbcon = new MySqlConnection(_connection_string);
dbcon.Open();
IDbCommand dbcmd = dbcon.CreateCommand();
// Get the group_id
string sql = "SELECT group_id from groupvpn WHERE group_name = \"" + group_name + "\"";
dbcmd.CommandText = sql;
IDataReader reader = dbcmd.ExecuteReader();
if(!reader.Read()) {
throw new Exception("No such group.");
}
int group_id = (int) reader["group_id"];
reader.Close();
// get revoked users
sql = "SELECT user_id FROM groups WHERE group_id = \"" + group_id + "\" and revoked = 1";
dbcmd.CommandText = sql;
reader = dbcmd.ExecuteReader();
// add revoked users by user name to the revocation list
ArrayList revoked_user_ids = new ArrayList();
while(reader.Read()) {
revoked_user_ids.Add((int) reader["user_id"]);
}
reader.Close();
ArrayList revoked_users = new ArrayList();
foreach(int user_id in revoked_user_ids) {
sql = "SELECT username FROM " + _db_prefix + "users WHERE id = " + user_id;
dbcmd.CommandText = sql;
IDataReader user_reader = dbcmd.ExecuteReader();
if(!user_reader.Read()) {
continue;
}
revoked_users.Add(user_reader["username"]);
user_reader.Close();
}
reader.Close();
dbcmd.Dispose();
dbcon.Close();
// get private key
string private_path = GetGroupPrivatePath(group_name) + "private_key";
if(!File.Exists(private_path)) {
throw new Exception("No private key for " + private_path + " " + File.Exists(private_path));
}
RSACryptoServiceProvider private_key = new RSACryptoServiceProvider();
using(FileStream fs = File.Open(private_path, FileMode.Open)) {
byte[] blob = new byte[fs.Length];
fs.Read(blob, 0, blob.Length);
private_key.ImportCspBlob(blob);
}
// create revocation list
byte[] to_sign = null;
using(MemoryStream ms = new MemoryStream()) {
NumberSerializer.WriteLong(DateTime.UtcNow.Ticks, ms);
AdrConverter.Serialize(revoked_users, ms);
to_sign = ms.ToArray();
}
// sign revocation list
SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider();
byte[] hash = sha1.ComputeHash(to_sign);
byte[] signature = private_key.SignHash(hash, CryptoConfig.MapNameToOID("SHA1"));
byte[] data = new byte[4 + to_sign.Length + signature.Length];
NumberSerializer.WriteInt(to_sign.Length, data, 0);
to_sign.CopyTo(data, 4);
signature.CopyTo(data, 4 + to_sign.Length);
// write revocation list
using(FileStream fs = File.Open(GetGroupDataPath(group_name) + "revocation_list", FileMode.Create)) {
fs.Write(data, 0, data.Length);
}
return true;
}