fiatjaf / nos2x

nostr signer extension
278 stars 55 forks source link

Support for master password #36

Open Anderson-Juhasc opened 1 year ago

Anderson-Juhasc commented 1 year ago

Something similar to Password Manager master password.

Code example:

encryptObject(obj, password) {
    try {
      const iv = crypto.randomBytes(16);
      const salt = crypto.randomBytes(64);
      const key = crypto.pbkdf2Sync(password, salt, 100000, 32, "sha512");
      const cipher = crypto.createCipheriv("aes-256-cbc", key, iv);
      let encrypted = cipher.update(JSON.stringify(obj), "utf8", "hex");
      encrypted += cipher.final("hex");
      return {
        iv: iv.toString("hex"),
        encrypted: encrypted,
        salt: salt.toString("hex"),
      };
    } catch (e) {
      return { error: e.reason };
    }
  },

  decryptObject(data, password) {
    try {
      const iv = Buffer.from(data.iv, "hex");
      const salt = Buffer.from(data.salt, "hex");
      const key = crypto.pbkdf2Sync(password, salt, 100000, 32, "sha512");
      const decipher = crypto.createDecipheriv("aes-256-cbc", key, iv);
      let decrypted = decipher.update(data.encrypted, "hex", "utf8");
      decrypted += decipher.final("utf8");
      return JSON.parse(decrypted);
    } catch (e) {
      return { error: e.reason };
    }
  },
fiatjaf commented 1 year ago

This is a good idea.

githubbbie commented 1 year ago

@Anderson-Juhasc did you code it yet? Send a PR :)

amunrarara commented 5 months ago

@Anderson-Juhasc Love this! Hope to see a PR

fiatjaf commented 5 months ago

We'll implement NIP-49 eventually.

Anderson-Juhasc commented 5 months ago

@fiatjaf how could I implement this nip? Have any script ready?