'Convert this Nodejs Function to C# Function
I have this function in NodeJS and I need some help to convert to C#, my problem is get the 'buf' from randomBytes
NodeJS Function:
function generateKeys() {
return new Promise((resolve, reject) => {
const dh = crypto.createECDH('prime256v1');
dh.generateKeys();
crypto.randomBytes(16, (err, buf) => {
if (err) {
return reject(err);
}
return resolve({
privateKey : escape(dh.getPrivateKey('base64')),
publicKey : escape(dh.getPublicKey('base64')),
authSecret : escape(buf.toString('base64')),
});
});
});
}
C# Code so far:
private static AsymmetricCipherKeyPair GenerateKeyPairEcc()
{
var random = new Org.BouncyCastle.Security.SecureRandom();
var oid = X962NamedCurves.GetOid("prime256v1");
var generator = new ECKeyPairGenerator();
var genParam = new ECKeyGenerationParameters(oid, random);
generator.Init(genParam);
AsymmetricCipherKeyPair ackp = generator.GenerateKeyPair();
return ackp;
}
public byte[] RandomBytes()
{
byte[] random = new byte[16];
RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
rng.GetBytes(random);
return random;
}
Solution 1:[1]
Try
public byte[] RandomBytes()
{
byte[] random = new byte[16];
RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
rng.GetBytes(random);
var bytesToHex = BytesToHex(salt);
return bytesToHex;
}
private static string BytesToHex(byte[] toConvert)
{
var s = new StringBuilder(toConvert.Length * 2);
foreach (var b in toConvert)
{
s.Append(b.ToString("x2"));
}
return s.ToString();
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |