This commit is contained in:
Edoardo Tenani 2016-02-16 23:11:21 +01:00
Родитель c450062818
Коммит 671e98bad1
2 изменённых файлов: 80 добавлений и 81 удалений

61
common.js Normal file
Просмотреть файл

@ -0,0 +1,61 @@
var _ = require('lodash')
var jsesc = require('jsesc')
var punycode = require('punycode')
var chars = require('./libs/crypto-lib/char-list.js')
// var chars = '!"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~£¥¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿ'
console.log('chars ', chars)
var CryptoLib = require('./libs/crypto-lib.js')
var encryptedText = CryptoLib.encrypt(chars, '🍘')
console.log('encrypt', encryptedText)
console.log('decrypt', CryptoLib.decrypt(encryptedText, '🍘'))
CryptoLib.generateEmojiSubsetFrom('🍘')
var encryptedText = CryptoLib.encrypt('this is a text', '🍘')
console.log('encrypt', encryptedText)
console.log('decrypt', CryptoLib.decrypt(encryptedText, '🍘'))
CryptoLib.generateEmojiSubsetFrom('🍎')
var encryptedText = CryptoLib.encrypt('this is a text', '🍎')
console.log('encrypt', encryptedText)
console.log('decrypt', CryptoLib.decrypt(encryptedText, '🍎'))
CryptoLib.generateEmojiSubsetFrom('🐝')
var encryptedText = CryptoLib.encrypt('this is a text', '🐝')
console.log('encrypt', encryptedText)
console.log('decrypt', CryptoLib.decrypt(encryptedText, '🐝'))
// caesar cypher OK
// var CaesarShifter = require('./libs/crypto-lib/caesar-shifter.js')
// console.log('encrypt', CaesarShifter.encrypt(chars, 0))
// console.log('encrypt', CaesarShifter.encrypt(chars, 1))
// console.log('encrypt', CaesarShifter.encrypt(chars, 2))
// console.log('encrypt', CaesarShifter.encrypt(chars, 3))
// console.log('encrypt', CaesarShifter.encrypt(chars, 4))
// console.log('encrypt', CaesarShifter.encrypt(chars, 10))
// console.log('decrypt', CaesarShifter.decrypt(CaesarShifter.encrypt(chars, 1), 1))
// console.log('decrypt', CaesarShifter.decrypt(CaesarShifter.encrypt(chars, 10), 10))
// emojifier
// var Emojifier = require('./libs/crypto-lib/emojifier.js')
// // Emojifier.generateEmojiListFrom()
// // console.log('0 ', Emojifier.encode(chars))
// // console.log('0 ', punycode.ucs2.encode(Emojifier.encode(chars)).length)
// Emojifier.generateEmojiListFrom(10)
// // console.log('20', Emojifier.encode(chars))
// // console.log('20', punycode.ucs2.encode(Emojifier.encode(chars)).length)
// gimmestats(Emojifier.encode(chars), 'encode')
// gimmestats2(Emojifier.decode(Emojifier.encode(chars)), 'decode')
// function gimmestats (text, name) {
// console.log(name, text, punycode.ucs2.decode(text).length)
// }
// function gimmestats2 (text, name) {
// console.log(name, text, text.length)
// }

100
test.js
Просмотреть файл

@ -1,15 +1,6 @@
var _ = require('lodash')
var jsesc = require('jsesc')
var punycode = require('punycode')
// unicode ASCII 8 chars
// hex chars
@ -43,98 +34,45 @@ var chars = ""+
// 00F0 ð ñ ò ó ô õ ö ÷ ø ù ú û ü ý þ ÿ
"ðñòóôõö÷øùúûüýþÿ" // no 00F7
var es = "😹𝌆"
// console.log(chars)
var _ = require('lodash')
var jsesc = require('jsesc')
var punycode = require('punycode')
// _.each(chars, function (el) {
// console.log(el, jsesc(el))
// })
// console.log(jsesc(chars))
// console.log(jsesc('foo 𝌆 bar'))
// console.log(jsesc(es))
var string = '😹'
var string = '😱😥😦😰 😦😰 😸 😪😢😰😰😸😤😢'
// console.log(string, punycode.ucs2.decode(string))
// console.log(string.length, punycode.ucs2.decode(string).length)
// console.log(string.codePointAt(), punycode.ucs2.decode(string))
// console.log(jsesc(string), punycode.ucs2.decode(string))
// console.log(string.match(/([\0-\uD7FF\uE000-\uFFFF]|[\uD800-\uDBFF][\uDC00-\uDFFF])/))
// console.log(punycode.ucs2.decode('𝌆'))
// console.log(punycode.ucs2.decode('😚'))
// console.log(punycode.ucs2.decode('☺'))
// console.log(punycode.ucs2.decode('☺️'))
// console.log(punycode.ucs2.decode('👦🏼'))
// make chars unicode codepoints
chars = _.flatten(_.map(chars, function (char) { return punycode.ucs2.decode(char) }))
console.log('total chars: ' + chars.length)
var emojis = [8986,8987,9200,9203,9728,9729,9742,9748,9749,9757,9786,9824,9827,9829,9830,9832,9875,9889,9917,9918,9924,9925,9962,9970,9971,9973,9978,9981,9992,9993,9994,9995,9996,9999,10002,10024,10052,10084,11088,126980,127183,127744,127745,127746,127747,127748,127749,127750,127751,127752,127753,127754,127755,127756,127757,127758,127759,127760,127762,127763,127764,127765,127766,127767,127768,127769,127770,127771,127772,127773,127774,127775,127776,127792,127793,127794,127795,127796,127797,127799,127800,127801,127802,127803,127804,127805,127806,127807,127808,127809,127810,127811,127812,127813,127814,127815,127816,127817,127818,127819,127820,127821,127822,127823,127824,127825,127826,127827,127828,127829,127830,127831,127832,127833,127834,127835,127836,127837,127838,127839,127840,127841,127842,127843,127844,127845,127846,127847,127848,127849,127850,127851,127852,127853,127854,127855,127856,127857,127858,127859,127860,127861,127862,127863,127864,127865,127866,127867,127868,127872,127873,127874,127875,127876,127877,127878,127879,127880,127881,127882,127883,127884,127885,127886,127887,127888,127889,127890,127891,127904,127905,127906,127907,127908,127909,127911,127912,127913,127914,127915,127916,127917,127918,127919,127920,127921,127922,127923,127924,127925,127926,127927,127928,127929,127930,127931,127932,127933,127934,127935,127936,127938,127939,127940,127942,127943,127944,127945,127946,127968,127969,127970,127971,127972,127973,127974,127976,127977,127978,127979,127980,127981,127982,127983,127984,128000,128001,128002,128003,128004,128005,128006,128007,128008,128009,128010,128011,128012,128013,128014,128015,128016,128017,128018,128019,128020,128021,128022,128023,128024,128025,128026,128027,128028,128029,128030,128031,128032,128033,128034,128035,128036,128037,128038,128039,128040,128041,128042,128043,128044,128045,128046,128047,128048,128049,128050,128051,128052,128053,128054,128055,128056,128057,128058,128059,128060,128061,128062,128064,128066,128067,128068,128069,128070,128071,128072,128073,128074,128075,128076,128077,128078,128079,128080,128081,128082,128083,128084,128085,128086,128087,128088,128089,128090,128091,128092,128093,128094,128095,128096,128097,128098,128099,128101,128102,128103,128104,128105,128107,128108,128109,128110,128111,128112,128113,128114,128115,128116,128117,128118,128119,128120,128121,128122,128123,128124,128125,128126,128127,128128,128129,128130,128131,128132,128133,128134,128135,128136,128139,128140,128141,128142,128143,128144,128145,128146,128147,128148,128149,128150,128151,128152,128153,128154,128155,128156,128157,128158,128159,128161,128162,128163,128164,128165,128166,128167,128168,128169,128170,128171,128172,128173,128174,128176,128179,128180,128181,128182,128183,128184,128185,128186,128187,128188,128189,128190,128191,128192,128195,128196,128209,128210,128211,128212,128213,128214,128215,128216,128217,128218,128220,128221,128222,128223,128224,128225,128226,128227,128228,128229,128230,128231,128232,128233,128234,128235,128236,128237,128238,128239,128240,128241,128242,128247,128249,128250,128251,128252,128263,128264,128265,128266,128267,128268,128269,128270,128276,128277,128278,128293,128294,128300,128301,128507,128508,128509,128510,128512,128513,128514,128515,128516,128517,128518,128519,128520,128521,128522,128523,128524,128525,128526,128527,128528,128529,128530,128531,128532,128533,128534,128535,128536,128537,128538,128539,128540,128541,128542,128543,128544,128545,128546,128547,128548,128549,128550,128551,128552,128553,128554,128555,128556,128557,128558,128559,128560,128561,128562,128563,128564,128565,128566,128567,128568,128569,128570,128571,128572,128573,128574,128575,128576,128581,128582,128583,128584,128585,128586,128587,128588,128589,128590,128640,128641,128642,128643,128644,128645,128646,128647,128648,128649,128650,128651,128652,128653,128654,128655,128656,128657,128658,128659,128660,128661,128662,128663,128664,128665,128666,128667,128668,128669,128670,128671,128672,128673,128674,128675,128676,128677,128678,128679,128680,128682,128690,128692,128693,128694,128701,128703,128704,128705]
console.log('total emojis: ' + emojis.length)
console.log(encode('this is a text'))
console.log(decode('🌻🌟🌠🌺 🌠🌺 🌘 🌻🌜🌿🌻'))
console.log(decode(encode('this is a text')))
// console.log(decode('🌻🌟🌠🌺 🌠🌺 🌘 🌻🌜🌿🌻'))
// YOU CANNOT USE key
// console.log(encode('this is a text', 518))
//console.log(decode('☝🚿🛀☕ 🛀☕ 🚨 ☝🚵♥☝', 518))
function encode(text, key) {
// if key is not defined assume is 0
if (typeof key === "undefined") key = 0
function encode(text) {
// convert text into unicode points ( from ucs2 )
points = punycode.ucs2.decode(text)
// map points removing 33 ( because we ignore first 33 symbols )
var points = punycode.ucs2.decode(text)
// map points with emoji indexes
points = _.map(points, function (point) {
// if point is not a valid symbol return it
if (point < 33 || point > 255) return point
// subtract 33 and return
return point - 33
})
// map points with emojis index
points = _.map(points, function (point) {
// if point is not a valid symbol return it
if (point < 33 || point > 255) return point
// sum 33 and return
// console.log(point, (key + point), (key + point) % emojis.length)
return emojis[(key + point) % emojis.length]
if (!_.includes(chars, point)) return point
// subtract 33 ( because we ignore first 33 symbols in UNICODE )
var index = point - 33
// return emoji char at index position
return emojis[point]
})
// encode in ucs2
return punycode.ucs2.encode(points)
}
function decode (text, key) {
// if key is not defined assume is 0
if (typeof key === "undefined") key = 0
function decode (text) {
// convert text into unicode points ( from ucs2 )
points = punycode.ucs2.decode(text)
var points = punycode.ucs2.decode(text)
// map points with emojis index
points = _.map(points, function (point) {
// find index of point in emojis ( or -1 )
var item = _.findIndex(emojis, function (el) { return point === el })
// if point is found return item
// if point is found return it
if (item > 0) return item
// else return point
return point
})
// map points adding 33 ( removed in encode )
points = _.map(points, function (point) {
// if point is not a valid symbol return it
if (point < 33 || point > 255) return point
// sum 33 and return
return point + 33
})
// encode in ucs2
return punycode.ucs2.encode(points)
}