mardi 9 août 2016

Java's BigInteger C# in Base 32

How would one convert the following line of Java to C# :

new BigInteger(130, new SecureRandom()).toString(32).replace("/", "w").toUpperCase(Locale.US);

As far as the random BigInteger I have this function:

static BigInteger RandomInteger(int bits)
{
            RNGCryptoServiceProvider secureRandom = new RNGCryptoServiceProvider();
            // make sure there is extra room for a 0-byte so our number isn't negative
            // in the case that the msb is set
            var bytes = new byte[bits / 8 + 1];
            secureRandom.GetBytes(bytes);
            // mask off excess bits
            bytes[bytes.Length - 1] &= (byte)((1 << (bits % 8)) - 1);
            return new BigInteger(bytes);
}

taken from this question : Equivalent of Java's BigInteger in C#

However I'm not sure if that function is correct as well.




Aucun commentaire:

Enregistrer un commentaire