PeculiarVentures / x509

@peculiar/x509 is an easy to use TypeScript/Javascript library based on @peculiar/asn1-schema that makes generating X.509 Certificates and Certificate Requests as well as validating certificate chains easy
https://peculiarventures.github.io/x509/
MIT License
78 stars 10 forks source link

ReferenceError: Buffer is not defined #64

Closed ikreb7 closed 9 months ago

ikreb7 commented 10 months ago

Hello,

I get this error message ReferenceError: Buffer is not <anonymous> webcrypto.es.js:244. This is my example code.

import * as x509 from "@peculiar/x509";
import { Crypto } from "@peculiar/webcrypto";

const crypto = new Crypto();
x509.cryptoProvider.set(crypto);

const algorithm = {
  name: "RSASSA-PKCS1-v1_5",
  hash: "SHA-256",
  publicExponent: new Uint8Array([1, 0, 1]),
  modulusLength: 2048,
 };
 const keys = crypto.subtle.generateKey(algorithm, false, ["sign", "verify"]);
 console.log(keys)

This are my package.json file

 "dependencies": {
   "@peculiar/webcrypto": "^1.4.3",
   "@peculiar/x509": "^1.9.5",
   "vue": "^3.3.4",
   ...
   },
   "devDependencies": {
     "vite": "4.4.9",
   },

The error is already thrown with the line const crypto = new Crypto();.

microshine commented 10 months ago

@ikreb7 Looks like you are using @peculiar/webcrypto in browser. You should use native crypto in browser, because @peculiar/webcrypto is WebCrypto API implementation that uses NodeJS Crypto API and it means it uses NodeJS objects like Buffer.

Try something like this

import * as x509 from "@peculiar/x509";

x509.cryptoProvider.set(self.crypto); // or crypto
ikreb7 commented 9 months ago

Thank you! My fault.