0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

NeDB でドキュメントの暗号化

Posted at

afterSerialization と beforeDeserialization オプションを追加する。

import Datastore from 'nedb'
import crypto from 'crypto'
import path from 'path'

let password = 'hogehoge'

const db = new Datastore({
  // 引数で受け取った文字列を暗号化して返す(改行コード \n は使用禁止)
  afterSerialization: doc => {
    const cipher = crypto.createCipher('aes-256-ctr', password)
    let encrypted = cipher.update(doc, 'utf8', 'hex')
    encrypted += cipher.final('hex')

    return encrypted
  },
  // 復号化した文字列を返す
  beforeDeserialization: encrypted => {
    const decipher = crypto.createDecipher('aes-256-ctr', password)
    let doc = decipher.update(encrypted, 'hex', 'utf8')
    doc += decipher.final('utf8')

    return doc
  },
  filename: path.join(__dirname, 'documents.db')
})
0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?