2017-03-01 62 views
3

我有一個arrayobject s在一個JSON文件。 object s沒有獨特的id s,所以我需要創建一個唯一的id或唯一的string使用JavaScript的md5的較短版本..?

[ 
    { 
    "name": "Jack Potts", 
    "age": "56" 
    }, { 
    "name": "Rusty Carr", 
    "age": "31" 
    } 
] 

我知道我可以使用MD5,傳遞object,但我要在URL中使用string,所以我寧願它是短。

而不是/people/3449c9e5e332f1dbb81505cd739fbf3f,我更喜歡更像/people/1dbb81505的東西。

它仍然需要是object的表示,因爲我要再次從URL中查找此人。

是否有任何產生短於MD5 stringstring ..?

我會猜測MD5是我最好的/唯一的選擇,但我想我會問。

UPDATE

我也許不明確的,因爲我本來可以。我不只需要生成一個獨特的id。我不會用我生成的任何東西來更新JSON文件。

我需要一種方法來採取object有問題,爲其創建一個URL,然後當訪問該URL時,使用該URL返回中的array

據我知道,如果你在同一個string傳遞給MD5一遍又一遍,它總是會返回相同的MD5 string,因爲它是一個表示。出於同樣的原因,在數據庫中存儲密碼時不要使用它?

也許MD5是好的,我只是認爲可能會產生一個更短的string這是一個表示的數據。這是我的問題。

更新2

array人們可能會改變。人們可能會被添加和刪除,因此使用array索引不起作用。

+0

MD5是256位長...所以,不... CRC32 –

+1

哈希函數的結果越短,碰撞的可能性就越大。 – CBroe

+0

只是使用整數,爲什麼長度很重要? – Dave3of5

回答

1

如果你只是想要一個較短的輸出MD5但否則滿足唯一性只是截短到你需要的長度,每一位與任何其他位一樣隨機,這就是你選擇的任何子集與其他任何子集一樣好。

但要認識到,如果兩個名字相同,你會得到相同的散列。

正如你必須意識到散列越短碰撞的更高變化,你就是在哈希長度與唯一性之間進行權衡,這並不壞,只要確保你有足夠的唯一性滿足你的需求。

+0

我喜歡這個答案......所以我可以使用MD5和'str.substring(0,10)'(例如)。我現在可以看到我的問題沒有措辭得很好,但這個答案是我所需要的。 –

+0

是的,如問題中的字符串表示。 – zaph

0

使用下列的函數:

function generateUID() { 
    var firstPart = (Math.random() * 46656) | 0; 
    var secondPart = (Math.random() * 46656) | 0; 
    firstPart = ("000" + firstPart.toString(36)).slice(-3); 
    secondPart = ("000" + secondPart.toString(36)).slice(-3); 
    return firstPart + secondPart; 
} 

A 6字符的字母數字序列是不夠好,隨機指數一個10K集合(366 = 2.2十億和363 = 46656)。

+0

我已經更新了這個問題,我不只需要一個唯一的ID,它需要是對象的**表示**,就像MD5一樣。 –

0

我建議你使用sha1,它會處理一個相對較短的散列。假設你的數據集相對有限< 1000000000000 ...等...項目碰撞的機率應該是最小的。

https://github.com/emn178/js-sha1是一個很好的圖書館

編輯修改此把它縮短

現在做了子,+碰撞檢測的修改應該是可靠的,只要項目的順序如果他們具有smae值,則不會改變。但話又說回來,如果他們有相同的價值觀是不應該的問題;-)

/* 
 
* [js-sha1]{@link https://github.com/emn178/js-sha1} 
 
* 
 
* @version 0.4.1 
 
* @author Chen, Yi-Cyuan [[email protected]] 
 
* @copyright Chen, Yi-Cyuan 2014-2016 
 
* @license MIT 
 
*/ 
 
/*jslint bitwise: true */ 
 
(function() { 
 
    'use strict'; 
 

 
    var root = typeof window === 'object' ? window : {}; 
 
    var NODE_JS = !root.JS_SHA1_NO_NODE_JS && typeof process === 'object' && process.versions && process.versions.node; 
 
    if (NODE_JS) { 
 
    root = global; 
 
    } 
 
    var COMMON_JS = !root.JS_SHA1_NO_COMMON_JS && typeof module === 'object' && module.exports; 
 
    var AMD = typeof define === 'function' && define.amd; 
 
    var HEX_CHARS = 'abcdef'.split(''); 
 
    var EXTRA = [-2147483648, 8388608, 32768, 128]; 
 
    var SHIFT = [24, 16, 8, 0]; 
 
    var OUTPUT_TYPES = ['hex', 'array', 'digest', 'arrayBuffer']; 
 

 
    var blocks = []; 
 

 
    var createOutputMethod = function (outputType) { 
 
    return function (message) { 
 
     return new Sha1(true).update(message)[outputType](); 
 
    }; 
 
    }; 
 

 
    var createMethod = function() { 
 
    var method = createOutputMethod('hex'); 
 
    if (NODE_JS) { 
 
     method = nodeWrap(method); 
 
    } 
 
    method.create = function() { 
 
     return new Sha1(); 
 
    }; 
 
    method.update = function (message) { 
 
     return method.create().update(message); 
 
    }; 
 
    for (var i = 0; i < OUTPUT_TYPES.length; ++i) { 
 
     var type = OUTPUT_TYPES[i]; 
 
     method[type] = createOutputMethod(type); 
 
    } 
 
    return method; 
 
    }; 
 

 
    var nodeWrap = function (method) { 
 
    var crypto = require('crypto'); 
 
    var Buffer = require('buffer').Buffer; 
 
    var nodeMethod = function (message) { 
 
     if (typeof message === 'string') { 
 
     return crypto.createHash('sha1').update(message, 'utf8').digest('hex'); 
 
     } else if (message.constructor === ArrayBuffer) { 
 
     message = new Uint8Array(message); 
 
     } else if (message.length === undefined) { 
 
     return method(message); 
 
     } 
 
     return crypto.createHash('sha1').update(new Buffer(message)).digest('hex'); 
 
    }; 
 
    return nodeMethod; 
 
    }; 
 

 
    function Sha1(sharedMemory) { 
 
    if (sharedMemory) { 
 
     blocks[0] = blocks[16] = blocks[1] = blocks[2] = blocks[3] = 
 
     blocks[4] = blocks[5] = blocks[6] = blocks[7] = 
 
     blocks[8] = blocks[9] = blocks[10] = blocks[11] = 
 
     blocks[12] = blocks[13] = blocks[14] = blocks[15] = 0; 
 
     this.blocks = blocks; 
 
    } else { 
 
     this.blocks = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; 
 
    } 
 

 
    this.h0 = 0x67452301; 
 
    this.h1 = 0xEFCDAB89; 
 
    this.h2 = 0x98BADCFE; 
 
    this.h3 = 0x10325476; 
 
    this.h4 = 0xC3D2E1F0; 
 

 
    this.block = this.start = this.bytes = 0; 
 
    this.finalized = this.hashed = false; 
 
    this.first = true; 
 
    } 
 

 
    Sha1.prototype.update = function (message) { 
 
    if (this.finalized) { 
 
     return; 
 
    } 
 
    var notString = typeof(message) !== 'string'; 
 
    if (notString && message.constructor === root.ArrayBuffer) { 
 
     message = new Uint8Array(message); 
 
    } 
 
    var code, index = 0, i, length = message.length || 0, blocks = this.blocks; 
 

 
    while (index < length) { 
 
     if (this.hashed) { 
 
     this.hashed = false; 
 
     blocks[0] = this.block; 
 
     blocks[16] = blocks[1] = blocks[2] = blocks[3] = 
 
     blocks[4] = blocks[5] = blocks[6] = blocks[7] = 
 
     blocks[8] = blocks[9] = blocks[10] = blocks[11] = 
 
     blocks[12] = blocks[13] = blocks[14] = blocks[15] = 0; 
 
     } 
 

 
     if(notString) { 
 
     for (i = this.start; index < length && i < 64; ++index) { 
 
      blocks[i >> 2] |= message[index] << SHIFT[i++ & 3]; 
 
     } 
 
     } else { 
 
     for (i = this.start; index < length && i < 64; ++index) { 
 
      code = message.charCodeAt(index); 
 
      if (code < 0x80) { 
 
      blocks[i >> 2] |= code << SHIFT[i++ & 3]; 
 
      } else if (code < 0x800) { 
 
      blocks[i >> 2] |= (0xc0 | (code >> 6)) << SHIFT[i++ & 3]; 
 
      blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3]; 
 
      } else if (code < 0xd800 || code >= 0xe000) { 
 
      blocks[i >> 2] |= (0xe0 | (code >> 12)) << SHIFT[i++ & 3]; 
 
      blocks[i >> 2] |= (0x80 | ((code >> 6) & 0x3f)) << SHIFT[i++ & 3]; 
 
      blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3]; 
 
      } else { 
 
      code = 0x10000 + (((code & 0x3ff) << 10) | (message.charCodeAt(++index) & 0x3ff)); 
 
      blocks[i >> 2] |= (0xf0 | (code >> 18)) << SHIFT[i++ & 3]; 
 
      blocks[i >> 2] |= (0x80 | ((code >> 12) & 0x3f)) << SHIFT[i++ & 3]; 
 
      blocks[i >> 2] |= (0x80 | ((code >> 6) & 0x3f)) << SHIFT[i++ & 3]; 
 
      blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3]; 
 
      } 
 
     } 
 
     } 
 

 
     this.lastByteIndex = i; 
 
     this.bytes += i - this.start; 
 
     if (i >= 64) { 
 
     this.block = blocks[16]; 
 
     this.start = i - 64; 
 
     this.hash(); 
 
     this.hashed = true; 
 
     } else { 
 
     this.start = i; 
 
     } 
 
    } 
 
    return this; 
 
    }; 
 

 
    Sha1.prototype.finalize = function() { 
 
    if (this.finalized) { 
 
     return; 
 
    } 
 
    this.finalized = true; 
 
    var blocks = this.blocks, i = this.lastByteIndex; 
 
    blocks[16] = this.block; 
 
    blocks[i >> 2] |= EXTRA[i & 3]; 
 
    this.block = blocks[16]; 
 
    if (i >= 56) { 
 
     if (!this.hashed) { 
 
     this.hash(); 
 
     } 
 
     blocks[0] = this.block; 
 
     blocks[16] = blocks[1] = blocks[2] = blocks[3] = 
 
     blocks[4] = blocks[5] = blocks[6] = blocks[7] = 
 
     blocks[8] = blocks[9] = blocks[10] = blocks[11] = 
 
     blocks[12] = blocks[13] = blocks[14] = blocks[15] = 0; 
 
    } 
 
    blocks[15] = this.bytes << 3; 
 
    this.hash(); 
 
    }; 
 

 
    Sha1.prototype.hash = function() { 
 
    var a = this.h0, b = this.h1, c = this.h2, d = this.h3, e = this.h4; 
 
    var f, j, t, blocks = this.blocks; 
 

 
    for(j = 16; j < 80; ++j) { 
 
     t = blocks[j - 3]^blocks[j - 8]^blocks[j - 14]^blocks[j - 16]; 
 
     blocks[j] = (t << 1) | (t >>> 31); 
 
    } 
 

 
    for(j = 0; j < 20; j += 5) { 
 
     f = (b & c) | ((~b) & d); 
 
     t = (a << 5) | (a >>> 27); 
 
     e = t + f + e + 1518500249 + blocks[j] << 0; 
 
     b = (b << 30) | (b >>> 2); 
 

 
     f = (a & b) | ((~a) & c); 
 
     t = (e << 5) | (e >>> 27); 
 
     d = t + f + d + 1518500249 + blocks[j + 1] << 0; 
 
     a = (a << 30) | (a >>> 2); 
 

 
     f = (e & a) | ((~e) & b); 
 
     t = (d << 5) | (d >>> 27); 
 
     c = t + f + c + 1518500249 + blocks[j + 2] << 0; 
 
     e = (e << 30) | (e >>> 2); 
 

 
     f = (d & e) | ((~d) & a); 
 
     t = (c << 5) | (c >>> 27); 
 
     b = t + f + b + 1518500249 + blocks[j + 3] << 0; 
 
     d = (d << 30) | (d >>> 2); 
 

 
     f = (c & d) | ((~c) & e); 
 
     t = (b << 5) | (b >>> 27); 
 
     a = t + f + a + 1518500249 + blocks[j + 4] << 0; 
 
     c = (c << 30) | (c >>> 2); 
 
    } 
 

 
    for(; j < 40; j += 5) { 
 
     f = b^c^d; 
 
     t = (a << 5) | (a >>> 27); 
 
     e = t + f + e + 1859775393 + blocks[j] << 0; 
 
     b = (b << 30) | (b >>> 2); 
 

 
     f = a^b^c; 
 
     t = (e << 5) | (e >>> 27); 
 
     d = t + f + d + 1859775393 + blocks[j + 1] << 0; 
 
     a = (a << 30) | (a >>> 2); 
 

 
     f = e^a^b; 
 
     t = (d << 5) | (d >>> 27); 
 
     c = t + f + c + 1859775393 + blocks[j + 2] << 0; 
 
     e = (e << 30) | (e >>> 2); 
 

 
     f = d^e^a; 
 
     t = (c << 5) | (c >>> 27); 
 
     b = t + f + b + 1859775393 + blocks[j + 3] << 0; 
 
     d = (d << 30) | (d >>> 2); 
 

 
     f = c^d^e; 
 
     t = (b << 5) | (b >>> 27); 
 
     a = t + f + a + 1859775393 + blocks[j + 4] << 0; 
 
     c = (c << 30) | (c >>> 2); 
 
    } 
 

 
    for(; j < 60; j += 5) { 
 
     f = (b & c) | (b & d) | (c & d); 
 
     t = (a << 5) | (a >>> 27); 
 
     e = t + f + e - 1894007588 + blocks[j] << 0; 
 
     b = (b << 30) | (b >>> 2); 
 

 
     f = (a & b) | (a & c) | (b & c); 
 
     t = (e << 5) | (e >>> 27); 
 
     d = t + f + d - 1894007588 + blocks[j + 1] << 0; 
 
     a = (a << 30) | (a >>> 2); 
 

 
     f = (e & a) | (e & b) | (a & b); 
 
     t = (d << 5) | (d >>> 27); 
 
     c = t + f + c - 1894007588 + blocks[j + 2] << 0; 
 
     e = (e << 30) | (e >>> 2); 
 

 
     f = (d & e) | (d & a) | (e & a); 
 
     t = (c << 5) | (c >>> 27); 
 
     b = t + f + b - 1894007588 + blocks[j + 3] << 0; 
 
     d = (d << 30) | (d >>> 2); 
 

 
     f = (c & d) | (c & e) | (d & e); 
 
     t = (b << 5) | (b >>> 27); 
 
     a = t + f + a - 1894007588 + blocks[j + 4] << 0; 
 
     c = (c << 30) | (c >>> 2); 
 
    } 
 

 
    for(; j < 80; j += 5) { 
 
     f = b^c^d; 
 
     t = (a << 5) | (a >>> 27); 
 
     e = t + f + e - 899497514 + blocks[j] << 0; 
 
     b = (b << 30) | (b >>> 2); 
 

 
     f = a^b^c; 
 
     t = (e << 5) | (e >>> 27); 
 
     d = t + f + d - 899497514 + blocks[j + 1] << 0; 
 
     a = (a << 30) | (a >>> 2); 
 

 
     f = e^a^b; 
 
     t = (d << 5) | (d >>> 27); 
 
     c = t + f + c - 899497514 + blocks[j + 2] << 0; 
 
     e = (e << 30) | (e >>> 2); 
 

 
     f = d^e^a; 
 
     t = (c << 5) | (c >>> 27); 
 
     b = t + f + b - 899497514 + blocks[j + 3] << 0; 
 
     d = (d << 30) | (d >>> 2); 
 

 
     f = c^d^e; 
 
     t = (b << 5) | (b >>> 27); 
 
     a = t + f + a - 899497514 + blocks[j + 4] << 0; 
 
     c = (c << 30) | (c >>> 2); 
 
    } 
 

 
    this.h0 = this.h0 + a << 0; 
 
    this.h1 = this.h1 + b << 0; 
 
    this.h2 = this.h2 + c << 0; 
 
    this.h3 = this.h3 + d << 0; 
 
    this.h4 = this.h4 + e << 0; 
 
    }; 
 

 
    Sha1.prototype.hex = function() { 
 
    this.finalize(); 
 

 
    var h0 = this.h0, h1 = this.h1, h2 = this.h2, h3 = this.h3, h4 = this.h4; 
 

 
    return HEX_CHARS[(h0 >> 28) & 0x0F] + HEX_CHARS[(h0 >> 24) & 0x0F] + 
 
      HEX_CHARS[(h0 >> 20) & 0x0F] + HEX_CHARS[(h0 >> 16) & 0x0F] + 
 
      HEX_CHARS[(h0 >> 12) & 0x0F] + HEX_CHARS[(h0 >> 8) & 0x0F] + 
 
      HEX_CHARS[(h0 >> 4) & 0x0F] + HEX_CHARS[h0 & 0x0F] + 
 
      HEX_CHARS[(h1 >> 28) & 0x0F] + HEX_CHARS[(h1 >> 24) & 0x0F] + 
 
      HEX_CHARS[(h1 >> 20) & 0x0F] + HEX_CHARS[(h1 >> 16) & 0x0F] + 
 
      HEX_CHARS[(h1 >> 12) & 0x0F] + HEX_CHARS[(h1 >> 8) & 0x0F] + 
 
      HEX_CHARS[(h1 >> 4) & 0x0F] + HEX_CHARS[h1 & 0x0F] + 
 
      HEX_CHARS[(h2 >> 28) & 0x0F] + HEX_CHARS[(h2 >> 24) & 0x0F] + 
 
      HEX_CHARS[(h2 >> 20) & 0x0F] + HEX_CHARS[(h2 >> 16) & 0x0F] + 
 
      HEX_CHARS[(h2 >> 12) & 0x0F] + HEX_CHARS[(h2 >> 8) & 0x0F] + 
 
      HEX_CHARS[(h2 >> 4) & 0x0F] + HEX_CHARS[h2 & 0x0F] + 
 
      HEX_CHARS[(h3 >> 28) & 0x0F] + HEX_CHARS[(h3 >> 24) & 0x0F] + 
 
      HEX_CHARS[(h3 >> 20) & 0x0F] + HEX_CHARS[(h3 >> 16) & 0x0F] + 
 
      HEX_CHARS[(h3 >> 12) & 0x0F] + HEX_CHARS[(h3 >> 8) & 0x0F] + 
 
      HEX_CHARS[(h3 >> 4) & 0x0F] + HEX_CHARS[h3 & 0x0F] + 
 
      HEX_CHARS[(h4 >> 28) & 0x0F] + HEX_CHARS[(h4 >> 24) & 0x0F] + 
 
      HEX_CHARS[(h4 >> 20) & 0x0F] + HEX_CHARS[(h4 >> 16) & 0x0F] + 
 
      HEX_CHARS[(h4 >> 12) & 0x0F] + HEX_CHARS[(h4 >> 8) & 0x0F] + 
 
      HEX_CHARS[(h4 >> 4) & 0x0F] + HEX_CHARS[h4 & 0x0F]; 
 
    }; 
 

 
    Sha1.prototype.toString = Sha1.prototype.hex; 
 

 
    Sha1.prototype.digest = function() { 
 
    this.finalize(); 
 

 
    var h0 = this.h0, h1 = this.h1, h2 = this.h2, h3 = this.h3, h4 = this.h4; 
 

 
    return [ 
 
     (h0 >> 24) & 0xFF, (h0 >> 16) & 0xFF, (h0 >> 8) & 0xFF, h0 & 0xFF, 
 
     (h1 >> 24) & 0xFF, (h1 >> 16) & 0xFF, (h1 >> 8) & 0xFF, h1 & 0xFF, 
 
     (h2 >> 24) & 0xFF, (h2 >> 16) & 0xFF, (h2 >> 8) & 0xFF, h2 & 0xFF, 
 
     (h3 >> 24) & 0xFF, (h3 >> 16) & 0xFF, (h3 >> 8) & 0xFF, h3 & 0xFF, 
 
     (h4 >> 24) & 0xFF, (h4 >> 16) & 0xFF, (h4 >> 8) & 0xFF, h4 & 0xFF 
 
    ]; 
 
    }; 
 

 
    Sha1.prototype.array = Sha1.prototype.digest; 
 

 
    Sha1.prototype.arrayBuffer = function() { 
 
    this.finalize(); 
 

 
    var buffer = new ArrayBuffer(20); 
 
    var dataView = new DataView(buffer); 
 
    dataView.setUint32(0, this.h0); 
 
    dataView.setUint32(4, this.h1); 
 
    dataView.setUint32(8, this.h2); 
 
    dataView.setUint32(12, this.h3); 
 
    dataView.setUint32(16, this.h4); 
 
    return buffer; 
 
    }; 
 

 
    var exports = createMethod(); 
 

 
    if (COMMON_JS) { 
 
    module.exports = exports; 
 
    } else { 
 
    root.sha1 = exports; 
 
    if (AMD) { 
 
     define(function() { 
 
     return exports; 
 
     }); 
 
    } 
 
    } 
 
})(); 
 
