2016-11-07 9 views
1

mongodbの暗号化されたフィールドで部分検索(/ regexクエリなど)が可能ですか?MongoDB暗号化フィールド部分検索

私は暗号化された電子メールIDを含むフィールドを持っています。だからmongoの電子メール[email protected]1bcuh6762jhjdSOME_ENCRYPTED_VALUEになりました。この暗号化された値の部分検索が可能になりました。

+0

弱い暗号化を使用する場合のみ。たとえば、プレフィックスマッチだけが必要な場合は、プレフィックスを保持する暗号化アルゴリズムを使用できます。 – CodesInChaos

+0

検索可能な暗号がいくつかあります(たとえば、忘れ去られたRAMなど)が、あなたはそれを使いたくないと思います。 http://outsourcedbits.org/categories/encrypted-search/ – CodesInChaos

+0

@CodesInChaosチュートリアルやいくつかのwikiへのリンクはありますか? –

答えて

1

3文字以上の可能なすべての値のハッシュを作成して解決しました。すべての可能な組み合わせに対してハッシュを作成したら、pre('save')で設定する値を追加できます。今

myModel.find({}).then(docs => { 
    docs.forEach(doc => doc.save().catch(console.log)) 
}) 

:あなたはこのようなすべてのレコードを移行することができ、その後hash.js

const hash = require('crypto'); 
const adler32 = require('adler32'); 
const crypto = require('crypto'); 
const _ = require('lodash'); 
adler32.register(); 

module.exports = Hash = { 
    /** 
    * This method creates hash of given text; 
    * @param text - text to hash; 
    */ 
    createHash: (text) => { 
     return crypto.createHash('adler32').update(text, 'ascii').digest('hex'); 
    }, 
    /** 
    * 
    * @param text- text separated by spaces 
    */ 
    createHashForText: (text) => { 
     "use strict"; 
     text= text.toLowerCase(); 
     text= text.trim(); 
     let textSlices = _.flatten(text.split(' ').filter(x => x.length) 
      .map((part) => { 
      if (part.length < 3) { 
       return part; 
      } 
      let nameSlices = []; 
      for (let index = 2; index < part.length; index ++) { 
       let n = part.slice(0, index+1); 
       textSlices .push(n); 
      } 
      return textSlices ; 
     })); 

     return textSlices .map(n => Hash.createHash(n)); 
    } 
} 

をハッシュするために

function createHash(next) { 
    this.hashes = hash.createHashForField(this.text); 
    debug(text); 
    next(); 
} 

mySchema.pre('save', createHash); 

モジュール:あなたはこの関数を作成することができ、スキーマ内の

検索すると、createHashの機能を使用する必要がありますキーワードのハッシュを検索し、データベースで検索します。

こちらがお役に立てば幸いです。

関連する問題