fixed_length_encoder 1.1.0 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/fixed_length_encoder.rb +89 -33
- metadata +4 -4
data/lib/fixed_length_encoder.rb
CHANGED
@@ -1,70 +1,126 @@
|
|
1
1
|
module FixedLengthEncoder
|
2
|
-
ALPHABET = '
|
2
|
+
ALPHABET = '0123456789abcdefghijklmnopqrstuvwxyz'
|
3
|
+
ENCODE_MAP = [406, 828, 85, 1284, 383, 878, 1066, 542, 665, 434, 270, 1150, 1016, 571, 379, 654, 1194, 1078, 148, 11, 798, 1102, 398, 23, 287, 1293, 50, 9, 1159, 1274, 971, 1031, 144, 812, 868, 574, 65, 1155, 314, 155, 512, 423, 1158, 597, 158, 1097, 966, 231, 202, 494, 1015, 594, 446, 1092, 755, 1285, 716, 705, 200, 482, 282, 1113, 1145, 123, 866, 779, 96, 95, 271, 1243, 1259, 825, 960, 1246, 262, 976, 1247, 1024, 484, 954, 1010, 918, 1088, 374, 690, 1149, 280, 540, 206, 1204, 1230, 1154, 838, 879, 1105, 733, 713, 579, 378, 1128, 1118, 41, 283, 967, 500, 849, 824, 176, 648, 455, 416, 552, 714, 722, 886, 910, 573, 420, 602, 737, 266, 901, 373, 959, 97, 1265, 750, 576, 1100, 1107, 851, 1235, 33, 48, 69, 146, 708, 1180, 852, 1253, 548, 36, 192, 1169, 337, 605, 1009, 793, 627, 75, 827, 1091, 870, 634, 466, 847, 958, 117, 331, 917, 659, 528, 986, 672, 286, 1127, 570, 1193, 608, 198, 453, 452, 357, 393, 1249, 1187, 1101, 1070, 483, 1134, 174, 1272, 72, 1182, 603, 1027, 950, 204, 1017, 524, 947, 1166, 893, 1271, 107, 1080, 1289, 1124, 1283, 909, 529, 316, 888, 836, 1181, 598, 728, 694, 244, 242, 1260, 835, 1042, 291, 278, 309, 468, 633, 1052, 1074, 589, 139, 411, 361, 857, 215, 523, 1004, 34, 433, 625, 1147, 642, 499, 773, 638, 118, 425, 985, 1210, 109, 288, 635, 513, 640, 837, 414, 84, 511, 1076, 1045, 717, 730, 945, 983, 1103, 369, 3, 1098, 731, 124, 59, 898, 568, 861, 496, 744, 892, 1196, 47, 803, 444, 254, 265, 74, 1160, 351, 1069, 1086, 1286, 68, 1171, 905, 241, 531, 1094, 818, 1035, 1106, 179, 306, 190, 44, 39, 273, 590, 0, 51, 669, 298, 521, 686, 359, 289, 308, 421, 145, 1049, 738, 323, 946, 973, 981, 834, 130, 405, 805, 112, 645, 1179, 1126, 624, 1280, 1060, 811, 349, 843, 1064, 251, 135, 680, 28, 618, 943, 815, 151, 1266, 1189, 228, 609, 147, 862, 575, 1058, 790, 1029, 63, 1000, 651, 536, 932, 1032, 438, 1174, 141, 247, 1245, 131, 1055, 809, 643, 760, 988, 952, 891, 430, 467, 368, 25, 543, 10, 450, 185, 922, 341, 327, 1121, 982, 82, 245, 999, 248, 1212, 2, 1019, 993, 473, 229, 1111, 948, 897, 1090, 193, 1083, 1198, 1023, 389, 965, 899, 201, 46, 1258, 307, 721, 400, 328, 358, 768, 979, 819, 785, 31, 1012, 325, 6, 221, 880, 987, 92, 53, 792, 657, 1203, 673, 1252, 488, 237, 518, 1214, 497, 980, 956, 675, 1048, 1162, 401, 210, 396, 656, 1219, 702, 76, 1232, 846, 360, 1117, 142, 535, 1224, 1261, 106, 99, 800, 788, 896, 515, 343, 86, 220, 1195, 726, 514, 505, 353, 771, 762, 301, 391, 207, 1056, 729, 381, 1292, 356, 183, 1263, 127, 437, 218, 163, 1220, 584, 1095, 60, 321, 1264, 735, 832, 230, 975, 639, 1167, 471, 58, 491, 246, 696, 427, 560, 796, 346, 1036, 1125, 855, 332, 1116, 626, 1079, 150, 1184, 588, 394, 392, 302, 143, 186, 546, 703, 1215, 239, 895, 962, 1018, 338, 290, 776, 679, 426, 157, 551, 78, 62, 928, 312, 272, 1168, 205, 480, 94, 924, 856, 907, 604, 676, 677, 1084, 682, 326, 1011, 525, 953, 806, 740, 1141, 1139, 628, 858, 1291, 375, 501, 1062, 510, 1185, 213, 1269, 1165, 650, 671, 984, 746, 754, 119, 913, 582, 1278, 580, 610, 585, 180, 300, 1030, 1201, 73, 684, 885, 1241, 165, 572, 1254, 388, 914, 859, 355, 462, 1290, 339, 701, 243, 925, 279, 362, 989, 951, 808, 718, 259, 789, 1022, 882, 212, 297, 1123, 687, 5, 1133, 152, 935, 294, 685, 927, 1206, 26, 403, 1151, 641, 15, 1047, 1136, 516, 1191, 126, 507, 1114, 125, 372, 1, 335, 970, 774, 944, 1099, 937, 98, 318, 919, 756, 364, 197, 538, 156, 517, 277, 724, 435, 317, 1161, 1037, 330, 942, 957, 134, 506, 583, 276, 795, 492, 553, 199, 1238, 688, 829, 758, 252, 853, 422, 1200, 303, 485, 440, 1295, 493, 864, 1020, 175, 89, 1223, 926, 195, 1279, 470, 459, 668, 1051, 961, 256, 322, 689, 348, 869, 550, 371, 664, 620, 61, 596, 479, 678, 902, 376, 1043, 88, 227, 1248, 489, 904, 1053, 850, 741, 116, 1270, 13, 1085, 992, 766, 502, 1156, 938, 1234, 1104, 177, 978, 1108, 1002, 249, 646, 284, 164, 1163, 172, 448, 709, 844, 767, 240, 1175, 386, 692, 770, 700, 593, 561, 27, 187, 1218, 21, 787, 1132, 333, 424, 181, 877, 1039, 194, 1153, 1119, 977, 1227, 503, 439, 1251, 712, 881, 404, 1231, 871, 1233, 565, 38, 1226, 37, 555, 1157, 461, 261, 120, 121, 1276, 233, 441, 1109, 930, 831, 12, 464, 81, 1148, 93, 526, 1065, 810, 704, 994, 711, 955, 968, 52, 315, 18, 1013, 490, 14, 1081, 557, 674, 732, 8, 662, 778, 412, 1110, 22, 995, 257, 1033, 599, 784, 64, 1096, 1213, 305, 232, 45, 623, 1115, 350, 189, 1211, 1209, 428, 764, 1236, 454, 1225, 1059, 281, 295, 749, 563, 681, 101, 320, 1038, 777, 35, 436, 698, 509, 334, 196, 1001, 921, 814, 848, 1288, 539, 133, 390, 1112, 1050, 1256, 495, 310, 29, 173, 236, 822, 632, 113, 1277, 340, 275, 747, 1129, 56, 432, 402, 972, 782, 1221, 1222, 734, 234, 347, 933, 214, 549, 794, 1250, 830, 149, 469, 615, 911, 667, 377, 299, 329, 1072, 263, 826, 537, 1122, 1028, 1202, 409, 934, 445, 1044, 208, 670, 1014, 889, 1217, 840, 1197, 820, 693, 1199, 209, 269, 757, 595, 87, 477, 1093, 1281, 752, 395, 617, 1172, 629, 1237, 417, 1143, 188, 19, 397, 1067, 655, 520, 647, 311, 903, 463, 706, 890, 161, 79, 486, 465, 522, 203, 997, 474, 1239, 780, 1041, 614, 235, 920, 940, 67, 586, 1244, 743, 644, 697, 293, 556, 873, 1178, 70, 49, 1275, 418, 1208, 238, 258, 222, 751, 366, 1267, 802, 57, 304, 567, 475, 683, 591, 666, 799, 661, 1082, 336, 867, 7, 839, 908, 530, 1063, 541, 720, 763, 931, 458, 32, 1192, 42, 715, 600, 710, 344, 365, 345, 160, 783, 110, 619, 408, 1242, 748, 191, 380, 255, 1240, 313, 1186, 745, 753, 226, 1077, 527, 324, 399, 114, 649, 791, 451, 974, 906, 929, 823, 354, 1021, 916, 456, 83, 863, 631, 1089, 138, 545, 129, 1183, 169, 727, 606, 658, 1140, 772, 559, 1005, 472, 1087, 407, 429, 1003, 883, 296, 1068, 24, 845, 652, 725, 363, 171, 653, 1025, 487, 80, 616, 554, 292, 30, 66, 801, 481, 410, 1287, 544, 1190, 419, 532, 1135, 431, 17, 460, 912, 40, 964, 352, 817, 1007, 20, 612, 872, 285, 128, 182, 159, 1152, 385, 874, 1131, 969, 1228, 990, 442, 168, 884, 382, 797, 447, 1073, 115, 581, 137, 91, 611, 1257, 1006, 991, 1057, 108, 804, 996, 816, 504, 1142, 225, 153, 105, 1229, 415, 691, 370, 140, 759, 860, 699, 663, 1216, 384, 833, 43, 894, 765, 1164, 941, 1207, 923, 71, 695, 55, 508, 587, 1177, 111, 636, 723, 630, 178, 136, 875, 1144, 963, 1120, 319, 250, 54, 1008, 876, 775, 268, 569, 821, 260, 742, 1262, 637, 998, 1176, 533, 1054, 1075, 622, 1188, 90, 1071, 184, 707, 607, 547, 1034, 170, 162, 342, 1146, 900, 77, 154, 1205, 1130, 457, 566, 719, 1273, 558, 16, 786, 915, 739, 413, 217, 1294, 223, 253, 578, 601, 534, 167, 476, 854, 564, 781, 216, 613, 1268, 387, 807, 769, 736, 887, 1138, 621, 449, 1137, 939, 761, 4, 1170, 562, 1255, 274, 841, 104, 577, 102, 1046, 367, 865, 443, 132, 100, 498, 478, 592, 936, 211, 660, 103, 949, 1282, 519, 166, 1026, 1173, 1061, 224, 842, 267, 122, 1040, 813, 219, 264]
|
4
|
+
DECODE_MAP = [296, 635, 383, 257, 1259, 613, 414, 1005, 815, 27, 370, 19, 792, 720, 810, 625, 1228, 1105, 807, 945, 1113, 754, 820, 23, 1080, 368, 621, 751, 331, 872, 1093, 411, 1015, 132, 228, 853, 141, 779, 777, 293, 1108, 101, 1017, 1164, 292, 831, 400, 269, 133, 982, 26, 297, 805, 419, 1189, 1173, 883, 993, 493, 261, 483, 703, 531, 346, 826, 36, 1094, 971, 280, 134, 981, 1171, 182, 582, 274, 149, 441, 1219, 530, 957, 1089, 794, 378, 1056, 247, 2, 457, 932, 710, 684, 1207, 1137, 418, 796, 538, 67, 66, 124, 642, 451, 1273, 849, 1267, 1280, 1265, 1151, 450, 194, 1143, 240, 1026, 1177, 317, 877, 1044, 1134, 718, 157, 236, 571, 784, 785, 1291, 63, 260, 633, 630, 476, 1117, 1062, 314, 357, 1272, 865, 660, 329, 1182, 1136, 1060, 221, 1156, 354, 446, 514, 32, 306, 135, 340, 18, 899, 508, 335, 615, 1150, 1220, 39, 649, 528, 44, 1119, 1024, 956, 1215, 479, 736, 586, 1284, 1240, 1128, 1064, 1214, 1085, 738, 873, 180, 683, 107, 729, 1181, 289, 578, 759, 1118, 474, 1209, 372, 515, 752, 944, 835, 291, 1031, 142, 392, 762, 687, 858, 647, 169, 667, 58, 399, 48, 961, 187, 536, 88, 468, 918, 928, 436, 1278, 609, 563, 894, 225, 1245, 1233, 478, 1294, 458, 415, 988, 1235, 1288, 1149, 1039, 711, 338, 387, 488, 47, 830, 787, 891, 968, 874, 426, 986, 519, 743, 283, 209, 597, 208, 379, 495, 355, 381, 733, 1188, 328, 672, 1236, 272, 1033, 694, 822, 987, 605, 1196, 783, 74, 908, 1295, 273, 120, 1290, 1193, 929, 10, 68, 534, 294, 1263, 880, 663, 651, 214, 599, 86, 844, 60, 102, 735, 1116, 164, 24, 241, 303, 524, 213, 1092, 977, 617, 845, 1078, 610, 299, 905, 579, 466, 513, 676, 994, 829, 290, 402, 304, 215, 871, 951, 533, 1035, 38, 806, 201, 654, 643, 1187, 850, 484, 695, 309, 1042, 413, 547, 375, 405, 906, 657, 158, 504, 757, 857, 636, 1003, 144, 523, 595, 879, 374, 1216, 456, 1021, 1023, 500, 892, 697, 325, 834, 276, 1110, 463, 1052, 592, 473, 172, 406, 302, 444, 223, 600, 1084, 646, 1022, 990, 1269, 367, 256, 1155, 700, 634, 122, 83, 558, 708, 904, 98, 14, 1032, 471, 1130, 4, 1162, 1121, 745, 1248, 589, 396, 866, 467, 512, 173, 511, 937, 437, 946, 22, 1043, 404, 435, 885, 622, 772, 315, 0, 1074, 1028, 914, 1097, 222, 818, 1232, 246, 1153, 110, 942, 984, 1101, 117, 305, 674, 41, 758, 237, 527, 497, 838, 1075, 365, 1104, 884, 229, 9, 653, 854, 477, 352, 768, 678, 788, 1127, 1271, 271, 916, 52, 1132, 739, 1255, 371, 1047, 171, 170, 841, 109, 1055, 1223, 1014, 690, 1106, 782, 593, 953, 793, 959, 154, 366, 216, 900, 689, 492, 1072, 386, 963, 996, 1241, 933, 1275, 705, 537, 1096, 59, 178, 78, 677, 958, 1088, 425, 713, 809, 494, 665, 680, 49, 870, 265, 429, 1274, 233, 104, 559, 724, 767, 1147, 462, 661, 631, 1174, 856, 561, 248, 40, 243, 461, 455, 628, 650, 427, 1283, 949, 300, 960, 226, 189, 549, 797, 1041, 161, 200, 1008, 284, 1102, 1202, 1239, 447, 349, 910, 648, 864, 87, 1010, 7, 369, 1099, 1061, 516, 1212, 140, 895, 699, 529, 111, 666, 1091, 780, 978, 812, 1227, 1070, 498, 750, 1261, 847, 1243, 776, 1224, 995, 263, 1194, 166, 13, 587, 116, 35, 342, 127, 1266, 1237, 97, 575, 1135, 573, 662, 481, 577, 972, 1175, 510, 220, 295, 998, 1276, 749, 51, 931, 704, 43, 205, 824, 1019, 1238, 118, 184, 542, 145, 1066, 1211, 168, 339, 576, 1138, 1114, 1246, 967, 901, 1090, 938, 332, 1027, 702, 1254, 1205, 832, 321, 230, 506, 148, 555, 940, 1180, 1058, 876, 217, 153, 242, 1178, 1199, 235, 490, 244, 624, 232, 360, 975, 318, 734, 950, 108, 1045, 566, 348, 1082, 1086, 15, 948, 438, 421, 1067, 160, 1279, 1001, 816, 1160, 701, 8, 999, 903, 691, 298, 919, 567, 163, 423, 813, 432, 543, 544, 706, 526, 330, 848, 546, 997, 583, 618, 301, 612, 669, 696, 84, 1154, 746, 926, 207, 1172, 496, 976, 855, 1159, 748, 596, 440, 517, 800, 57, 954, 1210, 136, 740, 1020, 802, 770, 96, 112, 1018, 56, 251, 604, 1225, 1011, 403, 113, 1179, 652, 1083, 460, 1065, 206, 470, 252, 259, 814, 95, 890, 486, 1251, 119, 308, 1231, 552, 717, 1197, 974, 266, 1037, 569, 881, 1030, 846, 126, 989, 936, 1038, 570, 54, 645, 930, 671, 1157, 361, 1258, 465, 1012, 839, 1166, 723, 742, 407, 1250, 747, 464, 1069, 234, 638, 1192, 525, 852, 817, 65, 965, 1244, 887, 1025, 825, 410, 1229, 755, 453, 606, 344, 1046, 420, 147, 896, 664, 499, 1131, 20, 1000, 452, 1095, 992, 270, 1144, 316, 551, 1249, 603, 359, 799, 324, 33, 1293, 861, 334, 1146, 1111, 286, 409, 925, 1195, 875, 1051, 106, 71, 909, 150, 1, 670, 898, 791, 487, 1163, 313, 211, 203, 245, 92, 1006, 923, 1264, 1289, 326, 741, 1081, 443, 155, 862, 105, 716, 130, 138, 673, 1242, 503, 540, 224, 556, 591, 1158, 264, 341, 1057, 681, 1270, 64, 1004, 34, 698, 152, 774, 1115, 979, 1122, 1183, 1191, 760, 5, 93, 416, 771, 608, 1077, 1129, 584, 114, 1252, 202, 921, 955, 364, 267, 192, 1165, 520, 454, 390, 262, 398, 1218, 121, 707, 952, 714, 282, 1049, 541, 1007, 199, 115, 902, 1107, 572, 590, 1230, 1054, 159, 81, 644, 969, 860, 373, 1170, 539, 598, 686, 619, 532, 1050, 790, 1013, 350, 893, 915, 616, 1277, 641, 726, 1257, 970, 1168, 658, 333, 639, 253, 310, 190, 389, 1281, 186, 602, 363, 550, 79, 803, 431, 659, 156, 123, 72, 693, 521, 1185, 1109, 397, 46, 103, 804, 1124, 637, 30, 886, 311, 1048, 489, 75, 765, 730, 408, 430, 312, 377, 254, 568, 238, 162, 417, 362, 601, 1126, 1141, 722, 385, 801, 821, 1145, 962, 1200, 380, 347, 859, 732, 1076, 227, 1071, 1140, 1112, 1190, 146, 80, 548, 412, 808, 920, 50, 12, 188, 522, 384, 682, 1053, 607, 395, 77, 1087, 1285, 185, 912, 345, 580, 31, 351, 823, 1213, 287, 501, 656, 851, 761, 1292, 966, 212, 709, 917, 250, 1268, 626, 433, 307, 868, 692, 218, 715, 1203, 358, 469, 1142, 343, 843, 323, 1287, 560, 1009, 327, 798, 6, 947, 1079, 277, 177, 1208, 907, 1133, 219, 1204, 249, 1040, 17, 507, 195, 811, 1002, 393, 545, 721, 278, 1073, 82, 1059, 391, 151, 53, 934, 285, 482, 827, 45, 258, 640, 128, 176, 21, 255, 728, 94, 288, 129, 731, 789, 819, 388, 867, 61, 632, 833, 505, 445, 100, 764, 1186, 376, 911, 611, 197, 502, 320, 165, 99, 882, 1222, 1123, 756, 614, 179, 1103, 627, 1256, 1253, 554, 1068, 553, 1148, 943, 1184, 62, 1217, 231, 795, 85, 11, 623, 1120, 763, 91, 37, 725, 781, 42, 28, 275, 655, 434, 737, 1167, 565, 191, 491, 535, 143, 1260, 281, 939, 1286, 353, 744, 1201, 1176, 980, 319, 137, 204, 183, 1063, 509, 562, 1036, 175, 1206, 337, 1100, 629, 1016, 167, 16, 459, 268, 924, 394, 927, 675, 581, 913, 422, 89, 1221, 620, 1169, 985, 837, 239, 836, 382, 828, 428, 518, 1161, 922, 753, 439, 480, 888, 889, 685, 448, 842, 778, 766, 1125, 1152, 90, 773, 442, 775, 727, 131, 840, 941, 668, 964, 1034, 585, 1029, 69, 973, 356, 73, 76, 712, 174, 897, 769, 424, 139, 588, 1262, 869, 1139, 401, 70, 210, 449, 1198, 475, 485, 125, 336, 991, 1247, 564, 719, 193, 181, 1226, 29, 983, 786, 878, 574, 688, 322, 935, 1282, 198, 3, 55, 279, 1098, 863, 196, 594, 557, 472, 25, 1234, 679]
|
3
5
|
LENGTH = 8
|
4
6
|
|
5
7
|
def self.encode(value, message_length = nil)
|
6
8
|
message_length = LENGTH if message_length.nil?
|
7
|
-
encoder = FixedLengthEncoder::Encoder.new(ALPHABET)
|
9
|
+
encoder = FixedLengthEncoder::Encoder.new(ALPHABET, ENCODE_MAP, DECODE_MAP)
|
8
10
|
encoder.encode(value, message_length)
|
9
11
|
end
|
10
12
|
|
11
13
|
def self.decode(message)
|
12
|
-
encoder = FixedLengthEncoder::Encoder.new(ALPHABET)
|
14
|
+
encoder = FixedLengthEncoder::Encoder.new(ALPHABET, ENCODE_MAP, DECODE_MAP)
|
13
15
|
encoder.decode(message)
|
14
16
|
end
|
15
17
|
|
18
|
+
def self.isValidAlphabet
|
19
|
+
valid = ALPHABET.split('').uniq.length == ALPHABET.length
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.isValidMap
|
23
|
+
length = ALPHABET.length*ALPHABET.length
|
24
|
+
valid = ENCODE_MAP.length == length && DECODE_MAP.length == length
|
25
|
+
if (valid)
|
26
|
+
(0..(length-1)).each do |i|
|
27
|
+
valid = (DECODE_MAP[ENCODE_MAP[i]] == i) if (valid)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
valid == true
|
31
|
+
end
|
32
|
+
|
16
33
|
class Encoder
|
17
|
-
def initialize(alphabet)
|
34
|
+
def initialize(alphabet, encode_map, decode_map)
|
18
35
|
@alphabet = alphabet
|
36
|
+
@base = alphabet.length
|
37
|
+
@encode_map = encode_map
|
38
|
+
@decode_map = decode_map
|
19
39
|
end
|
20
40
|
|
21
41
|
def setup(message_length)
|
22
42
|
@message_length = message_length
|
23
|
-
@
|
24
|
-
@max_bits = Math::log(@max_message_value, 2).floor
|
25
|
-
@max_value = 2**@max_bits
|
43
|
+
@max_value = (@alphabet.length)**message_length
|
26
44
|
end
|
27
45
|
|
28
46
|
def encode(value, message_length)
|
29
|
-
raise ArgumentError, 'Cannot encode a non-integer
|
47
|
+
raise ArgumentError, 'Cannot encode a non-integer' unless value.is_a?(Integer)
|
48
|
+
raise ArgumentError, 'Cannot encode negative values' if value < 0
|
30
49
|
self.setup(message_length)
|
31
50
|
raise ArgumentError, "Cannot encode #{value} in #{@message_length} characters" if value >= @max_value
|
32
|
-
value = self.
|
33
|
-
self.
|
51
|
+
value = self.offset(value, +1)
|
52
|
+
value = self.scramble_value(value, +1)
|
53
|
+
string = self.integer_to_string(value)
|
54
|
+
end
|
55
|
+
|
56
|
+
def decode(message)
|
57
|
+
raise ArgumentError, 'Cannot decode a non-string.' unless message.is_a?(String)
|
58
|
+
self.setup(message.length)
|
59
|
+
value = self.string_to_integer(message)
|
60
|
+
value = self.scramble_value(value, -1)
|
61
|
+
self.offset(value, -1)
|
62
|
+
end
|
63
|
+
|
64
|
+
def offset(value, direction)
|
65
|
+
offset = (@max_value/2).floor
|
66
|
+
(value += offset) if direction > 0
|
67
|
+
(value -= offset) if direction < 0
|
68
|
+
(value += @max_value) if value < 0
|
69
|
+
(value -= @max_value) if value >= @max_value
|
70
|
+
value
|
34
71
|
end
|
35
72
|
|
36
|
-
def
|
37
|
-
message =
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
73
|
+
def scramble_value(value, direction)
|
74
|
+
message = integer_to_array(value)
|
75
|
+
message = map_array(message, @encode_map, direction) if direction > 0
|
76
|
+
message = map_array(message, @decode_map, direction) if direction < 0
|
77
|
+
array_to_integer(message)
|
78
|
+
end
|
79
|
+
|
80
|
+
def map_array(message, map, direction)
|
81
|
+
indexes = (1..(@message_length - 1)).to_a
|
82
|
+
indexes = indexes.reverse if direction < 0
|
83
|
+
indexes.each do |index|
|
84
|
+
low = message[index - 1]
|
85
|
+
high = message[index]
|
86
|
+
map_index = high * @base + low
|
87
|
+
map_value = map[map_index]
|
88
|
+
message[index - 1] = map_value % @base
|
89
|
+
message[index] = map_value / @base
|
43
90
|
end
|
44
91
|
message
|
45
92
|
end
|
46
93
|
|
47
|
-
def
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
94
|
+
def integer_to_string(value)
|
95
|
+
message = integer_to_array(value)
|
96
|
+
message = message.map { |value| @alphabet[value] }
|
97
|
+
message.join
|
98
|
+
end
|
99
|
+
|
100
|
+
def integer_to_array(value)
|
101
|
+
message = []
|
102
|
+
while (value > 0 || message.length < @message_length)
|
103
|
+
remainder = value % @base
|
104
|
+
message.push remainder
|
105
|
+
value /= @base
|
106
|
+
end
|
107
|
+
message
|
52
108
|
end
|
53
109
|
|
54
|
-
def
|
55
|
-
base = alphabet.length
|
110
|
+
def array_to_integer(message)
|
56
111
|
value = 0
|
57
|
-
message.reverse.
|
58
|
-
|
112
|
+
message.reverse.each { |digit| value = (value * @base) + digit }
|
113
|
+
value
|
114
|
+
end
|
115
|
+
|
116
|
+
def string_to_integer(message)
|
117
|
+
value = 0
|
118
|
+
message.reverse.split('').map do |digit|
|
119
|
+
index = @alphabet.index(digit)
|
59
120
|
raise ArgumentError, 'Cannot decode an invalid character (' + digit + ')' if index.nil?
|
60
|
-
value = (value * base) + index
|
121
|
+
value = (value * @base) + index
|
61
122
|
end
|
62
123
|
value
|
63
|
-
end
|
64
|
-
|
65
|
-
def scramble_value(value, bits)
|
66
|
-
binary = self.integer_to_string('10', value, bits).reverse
|
67
|
-
self.string_to_integer('01', binary)
|
68
124
|
end
|
69
125
|
end
|
70
|
-
end
|
126
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fixed_length_encoder
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.2.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,10 +9,10 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2013-01-28 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
|
-
description: Integers are converted to strings using a
|
15
|
-
|
14
|
+
description: Integers are converted to strings using a complex mapping and a base
|
15
|
+
36 alphabet. Strings with valid digits (0-9 a-z)are converted back to integers. The
|
16
16
|
encoding is one-to-one but not sequential.This is useful for obfuscating user ids
|
17
17
|
in urls.
|
18
18
|
email: brett@paperyfrog.com
|