本文整理汇总了C#中Message.VerifySignature方法的典型用法代码示例。如果您正苦于以下问题:C# Message.VerifySignature方法的具体用法?C# Message.VerifySignature怎么用?C# Message.VerifySignature使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Message
的用法示例。
在下文中一共展示了Message.VerifySignature方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: DownloadBytesRequest
private Message DownloadBytesRequest(Message req)
{
string location = req.ReadString(1024);
long position = req.ReadInt64();
int count = req.ReadInt32();
req.VerifySignature(_clientKey);
Check.Assert<InvalidDataException>(location == ReadState(req.TransferId, "location"));
long totalSize = long.Parse(ReadState(req.TransferId, "total-length"));
Check.InRange(position, 0, totalSize);
Check.InRange(position + count, 0, totalSize);
Message response = new Message(TransferState.DownloadBytesResponse, req.TransferId, _clientKey, SessionKey);
using (MemoryStream ms = new MemoryStream())
{
long length;
OnDownloadBytes(req.TransferId, location, out length, position, count, ms);
Check.Assert<InvalidDataException>(length == totalSize);
Check.Assert<InvalidDataException>(ms.Position == count);
response.Write(ms.ToArray());
}
return response;
}
示例2: DownloadCompleteRequest
private Message DownloadCompleteRequest(Message req)
{
req.VerifySignature(_clientKey);
Delete(req.TransferId);
return Message.EmptyMessage;
}
示例3: CompleteRequest
private Message CompleteRequest(Message req)
{
try
{
string name = req.ReadString(1024);
Hash contentHash = Hash.FromBytes(req.ReadBytes(32));
req.VerifySignature(_clientKey);
long totalSize = long.Parse(ReadState(req.TransferId, "total-length"));
string location = ReadState(req.TransferId, "location");
Check.Assert<InvalidDataException>(location == name);
OnCompleteTransfer(req.TransferId, location, totalSize, contentHash);
Message response = new Message(TransferState.UploadCompleteResponse, req.TransferId, _clientKey, SessionKey);
return response;
}
finally
{
Delete(req.TransferId);
}
}
示例4: DownloadRequest
private Message DownloadRequest(Message req)
{
Check.Assert<InvalidDataException>(HasState(req.TransferId, "start-time")
&& HasState(req.TransferId, "nonce")
&& HasState(req.TransferId, "server-key")
&& !HasState(req.TransferId, "session-key"));
byte[] nonceExpected = Convert.FromBase64String(ReadState(req.TransferId, "nonce"));
DeleteState(req.TransferId, "nonce");
byte[] serverKeyBits = Convert.FromBase64String(ReadState(req.TransferId, "server-key"));
DeleteState(req.TransferId, "server-key");
byte[] hnonce = req.ReadBytes(32);
string name = req.ReadString(1024);
byte[] clientKeyBits = req.ReadBytes(32);
req.VerifySignature(_clientKey);
Check.Assert<InvalidDataException>(
Hash.SHA256(nonceExpected).Equals(Hash.FromBytes(hnonce))
);
Salt sessionKey = SessionSecret(clientKeyBits, serverKeyBits);
long length;
OnDownloadBytes(req.TransferId, name, out length, 0, 0, Stream.Null);
Message response = new Message(TransferState.DownloadResponse, req.TransferId, _clientKey, s => sessionKey);
response.Write(MaxOutboundFileChunk);
response.Write(length);
if(length <= MaxOutboundFileChunk)
{
Delete(req.TransferId);
using (MemoryStream ms = new MemoryStream())
{
OnDownloadBytes(req.TransferId, name, out length, 0, (int)length, ms);
Check.Assert<InvalidDataException>(ms.Position == length);
response.Write(ms.ToArray());
}
}
else
{
WriteState(req.TransferId, "total-length", length.ToString());
WriteState(req.TransferId, "location", name ?? String.Empty);
WriteState(req.TransferId, "session-key", sessionKey.ToString());
response.Write(new byte[0]);
}
return response;
}
示例5: TransferRequest
private Message TransferRequest(Message req)
{
Check.Assert<InvalidDataException>(HasState(req.TransferId, "start-time")
&& HasState(req.TransferId, "nonce")
&& HasState(req.TransferId, "server-key")
&& !HasState(req.TransferId, "session-key"));
byte[] nonceExpected = Convert.FromBase64String(ReadState(req.TransferId, "nonce"));
DeleteState(req.TransferId, "nonce");
byte[] serverKeyBits = Convert.FromBase64String(ReadState(req.TransferId, "server-key"));
DeleteState(req.TransferId, "server-key");
byte[] hnonce = req.ReadBytes(32);
long length = req.ReadInt64();
string name = req.ReadString(1024);
byte[] clientKeyBits = req.ReadBytes(32);
req.VerifySignature(_clientKey);
Check.Assert<InvalidDataException>(
Hash.SHA256(nonceExpected).Equals(Hash.FromBytes(hnonce))
);
OnBeginTransfer(req.TransferId, name, length);
WriteState(req.TransferId, "total-length", length.ToString());
WriteState(req.TransferId, "location", name ?? String.Empty);
WriteState(req.TransferId, "session-key", SessionSecret(clientKeyBits, serverKeyBits).ToString());
Message response = new Message(TransferState.UploadResponse, req.TransferId, _clientKey, SessionKey);
response.Write(MaxInboundFileChunk);
return response;
}
示例6: SendBytesRequest
private Message SendBytesRequest(Message req)
{
long position = req.ReadInt64();
byte[] bytes = req.ReadBytes(MaxInboundFileChunk);
req.VerifySignature(_clientKey);
string location = ReadState(req.TransferId, "location");
long totalSize = long.Parse(ReadState(req.TransferId, "total-length"));
Check.InRange(position, 0, totalSize - bytes.Length);
OnBytesReceived(req.TransferId, location, totalSize, position, bytes);
Message response = new Message(TransferState.SendBytesResponse, req.TransferId, _clientKey, SessionKey);
response.Write(position);
return response;
}
示例7: CompleteUpload
private void CompleteUpload(Guid transferId, Salt sessionKey, string location, Hash fullHash)
{
// STEP 4: Finalize the transfer
using (Message req = new Message(TransferState.UploadCompleteRequest, transferId, _publicKey, s => sessionKey))
{
req.Write(location);
req.Write(fullHash.ToArray());
Stream response = SendPayload(req, location, req.ToStream(_privateKey));
using (Message rsp = new Message(response, _privateKey, s => sessionKey))
{
Check.Assert<InvalidOperationException>(rsp.State == TransferState.UploadCompleteResponse);
rsp.VerifySignature(_publicKey);
}
}
}
示例8: NonceRequest
private Message NonceRequest(Message req)
{
req.VerifySignature(_clientKey);
Check.Assert<InvalidDataException>(HasState(req.TransferId, "start-time") == false);
WriteState(req.TransferId, "start-time", DateTime.UtcNow.Ticks.ToString());
byte[] nonce = new byte[NonceSize];
new Random().NextBytes(nonce);
WriteState(req.TransferId, "nonce", Convert.ToBase64String(nonce));
byte[] keydata = new byte[KeyBytes];
new System.Security.Cryptography.RNGCryptoServiceProvider().GetBytes(keydata);
WriteState(req.TransferId, "server-key", Convert.ToBase64String(keydata));
Message response = new Message(TransferState.NonceResponse, req.TransferId, _clientKey, NoSession);
response.Write(nonce);
response.Write(keydata);
return response;
}
示例9: ReadByteRange
private void ReadByteRange(Guid transferId, Salt sessionKey, string location, StreamCache streams, long offset, int count)
{
using (Message req = new Message(TransferState.DownloadBytesRequest, transferId, _publicKey, s=>sessionKey))
{
req.Write(location);
req.Write(offset);
req.Write(count);
Stream response = SendPayload(req, location, req.ToStream(_privateKey));
using (Message rsp = new Message(response, _privateKey, s=>sessionKey))
{
Check.Assert<InvalidOperationException>(rsp.State == TransferState.DownloadBytesResponse);
byte[] bytes = rsp.ReadBytes(100 * 1000 * 1024);
Check.Assert<InvalidOperationException>(bytes.Length == count);
rsp.VerifySignature(_publicKey);
using(Stream io = streams.Open(FileAccess.Write))
{
io.Seek(offset, SeekOrigin.Begin);
io.Write(bytes, 0, count);
}
}
}
}
示例10: TransferBytes
private void TransferBytes(Guid transferId, Salt sessionKey, string location, long offset, byte[] bytes)
{
// STEP 3...n: Send a block of bytes
using (Message req = new Message(TransferState.SendBytesRequest, transferId, _publicKey, s => sessionKey))
{
req.Write(offset);
req.Write(bytes);
Stream response = SendPayload(req, location, req.ToStream(_privateKey));
using (Message rsp = new Message(response, _privateKey, s => sessionKey))
{
Check.Assert<InvalidOperationException>(rsp.State == TransferState.SendBytesResponse);
Check.Assert<InvalidOperationException>(rsp.ReadInt64() == offset);
rsp.VerifySignature(_publicKey);
}
}
}
示例11: BeginUpload
private Salt BeginUpload(Guid transferId, string location, long length, out int maxMessageLength)
{
byte[] serverKeyBits;
byte[] nonce = GetNonce(transferId, location, out serverKeyBits);
Hash hnonce = Hash.SHA256(nonce);
//STEP 2: Create and send session key
Salt clientKeyBits = new Salt(Salt.Size.b256);
Salt sessionSecret = SessionSecret(clientKeyBits, serverKeyBits);
using (Message req = new Message(TransferState.UploadRequest, transferId, _publicKey, NoSession))
{
req.Write(hnonce.ToArray());
req.Write(length);
req.Write(location);
req.Write(clientKeyBits.ToArray());
Stream response = SendPayload(req, location, req.ToStream(_privateKey));
using (Message rsp = new Message(response, _privateKey, s=>sessionSecret))
{
Check.Assert<InvalidOperationException>(rsp.State == TransferState.UploadResponse);
maxMessageLength = Check.InRange(rsp.ReadInt32(), 0, int.MaxValue);
rsp.VerifySignature(_publicKey);
}
}
return sessionSecret;
}
示例12: Download
private bool Download(string location, StreamCache output)
{
int maxMessageLength;
long fileLength, bytesReceived = 0;
Guid transferId = Guid.NewGuid();
byte[] serverKeyBits;
byte[] nonce = GetNonce(transferId, location, out serverKeyBits);
Hash hnonce = Hash.SHA256(nonce);
//STEP 2: Create and send session key
Salt clientKeyBits = new Salt(Salt.Size.b256);
Salt sessionKey = SessionSecret(clientKeyBits, serverKeyBits);
using (Message req = new Message(TransferState.DownloadRequest, transferId, _publicKey, NoSession))
{
req.Write(hnonce.ToArray());
req.Write(location);
req.Write(clientKeyBits.ToArray());
Stream response = SendPayload(req, location, req.ToStream(_privateKey));
using (Message rsp = new Message(response, _privateKey, s=>sessionKey))
{
Check.Assert<InvalidOperationException>(rsp.State == TransferState.DownloadResponse);
maxMessageLength = Check.InRange(rsp.ReadInt32(), 0, int.MaxValue);
fileLength = Check.InRange(rsp.ReadInt64(), 0, 0x00FFFFFFFFFFFFFFL);
byte[] bytes = rsp.ReadBytes(100 * 1000 * 1024);
rsp.VerifySignature(_publicKey);
using(Stream io = output.Open(FileAccess.Write))
{
io.SetLength(fileLength);
if (bytes.Length > 0)
{
io.Seek(0, SeekOrigin.Begin);
io.Write(bytes, 0, bytes.Length);
bytesReceived += bytes.Length;
}
}
}
}
//STEP 3...n: Continue downloading other chunks of the file
if (bytesReceived < fileLength)
{
bool[] failed = new bool[1];
using (WorkQueue queue = new WorkQueue(LimitThreads))
{
queue.OnError += (o, e) => { failed[0] = true; };
while (bytesReceived < fileLength && !failed[0] && !_abort.WaitOne(0, false))
{
int len = (int) Math.Min(fileLength - bytesReceived, maxMessageLength);
BytesToRead task = new BytesToRead(
this, LimitThreads, transferId, sessionKey, location, output, bytesReceived, len);
queue.Enqueue(task.Send);
OnProgressChanged(location, bytesReceived, fileLength);
bytesReceived += len;
}
queue.Complete(true, failed[0] ? 5000 : 7200000);
}
if (_abort.WaitOne(0, false))
return false;
Check.Assert<InvalidDataException>(failed[0] == false);
// STEP 4: Complete the transfer
using (Message req = new Message(TransferState.DownloadCompleteRequest, transferId, _publicKey, s => sessionKey))
{
SendPayload(req, location, req.ToStream(_privateKey)).Dispose();
}
}
OnProgressChanged(location, fileLength, fileLength);
return true;
}
示例13: GetNonce
private byte[] GetNonce(Guid transferId, string location, out byte[] serverKeyBits)
{
byte[] nonce;
// STEP 1: Send a NonceRequest
using (Message req = new Message(TransferState.NonceRequest, transferId, _publicKey, NoSession))
{
Stream response = SendPayload(req, location, req.ToStream(_privateKey));
using (Message rsp = new Message(response, _privateKey, NoSession))
{
Check.Assert<InvalidOperationException>(rsp.State == TransferState.NonceResponse);
nonce = rsp.ReadBytes(1024);
serverKeyBits = rsp.ReadBytes(1024);
rsp.VerifySignature(_publicKey);
}
}
return nonce;
}