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

crypto.randomBytes() always returns 0 #277

Open
yuetloo opened this issue Jun 28, 2024 · 0 comments
Open

crypto.randomBytes() always returns 0 #277

yuetloo opened this issue Jun 28, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@yuetloo
Copy link

yuetloo commented Jun 28, 2024

Environment

"unenv": 1.9.0
node: v20.11.0

Reproduction

Polyfill a Nuxt app using

// nuxt.config.ts
import { env, nodeless } from 'unenv'
const { alias } = env(nodeless, {})

export default defineNuxtConfig({
  vite: {
    define: {
      global: 'globalThis',
    },
    resolve: {
      alias: {
        'node:buffer': alias['node:buffer'],
        crypto: alias['crypto'],
        stream: alias['stream'],
        http: alias['http'],
        https: alias['https'],
        zlib: alias['zlib'],
      },
    },
  },
})

// app.vue
import { randomBytes } from 'crypto'

const rand = randomBytes(32)
for(const num of rand) {
   console.log(num)
}

Describe the bug

Uint8Array.slice in the following line does not update the bytes buffer, use subArray instead as changes to the new array will update the original array.

https://github.com/unjs/unenv/blob/main/src/runtime/node/crypto/internal/node.ts#L44

    getRandomValues(
      Uint8Array.prototype.slice.call(
        bytes,
        generated,
        generated + MAX_RANDOM_VALUE_BYTES,
      ),
    );

Additional context

No response

Logs

No response

@yuetloo yuetloo added the bug Something isn't working label Jun 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant