本文整理汇总了C#中System.Security.Cryptography.KeyedHashAlgorithm类的典型用法代码示例。如果您正苦于以下问题:C# KeyedHashAlgorithm类的具体用法?C# KeyedHashAlgorithm怎么用?C# KeyedHashAlgorithm使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
KeyedHashAlgorithm类属于System.Security.Cryptography命名空间,在下文中一共展示了KeyedHashAlgorithm类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C#代码示例。
示例1: HMACSign
public static string HMACSign(string data, SecureString key, KeyedHashAlgorithm algorithm)
{
string str;
if (key == null)
{
throw new ArgumentNullException("key", "Please specify a Secret Signing Key.");
}
if (string.IsNullOrEmpty(data))
{
throw new ArgumentNullException("data", "Please specify data to sign.");
}
if (algorithm == null)
{
throw new ArgumentNullException("algorithm", "Please specify a KeyedHashAlgorithm to use.");
}
IntPtr zero = IntPtr.Zero;
char[] destination = new char[key.Length];
try
{
zero = Marshal.SecureStringToBSTR(key);
Marshal.Copy(zero, destination, 0, destination.Length);
algorithm.Key = Encoding.UTF8.GetBytes(destination);
str = Convert.ToBase64String(algorithm.ComputeHash(Encoding.UTF8.GetBytes(data.ToCharArray())));
}
finally
{
Marshal.ZeroFreeBSTR(zero);
algorithm.Clear();
Array.Clear(destination, 0, destination.Length);
}
return str;
}
开发者ID:pusp,项目名称:o2platform,代码行数:32,代码来源:AWSSDKUtils.cs
示例2: ShortKeyedHashAlgorithm
public ShortKeyedHashAlgorithm (KeyedHashAlgorithm baseAlgo, int outputHashBits)
{
_base = baseAlgo;
_hashBits = outputHashBits;
if (outputHashBits <= 0 || _base.HashSize < outputHashBits)
throw new ArgumentOutOfRangeException ();
}
开发者ID:kazuki,项目名称:opencrypto.net,代码行数:7,代码来源:ShortKeyedHashAlgorithm.cs
示例3: ComputeSignature
private void ComputeSignature(KeyedHashAlgorithm hash)
{
this.Signature.SignedInfo.ComputeReferenceDigests();
this.Signature.SignedInfo.ComputeHash(hash);
byte[] signatureValue = hash.Hash;
this.Signature.SetSignatureValue(signatureValue);
}
开发者ID:pritesh-mandowara-sp,项目名称:DecompliedDotNetLibraries,代码行数:7,代码来源:SignedXml.cs
示例4: HmacInfo
public HmacInfo(KeyedHashAlgorithm algorithm, int keySize)
{
Contract.Requires(algorithm != null);
KeySize = keySize;
Hmac = key => new HmacAlgorithm(algorithm, keySize, key);
}
开发者ID:jinhang2008,项目名称:FxSsh,代码行数:7,代码来源:HmacInfo.cs
示例5: GenerateKey
ProtectedKey GenerateKey(KeyedHashAlgorithm algorithm, DataProtectionScope dataProtectionScope)
{
using (algorithm)
{
return ProtectedKey.CreateFromPlaintextKey(algorithm.Key, dataProtectionScope);
}
}
开发者ID:davinx,项目名称:himedi,代码行数:7,代码来源:KeyedHashKeyGenerator.cs
示例6: KeyHashAlgorithmGenerator
protected KeyHashAlgorithmGenerator(KeyedHashAlgorithm algorithm)
{
if (algorithm == null)
{
throw new ArgumentNullException("algorithm");
}
this._algorithm = algorithm;
}
开发者ID:Ravivishnubhotla,项目名称:basewebframework,代码行数:8,代码来源:KeyHashAlgorithmGenerator.cs
示例7: Sign
public static string Sign(string data, SecureString key, KeyedHashAlgorithm algorithm)
{
if (key == null)
{
throw new AmazonCloudFrontException("The AWS Secret Access Key specified is NULL");
}
return AWSSDKUtils.HMACSign(data, key, algorithm);
}
开发者ID:pusp,项目名称:o2platform,代码行数:8,代码来源:AmazonCloudFrontUtil.cs
示例8: HmacAlgorithm
public HmacAlgorithm(KeyedHashAlgorithm algorithm, int keySize, byte[] key)
{
Contract.Requires(algorithm != null);
Contract.Requires(key != null);
Contract.Requires(keySize == key.Length << 3);
_algorithm = algorithm;
algorithm.Key = key;
}
开发者ID:jinhang2008,项目名称:FxSsh,代码行数:9,代码来源:HmacAlgorithm.cs
示例9: HMACSign
public static string HMACSign(string data, System.Security.SecureString key, KeyedHashAlgorithm algorithm)
#endif
{
#if UNITY
if (String.IsNullOrEmpty(key))
#else
if (null == key)
#endif
{
throw new ArgumentNullException("key", "The AWS Secret Access Key specified is NULL!");
}
if (String.IsNullOrEmpty(data))
{
throw new ArgumentNullException("data", "Please specify data to sign.");
}
if (null == algorithm)
{
throw new ArgumentNullException("algorithm", "Please specify a KeyedHashAlgorithm to use.");
}
#if UNITY
try
{
algorithm.Key = Encoding.UTF8.GetBytes(key);
return Convert.ToBase64String(algorithm.ComputeHash(
Encoding.UTF8.GetBytes(data.ToCharArray()))
);
}
finally
{
algorithm.Clear();
}
#else
// pointer to hold unmanaged reference to SecureString instance
IntPtr bstr = IntPtr.Zero;
char[] charArray = new char[key.Length];
try
{
// Marshal SecureString into byte array
bstr = Marshal.SecureStringToBSTR(key);
Marshal.Copy(bstr, charArray, 0, charArray.Length);
algorithm.Key = Encoding.UTF8.GetBytes(charArray);
return Convert.ToBase64String(algorithm.ComputeHash(
Encoding.UTF8.GetBytes(data.ToCharArray()))
);
}
finally
{
// Make sure that the clear text data is zeroed out
Marshal.ZeroFreeBSTR(bstr);
algorithm.Clear();
Array.Clear(charArray, 0, charArray.Length);
}
#endif
}
开发者ID:GoCarrot,项目名称:carrot-net,代码行数:56,代码来源:AWSSDKUtils.cs
示例10: SymmetricSignatureProvider
/// <summary>
/// Initializes a new instance of the <see cref="SymmetricSignatureProvider"/> class that uses an <see cref="SymmetricSecurityKey"/> to create and / or verify signatures over a array of bytes.
/// </summary>
/// <param name="key">The <see cref="SymmetricSecurityKey"/> used for signing.</param>
/// <param name="algorithm">The signature algorithm to use.</param>
/// <exception cref="ArgumentNullException">'key' is null.</exception>
/// <exception cref="ArgumentNullException">'algorithm' is null.</exception>
/// <exception cref="ArgumentException">'algorithm' contains only whitespace.</exception>
/// <exception cref="ArgumentOutOfRangeException">'<see cref="SymmetricSecurityKey"/>.KeySize' is smaller than <see cref="SignatureProviderFactory.MinimumSymmetricKeySizeInBits"/>.</exception>
/// <exception cref="InvalidOperationException"><see cref="SymmetricSecurityKey.GetKeyedHashAlgorithm"/> throws.</exception>
/// <exception cref="InvalidOperationException"><see cref="SymmetricSecurityKey.GetKeyedHashAlgorithm"/> returns null.</exception>
/// <exception cref="InvalidOperationException"><see cref="SymmetricSecurityKey.GetSymmetricKey"/> throws.</exception>
public SymmetricSignatureProvider(SymmetricSecurityKey key, string algorithm)
{
if (key == null)
{
throw new ArgumentNullException("key");
}
if (null == algorithm)
{
throw new ArgumentNullException(algorithm);
}
if (string.IsNullOrWhiteSpace(algorithm))
{
throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, ErrorMessages.IDX10002, "algorithm"));
}
if (key.KeySize < SignatureProviderFactory.MinimumSymmetricKeySizeInBits)
{
throw new ArgumentOutOfRangeException("key.KeySize", key.KeySize, string.Format(CultureInfo.InvariantCulture, ErrorMessages.IDX10603, key.GetType(), SignatureProviderFactory.MinimumSymmetricKeySizeInBits));
}
try
{
this.keyedHash = key.GetKeyedHashAlgorithm(algorithm);
}
catch (Exception ex)
{
if (DiagnosticUtility.IsFatal(ex))
{
throw;
}
throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, ErrorMessages.IDX10632, algorithm, key, ex), ex);
}
if (this.keyedHash == null)
{
throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, ErrorMessages.IDX10633, algorithm, key));
}
try
{
this.keyedHash.Key = key.GetSymmetricKey();
}
catch (Exception ex)
{
if (DiagnosticUtility.IsFatal(ex))
{
throw;
}
throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, ErrorMessages.IDX10634, algorithm, key, ex), ex);
}
}
开发者ID:richardschneider,项目名称:azure-activedirectory-identitymodel-extensions-for-dotnet,代码行数:67,代码来源:SymmetricSignatureProvider.cs
示例11: ManagedPsha1
public ManagedPsha1(byte[] secret, byte[] label, byte[] seed)
{
this.secret = secret;
this.seed = DiagnosticUtility.Utility.AllocateByteArray(label.Length + seed.Length);
label.CopyTo(this.seed, 0);
seed.CopyTo(this.seed, label.Length);
this.aValue = this.seed;
this.chunk = new byte[0];
this.index = 0;
this.position = 0;
this.hmac = CryptoHelper.NewHmacSha1KeyedHashAlgorithm(secret);
this.buffer = DiagnosticUtility.Utility.AllocateByteArray((this.hmac.HashSize / 8) + this.seed.Length);
}
开发者ID:pritesh-mandowara-sp,项目名称:DecompliedDotNetLibraries,代码行数:13,代码来源:Psha1DerivedKeyGenerator.cs
示例12: Pbkdf2
/// <summary>
/// Creates a new PBKDF2 stream.
/// </summary>
/// <param name="hmacAlgorithm">
/// The HMAC algorithm to use, for example <see cref="HMACSHA256"/>.
/// Make sure to set <see cref="KeyedHashAlgorithm.Key"/>.
/// </param>
/// <param name="salt">
/// The salt.
/// A unique salt means a unique PBKDF2 stream, even if the original key is identical.
/// </param>
/// <param name="iterations">The number of iterations to apply.</param>
public Pbkdf2(KeyedHashAlgorithm hmacAlgorithm, byte[] salt, int iterations)
{
Check.Null("hmacAlgorithm", hmacAlgorithm);
Check.Null("salt", salt);
Check.Length("salt", salt, 0, int.MaxValue - 4);
Check.Range("iterations", iterations, 1, int.MaxValue);
if (hmacAlgorithm.HashSize == 0 || hmacAlgorithm.HashSize % 8 != 0)
{ throw Exceptions.Argument("hmacAlgorithm", "Unsupported hash size."); }
int hmacLength = hmacAlgorithm.HashSize / 8;
_saltBuffer = new byte[salt.Length + 4]; Array.Copy(salt, _saltBuffer, salt.Length);
_iterations = iterations; _hmacAlgorithm = hmacAlgorithm;
_digest = new byte[hmacLength]; _digestT1 = new byte[hmacLength];
}
开发者ID:nikropht,项目名称:NBitcoin,代码行数:26,代码来源:Pbkdf2.cs
示例13: KeyedHashAlgorithmKeyCreator
/// <summary>
/// Initialize a new instance of the <see cref="KeyedHashAlgorithmKeyCreator"/> class.
/// </summary>
/// <param name="algorithmType">The <see cref="HashAlgorithm"/> type that should be used.</param>
public KeyedHashAlgorithmKeyCreator(Type algorithmType)
{
if (algorithmType == null)
{
throw new ArgumentNullException("algorithmType");
}
if (!typeof(HashAlgorithm).IsAssignableFrom(algorithmType))
{
throw new ArgumentException(Resources.TypeShouldDeriveFromHashAlgorithm, "algorithmType");
}
this.algorithm = CreateAlgorithm(algorithmType);
this.algorithmType = algorithmType;
}
开发者ID:ChiangHanLung,项目名称:PIC_VDS,代码行数:19,代码来源:KeyedHashAlgorithmKeyCreator.cs
示例14: KeyedHashAlgorithm
public KeyedHashAlgorithm(HashAlgorithmNames algorithm)
{
switch (algorithm)
{
case HashAlgorithmNames.Sha1:
alg = new C.HMACSHA1();
break;
case HashAlgorithmNames.Sha256:
alg = new C.HMACSHA256();
break;
case HashAlgorithmNames.Sha384:
alg = new C.HMACSHA384();
break;
case HashAlgorithmNames.Sha512:
alg = new C.HMACSHA512();
break;
case HashAlgorithmNames.Md5:
alg = new C.HMACMD5();
break;
default:
throw new NotSupportedException();
}
}
开发者ID:luzianz,项目名称:TwitterClient,代码行数:23,代码来源:KeyedHashAlgorithm.cs
示例15: HMACSign
/// <summary>
/// Computes RFC 2104-compliant HMAC signature
/// </summary>
/// <param name="data">The data to be signed</param>
/// <param name="key">The secret signing key</param>
/// <param name="algorithm">The algorithm to sign the data with</param>
/// <exception cref="T:System.ArgumentNullException"/>
/// <returns>A string representing the HMAC signature</returns>
public static string HMACSign(byte[] data, string key, KeyedHashAlgorithm algorithm)
{
if (String.IsNullOrEmpty(key))
{
throw new ArgumentNullException("key", "Please specify a Secret Signing Key.");
}
if (data == null || data.Length == 0)
{
throw new ArgumentNullException("data", "Please specify data to sign.");
}
if (null == algorithm)
{
throw new ArgumentNullException("algorithm", "Please specify a KeyedHashAlgorithm to use.");
}
try
{
algorithm.Key = Encoding.UTF8.GetBytes(key);
byte[] bytes = algorithm.ComputeHash(data);
return Convert.ToBase64String(bytes);
}
finally
{
algorithm.Clear();
}
}
开发者ID:grandcloud,项目名称:sndacs-csharp,代码行数:36,代码来源:CSSDKUtils.cs
示例16: CreateResourceUrl
static string CreateResourceUrl (KeyedHashAlgorithm kha, string assemblyName, string assemblyNameHash, string assemblyPath, string resourceNameHash, bool debug, bool notifyScriptLoaded)
{
string atime = String.Empty;
string extra = String.Empty;
#if SYSTEM_WEB_EXTENSIONS
extra = QueryParamSeparator + "n=" + (notifyScriptLoaded ? "t" : "f");
#endif
#if TARGET_JVM
atime = QueryParamSeparator + "t=" + assemblyName.GetHashCode ();
#else
if (!String.IsNullOrEmpty (assemblyPath) && File.Exists (assemblyPath))
atime = QueryParamSeparator + "t=" + File.GetLastWriteTimeUtc (assemblyPath).Ticks;
else
atime = QueryParamSeparator + "t=" + DateTime.UtcNow.Ticks;
#endif
string d = assemblyNameHash + "_" + resourceNameHash + (debug ? "_t" : "_f");
string href = HandlerFileName + "?d=" + d + atime + extra;
HttpContext ctx = HttpContext.Current;
HttpRequest req = ctx != null ? ctx.Request : null;
if (req != null) {
string appPath = VirtualPathUtility.AppendTrailingSlash (req.ApplicationPath);
href = appPath + href;
}
return href;
}
开发者ID:telurmasin,项目名称:mono,代码行数:27,代码来源:AssemblyResourceLoader.cs
示例17: GetResourceUrl
static string GetResourceUrl (KeyedHashAlgorithm kha, Assembly assembly, string resourceName, bool notifyScriptLoaded)
{
string assemblyName = assembly == currAsm ? "s" : assembly.GetName ().FullName;
string assemblyNameHash = GetStringHash (kha, assemblyName);
string resourceNameHash = GetStringHash (kha, resourceName);
bool debug = false;
string url;
AssemblyEmbeddedResources entry;
try {
_embeddedResourcesLock.EnterUpgradeableReadLock ();
if (!_embeddedResources.TryGetValue (assemblyNameHash, out entry) || entry == null) {
try {
_embeddedResourcesLock.EnterWriteLock ();
entry = new AssemblyEmbeddedResources () {
AssemblyName = assemblyName
};
InitEmbeddedResourcesUrls (kha, assembly, assemblyName, assemblyNameHash, entry);
_embeddedResources.Add (assemblyNameHash, entry);
} finally {
_embeddedResourcesLock.ExitWriteLock ();
}
}
string lookupKey;
#if SYSTEM_WEB_EXTENSIONS
debug = resourceName.EndsWith (".debug.js", StringComparison.OrdinalIgnoreCase);
string dbgTail = debug ? "d" : String.Empty;
lookupKey = resourceNameHash + (notifyScriptLoaded ? "t" : "f") + dbgTail;
#else
lookupKey = resourceNameHash;
#endif
EmbeddedResource res;
if (entry.Resources.TryGetValue (lookupKey, out res) && res != null)
url = res.Url;
else {
#if SYSTEM_WEB_EXTENSIONS
if (debug) {
resourceNameHash = GetStringHash (kha, resourceName.Substring (0, resourceName.Length - 9) + ".js");
lookupKey = resourceNameHash + (notifyScriptLoaded ? "t" : "f");
if (entry.Resources.TryGetValue (lookupKey, out res) && res != null)
url = res.Url;
else
url = null;
} else
#endif
url = null;
}
} finally {
_embeddedResourcesLock.ExitUpgradeableReadLock ();
}
if (url == null)
url = CreateResourceUrl (kha, assemblyName, assemblyNameHash, assembly.Location, resourceNameHash, debug, notifyScriptLoaded);
return url;
}
开发者ID:telurmasin,项目名称:mono,代码行数:57,代码来源:AssemblyResourceLoader.cs
示例18: InitEmbeddedResourcesUrls
static void InitEmbeddedResourcesUrls (KeyedHashAlgorithm kha, Assembly assembly, string assemblyName, string assemblyHash, AssemblyEmbeddedResources entry)
{
WebResourceAttribute [] attrs = (WebResourceAttribute []) assembly.GetCustomAttributes (typeof (WebResourceAttribute), false);
WebResourceAttribute attr;
string apath = assembly.Location;
for (int i = 0; i < attrs.Length; i++) {
attr = attrs [i];
string resourceName = attr.WebResource;
if (!String.IsNullOrEmpty (resourceName)) {
string resourceNameHash = GetStringHash (kha, resourceName);
#if SYSTEM_WEB_EXTENSIONS
bool debug = resourceName.EndsWith (".debug.js", StringComparison.OrdinalIgnoreCase);
string dbgTail = debug ? "d" : String.Empty;
string rkNoNotify = resourceNameHash + "f" + dbgTail;
string rkNotify = resourceNameHash + "t" + dbgTail;
if (!entry.Resources.ContainsKey (rkNoNotify)) {
var er = new EmbeddedResource () {
Name = resourceName,
Attribute = attr,
Url = CreateResourceUrl (kha, assemblyName, assemblyHash, apath, rkNoNotify, debug, false)
};
entry.Resources.Add (rkNoNotify, er);
}
if (!entry.Resources.ContainsKey (rkNotify)) {
var er = new EmbeddedResource () {
Name = resourceName,
Attribute = attr,
Url = CreateResourceUrl (kha, assemblyName, assemblyHash, apath, rkNotify, debug, true)
};
entry.Resources.Add (rkNotify, er);
}
#else
if (!entry.Resources.ContainsKey (resourceNameHash)) {
var er = new EmbeddedResource () {
Name = resourceName,
Attribute = attr,
Url = CreateResourceUrl (kha, assemblyName, assemblyHash, apath, resourceNameHash, false, false)
};
entry.Resources.Add (resourceNameHash, er);
}
#endif
}
}
}
开发者ID:telurmasin,项目名称:mono,代码行数:48,代码来源:AssemblyResourceLoader.cs
示例19: GetStringHash
static string GetStringHash (KeyedHashAlgorithm kha, string str)
{
if (String.IsNullOrEmpty (str))
return String.Empty;
string result;
Dictionary <string, string> cache = StringHashCache;
if (cache.TryGetValue (str, out result))
return result;
result = Convert.ToBase64String (kha.ComputeHash (Encoding.UTF8.GetBytes (str)));
cache.Add (str, result);
return result;
}
开发者ID:telurmasin,项目名称:mono,代码行数:14,代码来源:AssemblyResourceLoader.cs
示例20: ComputeSignature
public void ComputeSignature(KeyedHashAlgorithm macAlg)
{
int hashSize;
if (macAlg == null)
{
throw new ArgumentNullException("macAlg");
}
HMAC hash = macAlg as HMAC;
if (hash == null)
{
throw new CryptographicException(SecurityResources.GetResourceString("Cryptography_Xml_SignatureMethodKeyMismatch"));
}
if (this.m_signature.SignedInfo.SignatureLength == null)
{
hashSize = hash.HashSize;
}
else
{
hashSize = Convert.ToInt32(this.m_signature.SignedInfo.SignatureLength, (IFormatProvider) null);
}
if ((hashSize < 0) || (hashSize > hash.HashSize))
{
throw new CryptographicException(SecurityResources.GetResourceString("Cryptography_Xml_InvalidSignatureLength"));
}
if ((hashSize % 8) != 0)
{
throw new CryptographicException(SecurityResources.GetResourceString("Cryptography_Xml_InvalidSignatureLength2"));
}
this.BuildDigestedReferences();
switch (hash.HashName)
{
case "SHA1":
this.SignedInfo.SignatureMethod = "http://www.w3.org/2000/09/xmldsig#hmac-sha1";
break;
case "SHA256":
this.SignedInfo.SignatureMethod = "http://www.w3.org/2001/04/xmldsig-more#hmac-sha256";
break;
case "SHA384":
this.SignedInfo.SignatureMethod = "http://www.w3.org/2001/04/xmldsig-more#hmac-sha384";
break;
case "SHA512":
this.SignedInfo.SignatureMethod = "http://www.w3.org/2001/04/xmldsig-more#hmac-sha512";
break;
case "MD5":
this.SignedInfo.SignatureMethod = "http://www.w3.org/2001/04/xmldsig-more#hmac-md5";
break;
case "RIPEMD160":
this.SignedInfo.SignatureMethod = "http://www.w3.org/2001/04/xmldsig-more#hmac-ripemd160";
break;
default:
throw new CryptographicException(SecurityResources.GetResourceString("Cryptography_Xml_SignatureMethodKeyMismatch"));
}
byte[] src = this.GetC14NDigest(hash);
SignedXmlDebugLog.LogSigning(this, hash);
this.m_signature.SignatureValue = new byte[hashSize / 8];
Buffer.BlockCopy(src, 0, this.m_signature.SignatureValue, 0, hashSize / 8);
}
开发者ID:pritesh-mandowara-sp,项目名称:DecompliedDotNetLibraries,代码行数:63,代码来源:SignedXml.cs
注:本文中的System.Security.Cryptography.KeyedHashAlgorithm类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论