Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//<SNIPPET1>

Check failure on line 1 in snippets/cpp/VS_Snippets_CLR/Cryptography.PasswordDerivedbytes/cpp/sample.cpp

View workflow job for this annotation

GitHub Actions / snippets-build

ERROR: Project missing. A project (and optionally a solution file) must be in this directory or one of the parent directories to validate and build this code.
using namespace System;
using namespace System::Security::Cryptography;
using namespace System::Text;
Expand All @@ -18,12 +18,12 @@
randomBytes = gcnew array <Byte>(1);
}

// Create a new RNGCryptoServiceProvider.
RNGCryptoServiceProvider^ cryptoRNGProvider =
gcnew RNGCryptoServiceProvider();
// Create a new RandomNumberGenerator.
RandomNumberGenerator^ randomNumberGenerator =
RandomNumberGenerator::Create();

// Fill the buffer with random bytes.
cryptoRNGProvider->GetBytes(randomBytes);
randomNumberGenerator->GetBytes(randomBytes);

// return the bytes.
return randomBytes;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

Check failure on line 1 in snippets/cpp/VS_Snippets_CLR/HMACRIPEMD160/CPP/hmacripemd160.cpp

View workflow job for this annotation

GitHub Actions / snippets-build

ERROR: Project missing. A project (and optionally a solution file) must be in this directory or one of the parent directories to validate and build this code.
//<SNIPPET1>
using namespace System;
using namespace System::IO;
Expand Down Expand Up @@ -111,8 +111,7 @@
// secret key shared by sender and receiver.
array<Byte>^secretkey = gcnew array<Byte>(64);

//RNGCryptoServiceProvider is an implementation of a random number generator.
RNGCryptoServiceProvider^ rng = gcnew RNGCryptoServiceProvider;
RandomNumberGenerator^ rng = RandomNumberGenerator::Create();

// The array is now filled with cryptographically strong random bytes.
rng->GetBytes( secretkey );
Expand Down
2 changes: 2 additions & 0 deletions snippets/cpp/VS_Snippets_CLR/HMACRIPEMD160/CPP/makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
System.Security.Cryptography.HMACRIPEMD160.exe: hmacripemd160.cpp
cl /FeSystem.Security.Cryptography.HMACRIPEMD160.exe /clr:pure hmacripemd160.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

Check failure on line 1 in snippets/cpp/VS_Snippets_CLR/HMACSHA256/CPP/hmacsha256.cpp

View workflow job for this annotation

GitHub Actions / snippets-build

ERROR: Project missing. A project (and optionally a solution file) must be in this directory or one of the parent directories to validate and build this code.
//<SNIPPET1>
using namespace System;
using namespace System::IO;
Expand Down Expand Up @@ -111,8 +111,7 @@
// secret key shared by sender and receiver.
array<Byte>^secretkey = gcnew array<Byte>(64);

//RNGCryptoServiceProvider is an implementation of a random number generator.
RNGCryptoServiceProvider^ rng = gcnew RNGCryptoServiceProvider;
RandomNumberGenerator^ rng = RandomNumberGenerator::Create();

// The array is now filled with cryptographically strong random bytes.
rng->GetBytes( secretkey );
Expand Down
2 changes: 2 additions & 0 deletions snippets/cpp/VS_Snippets_CLR/HMACSHA256/CPP/makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
System.Security.Cryptography.HMACSHA256.exe: hmacsha256.cpp
cl /FeSystem.Security.Cryptography.HMACSHA256.exe /clr:pure hmacsha256.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

Check failure on line 1 in snippets/cpp/VS_Snippets_CLR/HMACSHA384/CPP/hmacsha384.cpp

View workflow job for this annotation

GitHub Actions / snippets-build

ERROR: Project missing. A project (and optionally a solution file) must be in this directory or one of the parent directories to validate and build this code.
//<SNIPPET1>
using namespace System;
using namespace System::IO;
Expand Down Expand Up @@ -111,8 +111,7 @@
// secret key shared by sender and receiver.
array<Byte>^secretkey = gcnew array<Byte>(64);

//RNGCryptoServiceProvider is an implementation of a random number generator.
RNGCryptoServiceProvider^ rng = gcnew RNGCryptoServiceProvider;
RandomNumberGenerator^ rng = RandomNumberGenerator::Create();

// The array is now filled with cryptographically strong random bytes.
rng->GetBytes( secretkey );
Expand Down
2 changes: 2 additions & 0 deletions snippets/cpp/VS_Snippets_CLR/HMACSHA384/CPP/makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
System.Security.Cryptography.HMACSHA384.exe: hmacsha384.cpp
cl /FeSystem.Security.Cryptography.HMACSHA384.exe /clr:pure hmacsha384.cpp
5 changes: 2 additions & 3 deletions snippets/cpp/VS_Snippets_CLR/HMACSHA512/CPP/hmacsha512.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

Check failure on line 1 in snippets/cpp/VS_Snippets_CLR/HMACSHA512/CPP/hmacsha512.cpp

View workflow job for this annotation

GitHub Actions / snippets-build

ERROR: Project missing. A project (and optionally a solution file) must be in this directory or one of the parent directories to validate and build this code.
//<SNIPPET1>
using namespace System;
using namespace System::IO;
Expand Down Expand Up @@ -110,9 +110,8 @@
// Create a random key using a random number generator. This would be the
// secret key shared by sender and receiver.
array<Byte>^secretkey = gcnew array<Byte>(64);

//RNGCryptoServiceProvider is an implementation of a random number generator.
RNGCryptoServiceProvider^ rng = gcnew RNGCryptoServiceProvider;

RandomNumberGenerator^ rng = RandomNumberGenerator::Create();

// The array is now filled with cryptographically strong random bytes.
rng->GetBytes( secretkey );
Expand Down
2 changes: 2 additions & 0 deletions snippets/cpp/VS_Snippets_CLR/HMACSHA512/CPP/makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
System.Security.Cryptography.HMACSHA512.exe: hmacsha512.cpp
cl /FeSystem.Security.Cryptography.HMACSHA512.exe /clr:pure hmacsha512.cpp
4 changes: 2 additions & 2 deletions snippets/cpp/VS_Snippets_CLR/rfc28981/CPP/rfc28981.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

Check failure on line 1 in snippets/cpp/VS_Snippets_CLR/rfc28981/CPP/rfc28981.cpp

View workflow job for this annotation

GitHub Actions / snippets-build

ERROR: Project missing. A project (and optionally a solution file) must be in this directory or one of the parent directories to validate and build this code.
//<SNIPPET1>
using namespace System;
using namespace System::IO;
Expand Down Expand Up @@ -27,8 +27,8 @@
String^ pwd1 = passwordargs[ 1 ];

array<Byte>^salt1 = gcnew array<Byte>(8);
RNGCryptoServiceProvider ^ rngCsp = gcnew RNGCryptoServiceProvider();
rngCsp->GetBytes(salt1);
RandomNumberGenerator^ rng = RandomNumberGenerator::Create();
rng->GetBytes(salt1);
//data1 can be a string or contents of a file.
String^ data1 = "Some test data";

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#using <System.dll>

Check failure on line 1 in snippets/cpp/VS_Snippets_CLR_Classic/classic RandomNumberGenerator.GetBytes Example/CPP/source.cpp

View workflow job for this annotation

GitHub Actions / snippets-build

ERROR: Project missing. A project (and optionally a solution file) must be in this directory or one of the parent directories to validate and build this code.
#using <System.Data.dll>
#using <System.Drawing.dll>
#using <System.Windows.Forms.dll>
Expand All @@ -16,8 +16,7 @@
// <Snippet1>
array<Byte>^ random = gcnew array<Byte>(100);

//RNGCryptoServiceProvider is an implementation of a random number generator.
RNGCryptoServiceProvider^ rng = gcnew RNGCryptoServiceProvider;
RandomNumberGenerator^ rng = RandomNumberGenerator::Create();
rng->GetBytes( random ); // The array is now filled with cryptographically strong random bytes.
// </Snippet1>
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#using <System.dll>

Check failure on line 1 in snippets/cpp/VS_Snippets_CLR_Classic/classic RandomNumberGenerator.GetNonZeroBytes Example/CPP/source.cpp

View workflow job for this annotation

GitHub Actions / snippets-build

