Wuu Posted September 16, 2016 Report Share Posted September 16, 2016 (edited) Ir kāds labs veids, kā lielu teksta blāķi saspiest, pēc iespējas unikālākā [a-z] simbolu virknē? Pagaidām ir ideja, katru burtu pār-konvertēt uz ascii. Sasummēt, un rezultātu pa vienam ciparam atpakaļ ascii + sāli (a.k. kopējais teksta garums) const result = String([...text.replace(/\s/, '')].reduce((all, val) => all / 2 + val.charCodeAt(), '')).replace(/\./, '').split('') .map(val => String.fromCharCode(97 + Number(val))) .join('') Pagaidu variantā tiek izmantoti tikai 10 simboli. Vēl viens variants, ar gatavu ascii tabulu, no kuras paņemt vajadzīgo simbolu virkni. const ascii = [45, 95, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122] const result = `${[...text].reduce((all, val) => all / 2 + val.charCodeAt(), 0)}${text.length}` .match(/(1\d{1,2})|(\d{1,2})/g) .map(val => ascii.indexOf(val) === -1 ? String.fromCharCode(ascii[val % ascii.length]) : String.fromCharCode(val)) .join('') Edited September 16, 2016 by Wuu Quote Link to comment Share on other sites More sharing options...
codehighriga Posted September 16, 2016 Report Share Posted September 16, 2016 (edited) Varbūt vienkārši izlaist caur md5 vai sha? Tev tas hash nederētu? Nu labi tur nebūtu tikai [a-z], būtu arī cipari. Edited September 16, 2016 by codehighriga Quote Link to comment Share on other sites More sharing options...
Blitz Posted September 16, 2016 Report Share Posted September 16, 2016 (edited) String.prototype.hashCode = function(){ var hash = 0; if (this.length == 0) return hash; for (i = 0; i < this.length; i++) { char = this.charCodeAt(i); hash = ((hash<<5)-hash)+char; hash = hash & hash; // Convert to 32bit integer } return hash; } + http://hashids.org/ Edited September 16, 2016 by Blitz Quote Link to comment Share on other sites More sharing options...
codez Posted September 16, 2016 Report Share Posted September 16, 2016 (edited) Ideja "teksta blāķi" pārveidot masīvā ar simboliem, lai to apstrādātu, neizskatās īpaši pievilcīga + tas FP kods ir absolūti nelasāms. Ja tu šitādu murgu uzrakstītu manis pārraudzītā projektā, tu izlidotu, acis nepamirkšķinot, ja vien tev nebūtu pietiekami ticama atruna, ka to izdarīji, lai izjokotu kādu kolēģi. Edited September 16, 2016 by codez Quote Link to comment Share on other sites More sharing options...
Wuu Posted September 16, 2016 Author Report Share Posted September 16, 2016 Varbūt vienkārši izlaist caur md5 vai sha? Tev tas hash nederētu? Nu labi tur nebūtu tikai [a-z], būtu arī cipari. Nepieciešams [a-Z], papētīšu Blitz variantu. Vienīgi http://hashids.org/links nedarbojas Quote Link to comment Share on other sites More sharing options...
Val Posted September 16, 2016 Report Share Posted September 16, 2016 nodzēs %A0 no linka Quote Link to comment Share on other sites More sharing options...
Blitz Posted September 16, 2016 Report Share Posted September 16, 2016 Salaboju linku, sry Quote Link to comment Share on other sites More sharing options...
daGrevis Posted September 16, 2016 Report Share Posted September 16, 2016 Kā tos ID izmantosi? Konvertēsi no ID atpakaļ uz tekstu at some point? Quote Link to comment Share on other sites More sharing options...
spainis Posted September 16, 2016 Report Share Posted September 16, 2016 'use strict'; const crypto = require('crypto'), h = crypto.createHash('md5'), chars = [...'ABCDEFGHIJKLMNOPQRSTUVWXYZ'], bigInt = require('big-integer'); h.update('test trololo', 'utf8'); let b = bigInt(h.digest('hex'), 16), res = ''; while(!b.isZero()) { const {quotient: q, remainder: r} = b.divmod(chars.length); res += chars[r.toString()]; b = q; } console.log(res); Quote Link to comment Share on other sites More sharing options...
Mr.Key Posted September 16, 2016 Report Share Posted September 16, 2016 mjā. Quote Link to comment Share on other sites More sharing options...
Aleksejs Posted September 17, 2016 Report Share Posted September 17, 2016 Es laikam izmantotu kādu no standartā gandrīz vienmēr pieejamajiem esošajiem hešošanas algoritmiem md5() sha1() crc32() vai arī kādu no sarakstahttp://lv.php.net/manual/en/function.hash-algos.phpJa nav vajadzības, lai algoritms būtu ar kriptogrāfijai nepieciešamajām īpašībām (piemēram, nav svarīgi, lai no rezultāta būtu grūti atrast tādus ievaddatus, kuri dod šādu rezultātu), tad var izmantot arī CRC. Ja nav svarīgi, tad būtu jāizvairās arī no md5 un sha1.Pēc tam iegūtos datus konvertētu no heksadecimālās bāzes uz bāzi, kas satur visus burtus:http://lv.php.net/manual/en/function.base-convert.php(ja neder, ka satur ciparus, tad var izmantot šo piemēru: http://lv.php.net/manual/en/function.base-convert.php#106546 ) Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.