UNPKG

6.5 kBSource Map (JSON)View Raw
1{"version":3,"file":"rabin.cjs","sources":["../hash/rabin.js"],"sourcesContent":["/**\n * @module rabin\n *\n * Very efficient & versatile fingerprint/hashing algorithm. However, it is not cryptographically\n * secure. Well suited for fingerprinting.\n */\n\nimport * as buffer from '../buffer.js'\nimport * as map from '../map.js'\n\nexport const StandardIrreducible8 = new Uint8Array([1, 221])\nexport const StandardIrreducible16 = new Uint8Array([1, 244, 157])\nexport const StandardIrreducible32 = new Uint8Array([1, 149, 183, 205, 191])\nexport const StandardIrreducible64 = new Uint8Array([1, 133, 250, 114, 193, 250, 28, 193, 231])\nexport const StandardIrreducible128 = new Uint8Array([1, 94, 109, 166, 228, 6, 222, 102, 239, 27, 128, 184, 13, 50, 112, 169, 199])\n\n/**\n * Maps from a modulo to the precomputed values.\n *\n * @type {Map<string,Uint8Array>}\n */\nconst _precomputedFingerprintCache = new Map()\n\n/**\n * @param {Uint8Array} m\n */\nconst ensureCache = m => map.setIfUndefined(_precomputedFingerprintCache, buffer.toBase64(m), () => {\n const byteLen = m.byteLength\n const cache = new Uint8Array(256 * byteLen)\n // Use dynamic computing to compute the cached results.\n // Starting values: cache(0) = 0; cache(1) = m\n cache.set(m, byteLen)\n for (let bit = 1; bit < 8; bit++) {\n const mBitShifted = buffer.shiftNBitsLeft(m, bit)\n const bitShifted = 1 << bit\n for (let j = 0; j < bitShifted; j++) {\n // apply the shifted result (reducing the degree of the polynomial)\n const msb = bitShifted | j\n const rest = msb ^ mBitShifted[0]\n for (let i = 0; i < byteLen; i++) {\n // rest is already precomputed in the cache\n cache[msb * byteLen + i] = cache[rest * byteLen + i] ^ mBitShifted[i]\n }\n // if (cache[(bitShifted | j) * byteLen] !== (bitShifted | j)) { error.unexpectedCase() }\n }\n }\n return cache\n})\n\nexport class RabinEncoder {\n /**\n * @param {Uint8Array} m assert(m[0] === 1)\n */\n constructor (m) {\n this.m = m\n this.blen = m.byteLength\n this.bs = new Uint8Array(this.blen)\n this.cache = ensureCache(m)\n /**\n * This describes the position of the most significant byte (starts with 0 and increases with\n * shift)\n */\n this.bpos = 0\n }\n\n /**\n * @param {number} byte\n */\n write (byte) {\n // assert(this.bs[0] === 0)\n // Shift one byte to the left, add b\n this.bs[this.bpos] = byte\n this.bpos = (this.bpos + 1) % this.blen\n const msb = this.bs[this.bpos]\n for (let i = 0; i < this.blen; i++) {\n this.bs[(this.bpos + i) % this.blen] ^= this.cache[msb * this.blen + i]\n }\n // assert(this.bs[this.bpos] === 0)\n }\n\n getFingerprint () {\n const result = new Uint8Array(this.blen - 1)\n for (let i = 0; i < result.byteLength; i++) {\n result[i] = this.bs[(this.bpos + i + 1) % this.blen]\n }\n return result\n }\n}\n\n/**\n * @param {Uint8Array} irreducible\n * @param {Uint8Array} data\n */\nexport const fingerprint = (irreducible, data) => {\n const encoder = new RabinEncoder(irreducible)\n for (let i = 0; i < data.length; i++) {\n encoder.write(data[i])\n }\n return encoder.getFingerprint()\n}\n"],"names":["map.setIfUndefined","buffer.toBase64","buffer.shiftNBitsLeft"],"mappings":";;;;;;;;;;;;;;;;;;;AAAA;AACA;AACA;AACA;AACA;AACA;AAIA;AACY,MAAC,oBAAoB,GAAG,IAAI,UAAU,CAAC,CAAC,CAAC,EAAE,GAAG,CAAC,EAAC;AAChD,MAAC,qBAAqB,GAAG,IAAI,UAAU,CAAC,CAAC,CAAC,EAAE,GAAG,EAAE,GAAG,CAAC,EAAC;AACtD,MAAC,qBAAqB,GAAG,IAAI,UAAU,CAAC,CAAC,CAAC,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,CAAC,EAAC;AAChE,MAAC,qBAAqB,GAAG,IAAI,UAAU,CAAC,CAAC,CAAC,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,EAAE,EAAE,GAAG,EAAE,GAAG,CAAC,EAAC;AACnF,MAAC,sBAAsB,GAAG,IAAI,UAAU,CAAC,CAAC,CAAC,EAAE,EA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