ERROR: Project missing. A project (and optionally a solution file) must be in this directory or one of the parent directories to validate and build this code.
#using <System.Data.dll>
#using <System.Drawing.dll>
#using <System.Windows.Forms.dll>
Expand All @@ -15,8 +15,7 @@
{
// <Snippet1>
array<Byte>^ random = gcnew array<Byte>(100);
//RNGCryptoServiceProvider is an implementation of a random number generator.
RNGCryptoServiceProvider^ rng = gcnew RNGCryptoServiceProvider;
RandomNumberGenerator^ rng = RandomNumberGenerator::Create();
rng->GetNonZeroBytes( random ); // The array is now filled with cryptographically strong random bytes, and none are zero.
// </Snippet1>
}
Expand Down
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@adegeo Why does Snippets 5000 care about this file given that it's configured to ignore .cpp files? See

ExtensionsCodeTriggers: .cs;.vb;.fs;.xaml;.razor;.cshtml;.vbhtml;.sln;.csproj;.fsproj;.vbproj;.proj
.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// This sample demonstrates how to encode and decode a string using

Check failure on line 1 in snippets/cpp/VS_Snippets_CLR_System/system.Security.Cryptography.RSAOAEPKeyExchangeDeformatter/CPP/rsaencoder.cpp

View workflow job for this annotation

GitHub Actions / snippets-build

ERROR: Project missing. A project (and optionally a solution file) must be in this directory or one of the parent directories to validate and build this code.
// the RSAOAEPKeyExchangeFormatter and RSAOAEPKeyExchangeDeformatter classes.
//<Snippet2>
using namespace System;
Expand Down Expand Up @@ -122,7 +122,7 @@

// Create a random number using the RNGCryptoServiceProvider provider.
//<Snippet6>
RNGCryptoServiceProvider^ ring = gcnew RNGCryptoServiceProvider;
RandomNumberGenerator^ ring = RandomNumberGenerator::Create();
rsaFormatter->Rng = ring;
//</Snippet6>

Expand Down Expand Up @@ -190,7 +190,7 @@
//
// Encoding the following message:
// A phrase to be encoded.
// Resulting message encoded: %?}T:v??xu?eD)YucItjwu¦ALH HB,Uj??2xq?.?s45
// Resulting message encoded: %?}T:v??xu?eD)YucItjwu¦ALH HB,Uj??2xq?.?s45
// ?f?L2?=X?CPzWx???"q5?6&N"AE,Z+T?(]S?_7~,?G{?VV!:S?df?
// Resulting message decoded:
// A phrase to be encoded.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ public static void Main(string[] Fileargs)
// Create a random key using a random number generator. This would be the
// secret key shared by sender and receiver.
byte[] secretkey = new Byte[64];
//RNGCryptoServiceProvider is an implementation of a random number generator.
using (RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider())

