Giter Site home page Giter Site logo

Comments (3)

MistressPlague avatar MistressPlague commented on May 28, 2024

Hi; in debugging it seems it is already disposed when the script's source stack is finished, I will contribute said fix regardless.
As for the latter, I personally don't find it any more understandable for me, but to be fair that is a fairly subjective one.

Thank you for helping out!

from antirip.

w200338 avatar w200338 commented on May 28, 2024

I'll gladly explain the second part. The RNGCryptoServiceProvider is purpose-made for generating cryptographically secure randomness which is used for things like secret keys, salts etc. The code example is definitely not as readable as it could be with some comments. It does the following:

  1. Create an array of four bytes
  2. Pass the array to RNGCryptoServiceProvider to fill it with random bytes
  3. Loop over every bit inside the array of random bytes and fill the _bitArray with them

That last step is done by sliding a bitmask over the every byte and checking if a specific bit is 0 (false) or 1 (true).

  • (1 << j): create bitmask where one bit is 1 and all the rest is 0
  • randomBytes[i] & (1 << j): apply bitmask to one of the random bytes
  • (randomBytes[i] & (1 << j)) != 0: if it's anything but 0 (the bitmask and random byte shared a 1 bit in the same position) it's true
step:       1          2          3          4
random:  01000101   01000101   01000101   01000101
mask:    00000001   00000010   00000100   00001000
result:  00000001   00000000   00000100   00000000
boolean:   true      false       true      false

Looking at it, it may be clearer to write this:

public void GenerateNewKey()
{
    using (var rng = new RNGCryptoServiceProvider())
    {
        byte[] randomBytes = new byte[4];
        rng.GetBytes(randomBytes);
        int randomNumber = BitConverter.ToInt32(randomBytes);

        for (int i = 0; i < 32; i++)
        {
            _bitKeys[i] = ((randomNumber & (1 << i)) != 0);
        }
    }
}

from antirip.

MistressPlague avatar MistressPlague commented on May 28, 2024

Like said before though, this does fall into subjectivity. To me, this makes it more vague, as I find standardized math; even if longer, easier to read than bitshifting. The only type of people who understand bitshifting already would not have issues with the looping alternative. Due to such, this part is wontfix, as it is too subjective overall to take either side objectively, so it falls to my subjective status.

Thank you though.

from antirip.

Related Issues (8)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.