Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Should provide method to generate IV #94

Open
bishopjun11 opened this issue Mar 2, 2020 · 2 comments
Open

Should provide method to generate IV #94

bishopjun11 opened this issue Mar 2, 2020 · 2 comments

Comments

@bishopjun11
Copy link

The IV should be random and unique for every run of your encryption method.
so best to provide a method to generate IV.
maybe anther method to generate Key.

@webdevelopland
Copy link

webdevelopland commented Apr 16, 2022

Here you go:
256 key and 128 iv

function getRandomKey(): Uint8Array {
  return getRandomBlock(32);
}

function getIV(): Uint8Array {
  return getRandomBlock(16);
}

function getRandomBlock(size: number): Uint8Array {
  const block: number[] = [];
  for (let i = 0; i < size; i++) {
    block.push(Math.floor(Math.random() * 256));
  }
  return new Uint8Array(block);
}

@ben-ekw
Copy link

ben-ekw commented May 24, 2023

Thanks @webdevelopland! Here's the JS equivalent for those who need it:

function getRandomKey() {
    return getRandomBlock(32);
  }

  function getRandomIV() {
    return getRandomBlock(16);
  }

  function getRandomBlock(size) {
    const block = [];
    for (let i = 0; i < size; i++) {
      block.push(Math.floor(Math.random() * 256));
    }
    return new Uint8Array(block);
  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants