aotrautils-srv 0.0.53 → 0.0.56

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.
@@ -1,6 +1,6 @@
1
1
 
2
2
 
3
- /*utils SERVER library associated with aotra version : «1.0.0.000 (11/07/2022-23:40:34)»*/
3
+ /*utils SERVER library associated with aotra version : «1.0.0.000 (12/07/2022-01:26:53)»*/
4
4
  /*-----------------------------------------------------------------------------*/
5
5
 
6
6
 
@@ -337,12 +337,12 @@ WebsocketImplementation={
337
337
  },
338
338
  };
339
339
 
340
- // DBG
341
- console.log("ADD RECEPTION ENTRY POINT channelName:«"+channelName+"»! nodeServerInstance.receptionEntryPoints.length:",nodeServerInstance.receptionEntryPoints.length);
342
340
 
343
341
  nodeServerInstance.receptionEntryPoints.push(receptionEntryPoint);
344
342
 
345
-
343
+ // // DBG
344
+ // console.log("ADD RECEPTION ENTRY POINT channelName:«"+channelName+"»! nodeServerInstance.receptionEntryPoints.length:",nodeServerInstance.receptionEntryPoints.length);
345
+
346
346
  return nodeServerInstance;
347
347
  },
348
348
 
@@ -1019,8 +1019,7 @@ initNodeServer=function(doOnClientConnection=null, doOnFinalizeServer=null, /*OP
1019
1019
  }
1020
1020
  });
1021
1021
  isForceUnsecure=(argCLCertPath==="unsecure");
1022
-
1023
-
1022
+
1024
1023
 
1025
1024
  const aotraNodeServer={config:serverConfig};
1026
1025
  aotraNodeServer.serverManager={ start:()=>{/*DEFAULT START FUNCTION, WILL BE OVERRIDEN LATER*/}};
@@ -1299,7 +1298,7 @@ function getListManager(config){
1299
1298
 
1300
1299
 
1301
1300
 
1302
- /*utils COMMONS library associated with aotra version : «1.0.0.000 (11/07/2022-23:40:34)»*/
1301
+ /*utils COMMONS library associated with aotra version : «1.0.0.000 (12/07/2022-01:26:53)»*/
1303
1302
  /*-----------------------------------------------------------------------------*/
1304
1303
 
1305
1304
 
@@ -2743,6 +2742,299 @@ aotest.profile=function(rootObject,methodName,visited=[]){
2743
2742
 
2744
2743
 
2745
2744
 
2745
+
2746
+ //================================================================
2747
+ //================= Cryptography utility methods =================
2748
+ //================================================================
2749
+
2750
+
2751
+ // NOT AOTESTABLE !
2752
+ //TODO : develop :
2753
+ //getHashedString=monitorProgression(100000,
2754
+ window.getHashedString=function(str,/*OPTIONAL*/isHeavyTreatment){
2755
+
2756
+ //CAUTION : YOU MUST *NOT* CHANGE THIS VALUE, UNLESS THE HASH HEAVY TREATMENT VALUE IN YOUR AOTRA SERVER-SIDE CODEIS THE SAME !
2757
+ const HEAVY_TREATMENT_LOOP_COUNT=100000;
2758
+
2759
+ if(isHeavyTreatment){
2760
+
2761
+ // If needed, we make sure the hashing will take a long time, so that
2762
+ // if the private salt key is leaked, it will be fairly difficult to crack
2763
+ // password write protection and content encryption :
2764
+
2765
+ let result=str;
2766
+ for(var i=0;i<HEAVY_TREATMENT_LOOP_COUNT;i++){
2767
+ result=getHashedString(result,false);
2768
+ }
2769
+
2770
+
2771
+ return result;
2772
+
2773
+ }else{
2774
+
2775
+
2776
+ // SHA-256 hash (sucks less) :
2777
+ // DEPENDENCY : SJCL JS libray (Stanford Javascript Crypto Library)
2778
+ if(typeof(sjcl)!="undefined"){
2779
+ let result=sjcl.codec.hex.fromBits(sjcl.hash.sha256.hash(str));
2780
+
2781
+ // TODO : develop :
2782
+ // hash.progressionMonitor.progress(1);
2783
+
2784
+
2785
+ return result;
2786
+ }
2787
+
2788
+ // if not found : MD5 hash (sucks) :
2789
+ //TRACE
2790
+ log("WARN : Could not find SHA hashing, using MD5. This sucks, and this very likely will prevent your aotra client to be able to write data to aotra server.");
2791
+
2792
+ return hex_md5(str);
2793
+ }
2794
+ }
2795
+ //);
2796
+
2797
+ function encrypt(strToEncrypt,key,/*OPTIONAL*/mode){
2798
+ if(!mode || typeof(sjcl)=="undefined") mode="xor";
2799
+ mode=mode.trim();
2800
+
2801
+ if(contains(mode,"aes") && contains(mode,"xor")){
2802
+
2803
+ // XOR :
2804
+ var xorStr=btoa(encodeXORNoPattern(strToEncrypt,key));
2805
+
2806
+ var aesStr=
2807
+ // AES
2808
+ sjcl.encrypt(key,xorStr);
2809
+
2810
+ return aesStr;
2811
+ }
2812
+
2813
+ // DEPENDENCY : SJCL JS libray (Stanford Javascript Crypto Library)
2814
+ if(mode==="aes")
2815
+ return sjcl.encrypt(key,strToEncrypt);
2816
+ return btoa(encodeXORNoPattern(strToEncrypt,key));
2817
+
2818
+ }
2819
+
2820
+ function decrypt(strToDecrypt,key,/*OPTIONAL*/mode){
2821
+ if(!mode || typeof(sjcl)=="undefined") mode="xor";
2822
+ mode=mode.trim();
2823
+
2824
+ if(contains(mode,"aes") && contains(mode,"xor")){
2825
+
2826
+ // AES
2827
+ var aesStr=sjcl.decrypt(key,strToDecrypt);
2828
+
2829
+ var xorStr=
2830
+ // XOR :
2831
+ encodeXORNoPattern(atob(aesStr),key);
2832
+
2833
+ return xorStr;
2834
+
2835
+ }
2836
+
2837
+ // DEPENDENCY : SJCL JS libray (Stanford Javascript Crypto Library)
2838
+ if(mode==="aes")
2839
+ return sjcl.decrypt(key,strToDecrypt);
2840
+ return encodeXORNoPattern(atob(strToDecrypt),key);
2841
+
2842
+
2843
+ }
2844
+
2845
+ function encodeXORNoPattern(strToEncode,key){
2846
+ let result="";
2847
+ let keyChunk=key;
2848
+ let keyLength=keyChunk.length;
2849
+ let previousChars="";
2850
+ let cnt=0;
2851
+ for(let i=0;i<strToEncode.length;i++){
2852
+ let char=strToEncode.charAt(i);
2853
+ let keyByte=keyChunk.charAt(cnt);
2854
+
2855
+ result+=String.fromCharCode(char.charCodeAt(0)^keyByte.charCodeAt(0));
2856
+
2857
+ if(cnt%keyLength===(keyLength-1)){
2858
+ cnt=0;
2859
+ // NO : TOO LONG TREATMENT !: We hash with all the previous blocks too :
2860
+ // We hash with a character of the previous chunk (they add up at each new chunk):
2861
+ keyChunk=getHashedString(previousChars+keyChunk);
2862
+
2863
+ previousChars+=keyChunk.charAt(keyChunk.length-1);
2864
+ }else{
2865
+ cnt++;
2866
+ }
2867
+ }
2868
+ return result;
2869
+ }
2870
+
2871
+ function generateRandomString(length,/*NULLABLE*/mode){
2872
+
2873
+ // This list must be very conservative, because we want to be able to pass it through GET URLs !
2874
+ // OLD (not enough conservative ?) : const ALLOWED_CHARS="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0912346789_-£¢¤¬²³¼½¾";
2875
+ const ALLOWED_CHARS="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0912346789!-:;";
2876
+
2877
+
2878
+ if(typeof(length)==="string"){
2879
+ if(contains(length,"->")){
2880
+ length=Math.getRandomInRange(length);
2881
+ }else{
2882
+ length=64;
2883
+ }
2884
+ }
2885
+
2886
+ var result="";
2887
+
2888
+ if(mode==="simple"){
2889
+
2890
+ for(var i=0;i<length;i++){
2891
+ var value=Math.getRandomInArray(ALLOWED_CHARS);
2892
+ result+=value;
2893
+ }
2894
+
2895
+ return result;
2896
+ }else if(mode==="human"){
2897
+
2898
+ const markovGraph={
2899
+ // For simplicity sakes, we ignore the consonants and vowels doubling possibilities:
2900
+ consonants:{// They always accept vowels at their following
2901
+ // Indicate ^ to specify wich of the other group letter to exclude as direct follower of the given letter :
2902
+ "b":["r","l"],
2903
+ "c":["h","t","k","l","r"],
2904
+ "d":["r"],
2905
+ "f":["r","l"],
2906
+ "g":["r","l"],
2907
+ "h":[],
2908
+ "j":["r"],
2909
+ "k":["r","l","t","h"],
2910
+ "l":["t"],
2911
+ "m":["b","p"],
2912
+ "n":["t","d"],
2913
+ "p":["h","r","s","t","l"],
2914
+ "q":["r","l"],
2915
+ "r":["t","c","d","k","q","h"],
2916
+ "s":["t","c","d","k","q","h"],
2917
+ "t":["r","h"],
2918
+ "v":["r","l"],
2919
+ "w":["r"],
2920
+ "x":["p"],
2921
+ "z":["r","h"],
2922
+ },
2923
+ vowels:{// They always accept consonants at their following
2924
+ "a":["i","u","y"],
2925
+ "e":["i","u","y"],
2926
+ "i":["e","o"],
2927
+ "o":["i","u"],
2928
+ "u":["i"],
2929
+ "y":[],
2930
+ },
2931
+ };
2932
+
2933
+ const LETTERS_TYPES=Object.keys(markovGraph);
2934
+
2935
+ const CURR_LETTER_MODE=Math.getRandomInArray(LETTERS_TYPES);
2936
+ const POSSIBLE_LETTERS=Object.keys(markovGraph[CURR_LETTER_MODE]);
2937
+ const CURR_LETTER=Math.getRandomInArray(POSSIBLE_LETTERS);
2938
+
2939
+ var automataState={
2940
+ currentLetterMode:CURR_LETTER_MODE,
2941
+ possibleLetters:POSSIBLE_LETTERS,
2942
+ currentLetter:CURR_LETTER,
2943
+
2944
+ calculateLetter:function(){
2945
+
2946
+ this.currentLetter=Math.getRandomInArray(this.possibleLetters);
2947
+
2948
+ var sameLetters=Object.keys( markovGraph[this.currentLetterMode] );
2949
+ if(!contains(sameLetters,this.currentLetter)){
2950
+ // If we picked from the same group : we don't change of group to pick
2951
+ // If we picked from the other group : we do change of group to pick
2952
+
2953
+ var otherLetterMode=foreach(LETTERS_TYPES,(item)=>{
2954
+ if(item!==this.currentLetterMode) return item;
2955
+ });
2956
+
2957
+ this.currentLetterMode=otherLetterMode;
2958
+ }
2959
+
2960
+ },
2961
+
2962
+ updatePossibleLettersForLetter:function(){
2963
+
2964
+ var nextPossibleLetters=[];
2965
+ var nextNotPossibleLetters=[];
2966
+
2967
+ // We store those of the other group marked «^»
2968
+ foreach(markovGraph[this.currentLetterMode][this.currentLetter],(letter)=>{
2969
+ if(letter.charAt(0)==="^"){
2970
+ nextNotPossibleLetters.push(letter.charAt(1));
2971
+ }else{
2972
+ nextPossibleLetters.push(letter);
2973
+ }
2974
+ });
2975
+
2976
+ var otherLetterMode=foreach(LETTERS_TYPES,(item)=>{
2977
+ if(item!==this.currentLetterMode) return item;
2978
+ });
2979
+ var otherLetters=Object.keys(markovGraph[otherLetterMode]);
2980
+
2981
+ // We exclude those marked «^»
2982
+ foreach( otherLetters ,(letter)=>{
2983
+ if(!contains(nextNotPossibleLetters,letter)){
2984
+ nextPossibleLetters.push(letter);
2985
+ }
2986
+ });
2987
+
2988
+ this.possibleLetters=nextPossibleLetters;
2989
+ }
2990
+ };
2991
+
2992
+ var i=0;
2993
+ do{
2994
+
2995
+ result+=automataState.currentLetter;
2996
+
2997
+ automataState.updatePossibleLettersForLetter();
2998
+ automataState.calculateLetter();
2999
+
3000
+ i++;
3001
+ }while(i<length);
3002
+
3003
+ return result;
3004
+ }
3005
+
3006
+ for(var i=0;i<length;i++){
3007
+ var value=Math.getRandomInt(255,0);
3008
+ result+=String.fromCharCode(value);
3009
+ }
3010
+
3011
+ if(mode==="base64"){
3012
+ return btoa(result);
3013
+ }
3014
+
3015
+ return result;
3016
+ }
3017
+
3018
+
3019
+
3020
+ // NOT AOTESTABLE !
3021
+ function getUniqueIdWithDate(){
3022
+ return getHashedString(getNow() + "");
3023
+ }
3024
+
3025
+
3026
+
3027
+
3028
+
3029
+
3030
+ //================================================================
3031
+ //================= Miscelanneous utility methods =================
3032
+ //================================================================
3033
+
3034
+
3035
+
3036
+
3037
+
2746
3038
  // NOT AOTESTABLE !
2747
3039
 
2748
3040
  // This function is for simple variables, like strings, booleans, or Objects you
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "aotrautils-srv",
3
- "version": "0.0.53",
3
+ "version": "0.0.56",
4
4
  "main": "aotrautils-srv.build.js",
5
5
  "description": "A library for vanilla javascript utils (server-side) used in aotra javascript CMS",
6
6
  "author": "Jeremie Ratomposon <info@alqemia.com> (https://alqemia.com)",