using (RandomNumberGenerator rng = RandomNumberGenerator.Create())
{
// The array is now filled with cryptographically strong random bytes.
rng.GetBytes(secretkey);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ public static void Main(string[] Fileargs)
// Create a random key using a random number generator. This would be the
// secret key shared by sender and receiver.
byte[] secretkey = new Byte[64];
//RNGCryptoServiceProvider is an implementation of a random number generator.
using (RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider())

using (RandomNumberGenerator rng = RandomNumberGenerator.Create())
{
// The array is now filled with cryptographically strong random bytes.
rng.GetBytes(secretkey);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ public static void Main(string[] Fileargs)
// Create a random key using a random number generator. This would be the
// secret key shared by sender and receiver.
byte[] secretkey = new Byte[64];
//RNGCryptoServiceProvider is an implementation of a random number generator.
using (RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider())

using (RandomNumberGenerator rng = RandomNumberGenerator.Create())
{
// The array is now filled with cryptographically strong random bytes.
rng.GetBytes(secretkey);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ public static void Main(string[] Fileargs)
// Create a random key using a random number generator. This would be the
// secret key shared by sender and receiver.
byte[] secretkey = new Byte[64];
//RNGCryptoServiceProvider is an implementation of a random number generator.
using (RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider())

using (RandomNumberGenerator rng = RandomNumberGenerator.Create())
{
// The array is now filled with cryptographically strong random bytes.
rng.GetBytes(secretkey);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ public static void Main(string[] Fileargs)
// Create a random key using a random number generator. This would be the
// secret key shared by sender and receiver.
byte[] secretkey = new Byte[24];
//RNGCryptoServiceProvider is an implementation of a random number generator.
using (RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider())

using (RandomNumberGenerator rng = RandomNumberGenerator.Create())
{
// The array is now filled with cryptographically strong random bytes.
rng.GetBytes(secretkey);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,11 @@ public static byte[] CreateRandomSalt(int length)
randBytes = new byte[1];
}

// Create a new RNGCryptoServiceProvider.
RNGCryptoServiceProvider rand = new RNGCryptoServiceProvider();

// Fill the buffer with random bytes.
rand.GetBytes(randBytes);
using (RandomNumberGenerator rng = RandomNumberGenerator.Create())
{
// Fill the buffer with random bytes.
rng.GetBytes(randBytes);
}

// return the bytes.
return randBytes;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,9 @@ private void ConstructFormatter()
rsaFormatter.SetKey(key);
//</Snippet5>

// Create a random number using the RNGCryptoServiceProvider provider.
// Create a random number using the RandomNumberGenerator
//<Snippet6>
RNGCryptoServiceProvider ring = new RNGCryptoServiceProvider();
RandomNumberGenerator ring = RandomNumberGenerator.Create();
rsaFormatter.Rng = ring;
//</Snippet6>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
using System;
using System.Data;
using System.Security.Cryptography ;
using System.Security.Cryptography;
using System.Windows.Forms;

public class Form1: Form
{
public static void Main()
{
// <Snippet1>
byte[] random = new Byte[100];
// <Snippet1>
byte[] random = new byte[100];

//RNGCryptoServiceProvider is an implementation of a random number generator.
RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
rng.GetBytes(random); // The array is now filled with cryptographically strong random bytes.
// </Snippet1>
using (RandomNumberGenerator rng = RandomNumberGenerator.Create())
{
rng.GetBytes(random); // The array is now filled with cryptographically strong random bytes.
}
// </Snippet1>
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ public class Form1: Form
public void Method()
{
// <Snippet1>
byte[] random = new Byte[100];
//RNGCryptoServiceProvider is an implementation of a random number generator.
RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
rng.GetNonZeroBytes(random); // The array is now filled with cryptographically strong random bytes, and none are zero.
byte[] random = new byte[100];

using (RandomNumberGenerator rng = RandomNumberGenerator.Create())
{
rng.GetNonZeroBytes(random); // The array is now filled with cryptographically strong random bytes, and none are zero.
}
// </Snippet1>
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,10 @@ public static void Main(string[] passwordargs)
string pwd1 = passwordargs[0];
// Create a byte array to hold the random value.
byte[] salt1 = new byte[8];
using (RNGCryptoServiceProvider rngCsp = new
RNGCryptoServiceProvider())
using (RandomNumberGenerator rng = RandomNumberGenerator.Create())
{
// Fill the array with a random value.
rngCsp.GetBytes(salt1);
rng.GetBytes(salt1);
}

//data1 can be a string or contents of a file.
Expand Down
22 changes: 16 additions & 6 deletions snippets/csharp/VS_Snippets_CFX/samlattribute/cs/source.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//-----------------------------------------------------------------------------

Check failure on line 1 in snippets/csharp/VS_Snippets_CFX/samlattribute/cs/source.cs

View workflow job for this annotation

GitHub Actions / snippets-build

ERROR: Project missing. A project (and optionally a solution file) must be in this directory or one of the parent directories to validate and build this code.
// Copyright (c) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
using System;
Expand Down Expand Up @@ -323,9 +323,13 @@
{
// Create an array to store the key bytes.
byte[] key = new byte[keySize/8];

// Create some random bytes.
RNGCryptoServiceProvider random = new RNGCryptoServiceProvider();
random.GetNonZeroBytes(key);
using (RandomNumberGenerator random = RandomNumberGenerator.Create())
{
random.GetNonZeroBytes(key);
}

// Create a BinarySecretSecurityToken from the random bytes and return it.
return new BinarySecretSecurityToken(key);
}
Expand Down Expand Up @@ -409,19 +413,25 @@
{
// Create an array to store the entropy bytes.
stsEntropy = new byte[keySize / 8];

// Create some random bytes.
RNGCryptoServiceProvider random = new RNGCryptoServiceProvider();
random.GetNonZeroBytes(stsEntropy);
using (RandomNumberGenerator random = RandomNumberGenerator.Create())
{
random.GetNonZeroBytes(stsEntropy);
}
// Compute the combined key.
key = RequestSecurityTokenResponse.ComputeCombinedKey(senderEntropy, stsEntropy, keySize);
}
else // Issuer entropy only...
{
// Create an array to store the entropy bytes.
key = new byte[keySize / 8];

// Create some random bytes.
RNGCryptoServiceProvider random = new RNGCryptoServiceProvider();
random.GetNonZeroBytes(key);
using (RandomNumberGenerator random = RandomNumberGenerator.Create())
{
random.GetNonZeroBytes(key);
}
}

// Create a BinarySecretSecurityToken to be the proof token, based on the key material
Expand Down
20 changes: 14 additions & 6 deletions snippets/visualbasic/VS_Snippets_CFX/samlattribute/vb/source.vb
Original file line number Diff line number Diff line change
Expand Up @@ -320,9 +320,12 @@ Namespace Microsoft.ServiceModel.Samples.Federation
Protected Shared Function CreateProofToken(ByVal keySize As Integer) As BinarySecretSecurityToken
' Create an array to store the key bytes.
Dim key(keySize / 8 - 1) As Byte

' Create some random bytes.
Dim random As New RNGCryptoServiceProvider()
random.GetNonZeroBytes(key)
Using random As RandomNumberGenerator = RandomNumberGenerator.Create()
random.GetNonZeroBytes(key)
End Using

' Create a BinarySecretSecurityToken from the random bytes and return it.
Return New BinarySecretSecurityToken(key)
End Function
Expand Down Expand Up @@ -398,18 +401,23 @@ Namespace Microsoft.ServiceModel.Samples.Federation
If Not (senderEntropy Is Nothing) Then
' Create an array to store the entropy bytes.
stsEntropy = New Byte(keySize / 8) {}

' Create some random bytes.
Dim random As New RNGCryptoServiceProvider()
random.GetNonZeroBytes(stsEntropy)
Using random As RandomNumberGenerator = RandomNumberGenerator.Create()
random.GetNonZeroBytes(stsEntropy)
End Using

' Compute the combined key.
key = RequestSecurityTokenResponse.ComputeCombinedKey(senderEntropy, stsEntropy, keySize)
' Issuer entropy only...
Else
' Create an array to store the entropy bytes.
key = New Byte(keySize / 8) {}

' Create some random bytes.
Dim random As New RNGCryptoServiceProvider()
random.GetNonZeroBytes(key)
Using random As RandomNumberGenerator = RandomNumberGenerator.Create()
random.GetNonZeroBytes(key)
End Using
End If

' Create a BinarySecretSecurityToken to be the proof token, based on the key material
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,11 @@ Module PasswordDerivedBytesExample
randBytes = New Byte(0) {}
End If

' Create a new RNGCryptoServiceProvider.
Dim rand As New RNGCryptoServiceProvider()

' Fill the buffer with random bytes.
rand.GetBytes(randBytes)
' Create a new RandomNumberGenerator.
Using rand As RandomNumberGenerator = RandomNumberGenerator.Create()
' Fill the buffer with random bytes.
rand.GetBytes(randBytes)
End Using

' return the bytes.
Return randBytes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ Public Class HMACRIPEMD160example
' Create a random key using a random number generator. This would be the
' secret key shared by sender and receiver.
Dim secretkey() As Byte = New [Byte](63) {}
'RNGCryptoServiceProvider is an implementation of a random number generator.
Using rng As New RNGCryptoServiceProvider()

Using rng As RandomNumberGenerator = RandomNumberGenerator.Create()
' The array is now filled with cryptographically strong random bytes.
rng.GetBytes(secretkey)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ Public Class HMACSHA256example
' Create a random key using a random number generator. This would be the
' secret key shared by sender and receiver.
Dim secretkey() As Byte = New [Byte](63) {}
'RNGCryptoServiceProvider is an implementation of a random number generator.
Using rng As New RNGCryptoServiceProvider()

Using rng As RandomNumberGenerator = RandomNumberGenerator.Create()
' The array is now filled with cryptographically strong random bytes.
rng.GetBytes(secretkey)

Expand Down
Loading