本文整理汇总了C#中System.Random.NextBytes方法的典型用法代码示例。如果您正苦于以下问题:C# System.Random.NextBytes方法的具体用法?C# System.Random.NextBytes怎么用?C# System.Random.NextBytes使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Random
的用法示例。
在下文中一共展示了System.Random.NextBytes方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TestRSA
internal void TestRSA()
{
var data = new byte[32];
var r = new Random();
var user = new byte[64];
var pass = new byte[64];
r.NextBytes(user);
r.NextBytes(pass);
var rsaKeyPair = Crypto.DeriveRsaKey(user, pass);
r.NextBytes(data);
data[data.Length-1] = 0;
Assert.AreEqual(data.Length, Crypto.DecryptRsa(Crypto.EncryptRsa(data, rsaKeyPair.Public), rsaKeyPair).Length);
}
示例2: TestRSAEncryption
internal void TestRSAEncryption()
{
var user = new byte[64];
var pass = new byte[64];
var r = new Random();
r.NextBytes(user);
r.NextBytes(pass);
var keyPair = Crypto.DeriveRsaKey(user, pass);
var data = new byte[64];
r.NextBytes(data);
var encrypted = Crypto.EncryptRsa(data, keyPair.Public);
Assert.AreElementsNotEqual(data, encrypted);
var decrypted = Crypto.DecryptRsa(encrypted, keyPair);
Assert.AreElementsEqual(data, decrypted);
}
示例3: SomeInsanityTests
public void SomeInsanityTests() {
byte[] data;
bool got_x;
MemBlock b;
System.Random r = new System.Random();
for(int i = 0; i < 100; i++) {
int size = r.Next(1024);
data = new byte[size];
r.NextBytes(data);
int overshoot = r.Next(1,1024);
got_x = false;
b = null;
try {
//Should throw an exception:
b = MemBlock.Reference(data, 0, size + overshoot);
}
catch {
got_x = true;
}
Assert.IsNull(b, "Reference failure test");
Assert.IsTrue(got_x, "Exception catch test");
overshoot = r.Next(1,1024);
got_x = false;
b = MemBlock.Reference(data);
try {
//Should throw an exception:
byte tmp = b[size + overshoot];
}
catch {
got_x = true;
}
Assert.IsTrue(got_x, "index out of range exception");
got_x = false;
try {
//Should throw an exception:
byte tmp = b[ b.Length ];
}
catch {
got_x = true;
}
Assert.IsTrue(got_x, "index out of range exception");
}
}
示例4: Test
public void Test() {
System.Random r = new System.Random();
byte[] data;
for(int i = 0; i < 100; i++) {
data = new byte[ r.Next(1024) ];
r.NextBytes(data);
int offset = r.Next(data.Length);
MemBlock mb1 = new MemBlock(data, 0, data.Length);
MemBlock mb1a = MemBlock.Copy(data, 0, data.Length);
Assert.AreEqual(mb1, mb1a, "MemBlock.Copy");
Assert.AreEqual(mb1, data, "MemBlock == byte[]");
MemBlock mb2 = new MemBlock(data, offset, data.Length - offset);
MemBlock mb2a = mb1.Slice(offset);
MemBlock mb3 = new MemBlock(data, 0, offset);
MemBlock mb3a = mb1.Slice(0, offset);
Assert.IsTrue(mb3.Equals( mb3a ), "mb3.Equals(mb3a)");
Assert.IsTrue(mb3a.Equals( mb3 ), "mb3a.Equals(mb3)");
Assert.AreEqual(mb3.CompareTo(mb2) + mb2.CompareTo(mb3), 0, "CompareTo");
Assert.IsTrue(mb2.Equals( mb2a ), "mb2.Equals(mb2a)");
Assert.IsTrue(mb2a.Equals( mb2 ), "mb2a.Equals(mb2)");
MemBlock cat = MemBlock.Concat(mb3, mb2);
MemBlock cata = MemBlock.Concat(mb3a, mb2a);
Assert.IsTrue(cat.Equals(cata), "Concat Equals");
Assert.IsTrue(cata.Equals(cat), "Concat a Equals");
Assert.IsTrue(mb1.Equals(cat), "Concat Equals Original");
if( offset != 0 ) {
//These should not be equal
Assert.IsFalse(mb2.Equals(mb1), "mb2 != mb1");
}
int mb2a_l = mb2a.Length;
byte[] tmp_data = new byte[mb2a_l];
mb2a.CopyTo(tmp_data, 0);
MemBlock mb2b = new MemBlock(tmp_data, 0, tmp_data.Length);
Assert.IsTrue(mb2a.Equals(mb2b), "mb2a.Equals(mb2b)");
Assert.IsTrue(mb2b.Equals(mb2a), "mb2b.Equals(mb2a)");
//Check the Hash:
Assert.AreEqual(mb2b.GetHashCode(), mb2a.GetHashCode(), "GetHashCode");
//Here are some manual equality testing using the indexer
bool all_equals = true;
int j = 0;
while( all_equals && (j < mb1.Length) ) {
all_equals = (mb1[ j ] == cat[ j ]);
j++;
}
Assert.IsTrue(all_equals, "Manual equality test mb1");
all_equals = true;
j = 0;
while( all_equals && (j < mb2.Length) ) {
all_equals = (mb2[ j ] == mb2a[ j ]);
j++;
}
Assert.IsTrue(all_equals, "Manual equality test mb2");
all_equals = true;
j = 0;
while( all_equals && (j < mb2.Length) ) {
all_equals = (mb2[ j ] == mb2b[ j ]);
j++;
}
Assert.IsTrue(all_equals, "Manual equality test mb2b");
}
}
示例5: BVT_TunnelSCSIPersistentReserve_Preempt
public void BVT_TunnelSCSIPersistentReserve_Preempt()
{
BaseTestSite.Log.Add(LogEntryKind.TestStep, "1. The first client opens a shared virtual disk file and expects success.");
OpenSharedVHD(TestConfig.NameOfSharedVHDX, RSVD_PROTOCOL_VERSION.RSVD_PROTOCOL_VERSION_1);
BaseTestSite.Log.Add(LogEntryKind.TestStep, "2. The second client opens a shared virtual disk file and expects success.");
OpenSharedVHD(TestConfig.NameOfSharedVHDX, RSVD_PROTOCOL_VERSION.RSVD_PROTOCOL_VERSION_1, 0, true, secondClient, "client02");
System.Random random = new System.Random();
byte[] firstReservationkey = new byte[8];
random.NextBytes(firstReservationkey);
byte[] secondReservationKey = new byte[8];
random.NextBytes(secondReservationKey);
BaseTestSite.Log.Add(LogEntryKind.TestStep,
"3. The first client sends Register service action of SCSI Persistent Reserve Out command by tunnel operation RSVD_TUNNEL_SCSI_OPERATION to server and expects success.");
byte scsiStatus;
SendAndReceiveSCSICommand(
this.client,
10,
CreateCDBBuffer_PersistentReserveOut(PERSISTENT_RESERVE_OUT_SERVICE_ACTION.REGISTER_AND_IGNORE_EXISTING_KEY, 0),
CreateDataBuffer_PersistentReserveOut(0, System.BitConverter.ToUInt64(firstReservationkey, 0)),
out scsiStatus);
VerifyFieldInResponse("SCSIStatus", 0, scsiStatus); // Status code 0 indicates that the device has completed the task successfully.
BaseTestSite.Log.Add(LogEntryKind.TestStep,
"4. The first client sends Reserve service action of SCSI Persistent Reserve Out command by tunnel operation RSVD_TUNNEL_SCSI_OPERATION to server and expects success.");
SendAndReceiveSCSICommand(
this.client,
10,
CreateCDBBuffer_PersistentReserveOut(PERSISTENT_RESERVE_OUT_SERVICE_ACTION.RESERVE, PERSISTENT_RESERVATION_SCOPE_AND_TYPE_CODE.WriteExclusiveRegistrantsOnly),
CreateDataBuffer_PersistentReserveOut(System.BitConverter.ToUInt64(firstReservationkey, 0), 0),
out scsiStatus);
VerifyFieldInResponse("SCSIStatus", 0, scsiStatus); // Status code 0 indicates that the device has completed the task successfully.
BaseTestSite.Log.Add(LogEntryKind.TestStep,
"5. The second client sends Register service action of SCSI Persistent Reserve Out command by tunnel operation RSVD_TUNNEL_SCSI_OPERATION to server and expects success.");
SendAndReceiveSCSICommand(
secondClient,
10,
CreateCDBBuffer_PersistentReserveOut(PERSISTENT_RESERVE_OUT_SERVICE_ACTION.REGISTER_AND_IGNORE_EXISTING_KEY, 0),
CreateDataBuffer_PersistentReserveOut(0, System.BitConverter.ToUInt64(secondReservationKey, 0)),
out scsiStatus);
VerifyFieldInResponse("SCSIStatus", 0, scsiStatus); // Status code 0 indicates that the device has completed the task successfully.
BaseTestSite.Log.Add(LogEntryKind.TestStep,
"6. The second client sends Preempt service action of SCSI Persistent Reserve Out command by tunnel operation RSVD_TUNNEL_SCSI_OPERATION to server and expects success.");
SendAndReceiveSCSICommand(
secondClient,
10,
CreateCDBBuffer_PersistentReserveOut(PERSISTENT_RESERVE_OUT_SERVICE_ACTION.PREEMPT, PERSISTENT_RESERVATION_SCOPE_AND_TYPE_CODE.WriteExclusiveRegistrantsOnly),
CreateDataBuffer_PersistentReserveOut(System.BitConverter.ToUInt64(secondReservationKey, 0), System.BitConverter.ToUInt64(firstReservationkey, 0)),
out scsiStatus);
VerifyFieldInResponse("SCSIStatus", 0, scsiStatus); // Status code 0 indicates that the device has completed the task successfully.
BaseTestSite.Log.Add(LogEntryKind.TestStep, "7. The first client closes the file.");
client.CloseSharedVirtualDisk();
BaseTestSite.Log.Add(LogEntryKind.TestStep, "8. The second client closes the file.");
secondClient.CloseSharedVirtualDisk();
}
示例6: Test
public void Test() {
System.Random r = new System.Random();
for(int i = 0; i < 1024; i++) {
//Test ClassOf and SetClass:
int c = r.Next(160);
byte[] buf0 = new byte[Address.MemSize];
//Fill it with junk
r.NextBytes(buf0);
Address.SetClass(buf0, c);
int c2 = Address.ClassOf(MemBlock.Reference(buf0, 0, Address.MemSize));
Assert.AreEqual(c,c2, "Class Round Trip");
//Test BigInteger stuff:
int size = r.Next(1, MemSize + 1);
byte[] buf1 = new byte[size];
r.NextBytes(buf1);
BigInteger b1 = new BigInteger(buf1);
byte[] buf2 = Address.ConvertToAddressBuffer(b1);
//Check to see if the bytes are equivalent:
int min_len = System.Math.Min(buf1.Length, buf2.Length);
bool all_eq = true;
for(int j = 0; j < min_len; j++) {
all_eq = all_eq
&& (buf2[buf2.Length - j - 1] == buf1[buf1.Length - j - 1]);
}
if( !all_eq ) {
System.Console.Error.WriteLine("Buf1: ");
foreach(byte b in buf1) {
System.Console.Write("{0} ",b);
}
System.Console.Error.WriteLine();
System.Console.Error.WriteLine("Buf2: ");
foreach(byte b in buf2) {
System.Console.Write("{0} ",b);
}
System.Console.Error.WriteLine();
}
Assert.IsTrue(all_eq, "bytes are equivalent");
BigInteger b2 = new BigInteger(buf2);
Assert.AreEqual(b1, b2, "BigInteger round trip");
}
}
示例7: NewGuid
public static Guid NewGuid()
{
byte[] bytes = new byte[16];
System.Random random = new System.Random();
random.NextBytes(bytes);
return new Guid(bytes);
}
示例8: Test
public void Test() {
System.Random r = new System.Random();
//Test numeric type codes:
for(int i = 1; i < 32; i++ ) {
PType p = new PType(i);
MemBlock b = p.ToMemBlock();
byte[] buf = new byte[100];
r.NextBytes(buf); //Get some junk:
MemBlock junk = MemBlock.Reference(buf);
MemBlock b1 = MemBlock.Concat(b, junk);
MemBlock rest = null;
PType pp = PType.Parse(b1, out rest);
byte[] buf2 = new byte[1];
buf2[0] = (byte)i;
MemBlock b2 = MemBlock.Reference(buf2);
Assert.AreEqual(p, pp, System.String.Format("Round trip int: {0}", i));
Assert.AreEqual( b, b2, System.String.Format("Convert to MemBlock int: {0}", i) );
Assert.AreEqual(i, pp.TypeNumber, "Typenumber equality");
Assert.AreEqual(rest, junk, "rest in int PType");
}
//Test string types:
for(int i = 0; i < 1000; i++) {
//Make a random string:
//
byte[] buf = new byte[ r.Next(1, 100) ];
r.NextBytes(buf);
string s = Base32.Encode(buf);
PType p1 = new PType(s);
r.NextBytes(buf); //Get some junk:
MemBlock b = MemBlock.Copy(buf);
MemBlock combine = MemBlock.Concat( p1.ToMemBlock(), b);
MemBlock b2 = null;
PType p2 = PType.Parse(combine, out b2);
Assert.AreEqual( p1, p2, "Round trip string: " + s);
Assert.AreEqual( b, b2, "Round trip rest" );
Assert.AreEqual( s, p2.ToString(), "Round trip to string");
Assert.AreEqual( s, p1.ToString(), "Round trip to string");
Assert.AreEqual( p1.TypeNumber, p2.TypeNumber, "RT: TypeNumber test");
}
//Test all one byte ascii strings:
for(byte b = 32; b < ASCII_UPPER_BOUND; b++) {
MemBlock raw = MemBlock.Reference( new byte[]{ b, 0 } );
MemBlock rest;
PType p1 = PType.Parse(raw, out rest);
Assert.AreEqual(rest, MemBlock.Null, "Rest is null");
PType p2 = PType.Parse(raw, out rest);
Assert.AreEqual(rest, MemBlock.Null, "Rest is null");
Assert.IsTrue(p1 == p2, "reference equality of single byte type");
Assert.AreEqual(p1, p2, "equality of single byte type");
Assert.AreEqual(p1, new PType(p1.ToString()), "Round trip string");
}
//Test TypeNumber of string types:
for(int i = 0; i < 100; i++) {
byte[] buf = new byte[20];
r.NextBytes(buf);
for( int j = 1; j < 4; j++) {
string s = Base32.Encode(buf).Substring(0, j);
PType p1 = new PType(s);
byte[] buf2 = System.Text.Encoding.UTF8.GetBytes(s);
int t = 0;
for(int k = 0; k < buf2.Length; k++) {
t = t | buf2[k];
t <<= 8;
}
Assert.AreEqual(t, p1.TypeNumber, System.String.Format("String type number: {0}, s={1}", t, s) );
}
}
//Console.Error.WriteLine("Tested PType");
}
示例9: TestRSASigning
internal void TestRSASigning()
{
var user = new byte[64];
var pass = new byte[64];
var r = new Random();
r.NextBytes(user);
r.NextBytes(pass);
var keyPair = Crypto.DeriveRsaKey(user, pass);
var data = new byte[64];
r.NextBytes(data);
var signature = Crypto.Sign(data, keyPair);
Assert.IsTrue(Crypto.VerifySignature(data, keyPair.Public, signature));
signature[0] = (byte) ~signature[0];
Assert.IsFalse(Crypto.VerifySignature(data, keyPair.Public, signature));
signature[0] = (byte) ~signature[0];
data[0] = (byte) ~data[0];
Assert.IsFalse(Crypto.VerifySignature(data, keyPair.Public, signature));
}
示例10: TestSend
public void TestSend() {
var fp = FragPipe.Instance;
var head_c = new HeaderChop(new PType("frag").ToMemBlock());
fp.Subscribe(head_c, null);
var fh = new FragmentingHandler(1000);
head_c.Subscribe(fh, null);
var th = new TestHandler();
fh.Subscribe(th, null);
head_c.WithoutHeader.Subscribe(th, null);
var fs = new FragmentingSender(100, fp);
var r = new System.Random();
for(int length = 1; length < 10000; length++) {
var buf = new byte[length];
r.NextBytes(buf);
var dat = MemBlock.Reference(buf);
fs.Send(dat); //This will do the assert.
Assert.AreEqual(dat, th.LastData, "Data was received");
}
}