+function() { 
 
    var HASHLENGTH = 5; 
 
    document.getElementById('clickme').onclick = function() { 
 
    // get all the values as objects 
 
    var values = JSON.parse(document.getElementById('sample').value); 
 

 
    for (var c = 0; c < values.length; c++) { 
 
     // Hash it and substring it given the hashlength 
 
     values[c].hash = sha1(JSON.stringify(values[c])).substring(0,HASHLENGTH); 
 
     // If you don't need the collision detection, you can just remove these loops. 
 
     // check for collisions 
 
     for(var i = 0;i < c; i++) { 
 
      // collision detected. add a dependable value to get a new hash. 
 
      if(values[i].hash == values[c].hash) { 
 
       if(values[c].hasOwnProperty('_change')) { 
 
        values[c]._change = sha1(values[c]._change); 
 
       } 
 
       else { 
 
        values[c]._change = sha1(values[c].hash); 
 
       } 
 
       c--;break; // return to same thing for a rehash 
 
      } 
 
     } 
 
    } 
 
    console.log(JSON.stringify(values,null,4)); 
 
    } 
 
}();
textarea { width:100%;height:200px;}
<button id="clickme"> Parse </button> 
 
<textarea id="sample"> 
 
[ 
 
    { 
 
    "name": "Jack Potts", 
 
    "age": "56" 
 
    }, { 
 
    "name": "Rusty Carr", 
 
    "age": "31" 
 
    }, 
 
    { 
 
    "name": "Rusty Carr", 
 
    "age": "31" 
 
    }, 
 
    { 
 
    "name": "Rusty Carr", 
 
    "age": "31" 
 
    }, 
 
    { 
 
    "name": "Rusty Carr", 
 
    "age": "31" 
 
    }, 
 
    { 
 
    "name": "Rusty Carr", 
 
    "age": "31" 
 
    }, 
 
    { 
 
    "name": "Rusty Carr", 
 
    "age": "31" 
 
    } 
 
] 
 
</textarea>

+0

這似乎產生一個散列一樣長,如果不超過MD5。我的問題是關於MD5的縮短版本。 –

+0

通常所做的是真正的哈希子串。所以如果你做一個5字母的子字符串,你有16x16x16x16x16(1048576)字符組合的可能性。使用一個提供足夠靈活性的javascript數據集。如果有一個collsion,你可以通過添加一個值來緩和地檢查和修改對象,使它不可碰撞。我將修改我的示例以表明這一點。 – Tschallacka

+0

@StephenLast我用一個子串+雙重碰撞檢測修改了我的例子。對於真實世界的實現應該足夠強大。 – Tschallacka

0

我不會更新與任何我生成JSON文件。

我需要一種方法來接受有問題的對象,爲它創建一個URL,然後當訪問URL時,使用URL返回數組中的對象。

然後使用對象的索引數組中:這種方式people/0將返回{"name": "Jack Potts", "age": "56"}people/1將返回{"name": "Rusty Carr", "age": "31"},等等...

+0

只要JSON數組中的人員不會改變,就可以工作,在我的情況下,他們可能會改變。人們可能被添加和刪除,並且URL需要指向正確的人。我會更新我的問題 - 對不起! –