本文整理汇总了C#中System.Security.Cryptography.SHA1Managed.Initialize方法的典型用法代码示例。如果您正苦于以下问题:C# SHA1Managed.Initialize方法的具体用法?C# SHA1Managed.Initialize怎么用?C# SHA1Managed.Initialize使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Security.Cryptography.SHA1Managed
的用法示例。
在下文中一共展示了SHA1Managed.Initialize方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: WardenRandom
public WardenRandom(byte[] seed)
{
Data1 = new byte[0x14];
Data2 = new byte[0x14];
Data3 = new byte[0x14];
int length1 = (int)seed.Length >> 1;
int length2 = seed.Length - length1;
byte[] seed1 = new byte[length1];
byte[] seed2 = new byte[length2];
for (int i = 0; i < length1; i++)
seed1[i] = seed[i];
for (int i = 0; i < length2; i++)
seed2[i] = seed[i + length1];
SHA1 sha = new SHA1Managed();
Data2 = sha.ComputeHash(seed1);
Data3 = sha.ComputeHash(seed2);
sha.Initialize();
sha.TransformBlock(Data2, 0, Data2.Length, Data2, 0);
sha.TransformBlock(Data1, 0, Data1.Length, Data1, 0);
sha.TransformFinalBlock(Data3, 0, Data3.Length);
Data1 = sha.Hash;
sha.Initialize();
}
示例2: CreateServerKeyExchangeSign
public int CreateServerKeyExchangeSign(SecurityParameters sparams, byte[] params_buffer, int params_offset, int params_length, byte[] sign_buffer, int sign_offset)
{
byte[] hash;
using (SHA1Managed sha1 = new SHA1Managed ()) {
sha1.Initialize ();
sha1.TransformBlock (sparams.ClientRandom, 0, sparams.ClientRandom.Length, sparams.ClientRandom, 0);
sha1.TransformBlock (sparams.ServerRandom, 0, sparams.ServerRandom.Length, sparams.ServerRandom, 0);
sha1.TransformBlock (params_buffer, params_offset, params_length, params_buffer, params_offset);
sha1.TransformFinalBlock (Utility.EmptyByteArray, 0, 0);
hash = sha1.Hash;
}
// 署名
byte[] sign = _ecdsa.SignHash (hash);
// DER形式に変換
// TODO: 400bit以上の署名サイズに対応させる
byte der_len = (byte)(sign.Length + 6);
byte int_len = (byte)(sign.Length >> 1);
sign_buffer[sign_offset + 0] = 0x30;
sign_buffer[sign_offset + 1] = (byte)(der_len - 2);
sign_buffer[sign_offset + 2] = 0x02;
sign_buffer[sign_offset + 3] = int_len;
Buffer.BlockCopy (sign, 0, sign_buffer, sign_offset + 4, int_len);
sign_offset += int_len + 4;
sign_buffer[sign_offset + 0] = 0x02;
sign_buffer[sign_offset + 1] = int_len;
Buffer.BlockCopy (sign, int_len, sign_buffer, sign_offset + 2, int_len);
return der_len;
}
示例3: CreateWsseHeader
private String CreateWsseHeader()
{
// HTTPリクエスト毎に生成するセキュリティ・トークン(ランダム文字列)
byte[] b_nonce = new byte[8];
Random rand = new Random();
rand.NextBytes( b_nonce );
// nonce 生成時の日
string created = DateTime.Now.ToUniversalTime().ToString( "o" );
byte[] b_created = Encoding.UTF8.GetBytes( created );
byte[] b_password = Encoding.UTF8.GetBytes( Password );
SHA1Managed sh1 = new SHA1Managed();
sh1.Initialize();
// Join
byte[] origin = new byte[b_nonce.Length + b_created.Length + b_password.Length];
Array.Copy( b_nonce, 0, origin, 0, b_nonce.Length );
Array.Copy( b_created, 0, origin, b_nonce.Length, b_created.Length );
Array.Copy( b_password, 0, origin, b_nonce.Length + b_created.Length, b_password.Length );
// Create Hash Value
byte[] passwordDigest = sh1.ComputeHash( origin );
string header = String.Format(
"UsernameToken Username=\"{0}\", PasswordDigest=\"{1}\", Nonce=\"{2}\", Created=\"{3}\"",
Username, Convert.ToBase64String( passwordDigest ), Convert.ToBase64String( b_nonce ), created );
return header;
}
示例4: CreateWsseHeader
private string CreateWsseHeader(string username, string password)
{
var nonce = new byte[8];
new Random().NextBytes(nonce);
var created = DateTime.Now.ToUniversalTime().ToString("o");
var digest = nonce
.Concat(Encoding.UTF8.GetBytes(created))
.Concat(Encoding.UTF8.GetBytes(password))
.ToArray();
var sh1 = new SHA1Managed();
sh1.Initialize();
var hashedDigest = sh1.ComputeHash(digest);
return string.Format(
"UsernameToken Username=\"{0}\", PasswordDigest=\"{1}\", Nonce=\"{2}\", Created=\"{3}\"",
username, Convert.ToBase64String(hashedDigest), Convert.ToBase64String(nonce), created);
}
示例5: GetRevlogEntryDataNodeID
protected HgNodeID GetRevlogEntryDataNodeID(HgNodeID firstParentNodeID, HgNodeID secondParentNodeID, byte[] data)
{
var parents = new[] {
Min(firstParentNodeID, secondParentNodeID),
Max(firstParentNodeID, secondParentNodeID)
};
using(var hash = new SHA1Managed())
{
hash.Initialize();
var buffer = new byte[hash.HashSize / 8];
hash.TransformBlock(parents[0].NodeID, 0, 20, buffer, 0);
hash.TransformBlock(parents[1].NodeID, 0, 20, buffer, 0);
hash.TransformFinalBlock(data, 0, data.Length);
buffer = hash.Hash;
var node = new HgNodeID(buffer);
return node;
} // using
}
示例6: makeHandshake
void makeHandshake( string url )
{
Uri uri = new Uri( url );
string host = uri.DnsSafeHost;
string path = uri.PathAndQuery;
bool isWss = url.ToLower().StartsWith( "wss" );
string origin = uri.Scheme + "://" + host;
Socket sock = new Socket( AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp );
sock.SetSocketOption( SocketOptionLevel.Socket, SocketOptionName.NoDelay, true );
IPAddress addr = Dns.GetHostAddresses( host )[ 0 ];
int port = uri.Port;
port = port < 0 ? (isWss ? 443 : 80) : port;
sock.Connect( new IPEndPoint( addr, port ) );
Stream stream = new NetworkStream( sock, true );
if (isWss)
{
var ssl = new SslStream( stream, false, validateServerCertificate );
ssl.AuthenticateAsClient( host );
stream = ssl;
}
// create HttpKey header
var bsec = new byte[ 16 ];
using (var rnd = RandomNumberGenerator.Create())
rnd.GetBytes( bsec );
var ssec = Convert.ToBase64String( bsec );
var whs = RequestHeaders;
whs[ HttpRequestHeader.Host ] = host;
whs[ sOrigin ] = origin;
whs[ sSecOrigin ] = origin;
whs[ sSecKey ] = ssec;
string sreq0 = string.Format( HttpHeader, path );
string sreq = sreq0 + "\r\n" + whs.ToString();
whs[ HttpKey ] = sreq0;
// send HttpKey-header to server (UTF8. ignore _enc)
int len = Encoding.UTF8.GetBytes( sreq, 0, sreq.Length, _buf_data, 0 );
stream.Write( _buf_data, 0, len );
// read response from server
whs = readHttpResponse( stream, ResponseHeaders );
// Sincerely yours, Captain Obviousness
// check headers
if ((!"HTTP/1.1 101 Switching Protocols".Equals(whs[HttpKey]) && !"HTTP/1.1 101 Web Socket Protocol Handshake".Equals(whs[HttpKey]))
|| !sWebsocket.Equals( whs[ HttpResponseHeader.Upgrade ] )
|| !sUpgrade.Equals( whs[ HttpResponseHeader.Connection ] ))
{
throw new FormatException( "Invalid handshake response" );
}
// check SecKey
ssec += sSecSalt;
using (var sha1 = new SHA1Managed())
{
sha1.Initialize();
bsec = sha1.ComputeHash( Encoding.ASCII.GetBytes( ssec ) );
}
var shash = Convert.ToBase64String( bsec );
if (whs[ sSecAccept ] != shash)
throw new FormatException( "Sec-WebSocket-Accept not equals to SHA1-hash" );
// The connection is now in the OPEN state
_state = WebSocketState.Open;
Stream = stream;
beginRecv( stream );
}
示例7: Sha1Sum
/// <summary>
/// Constructor.
/// </summary>
public Sha1Sum()
{
_sha1 = new SHA1Managed();
_sha1.Initialize();
}
示例8: SHA1HashPassword
//===============================================================
// Function: SHA1HashPassword
//===============================================================
private string SHA1HashPassword(string password)
{
SHA1Managed hashProvider;
Byte[] passwordBytes;
//Byte[] hashBytes;
passwordBytes = System.Text.Encoding.Unicode.GetBytes(password);
hashProvider = new SHA1Managed();
hashProvider.Initialize();
passwordBytes = hashProvider.ComputeHash(passwordBytes);
string hashedPassword = Convert.ToBase64String(passwordBytes);
return hashedPassword;
}
示例9: GetSHA1Checksum
private static byte[] GetSHA1Checksum(string fileName)
{
using(var file = new FileStream(fileName, FileMode.Open))
using(var sha = new SHA1Managed())
{
sha.Initialize();
return sha.ComputeHash(file);
}
}
示例10: Authenticator
public Authenticator()
{
sha = new SHA1Managed();
sha.Initialize();
}