renamed variables to be more self explanatory, improved split function
authorStephan Richter <github@keawe.de>
Fri, 13 Oct 2017 10:53:46 +0000 (12:53 +0200)
committerStephan Richter <github@keawe.de>
Fri, 13 Oct 2017 10:53:46 +0000 (12:53 +0200)
src/secret.html

index 996ccd23e9686dfffbdafd05f61ebbc59270649f..dd2a677a073890e11af1ce2eac7cb2c168ce933d 100644 (file)
 </style>
 <script type="text/javascript">
 var prime=257;
-function e(e,t){return '<'+e+'>'+t+'</'+e+'>';} // html element with content t
-function g(i){return document.getElementById(i);}
-function h(i,t){g(i).innerHTML=t};
-function v(i){return g(i).value;} // value of element with id
+function createElement(e,t){return '<'+e+'>'+t+'</'+e+'>';} // html element with content t
+function getElement(i){return document.getElementById(i);}
+function setHtmlOf(i,t){getElement(i).innerHTML=t};
+function valueOfField(i){return getElement(i).value;} // value of element with id
 
+function rand256(){
+       return Math.floor(Math.random() * 256);
+}
 
 /* Split number into the shares */
-function split(number, available, needed) {
-    var coef = [], x, exp, c, accum, shares = [];
-    for(c = 1, coef[0] = number; c < needed; c++) coef[c] = Math.floor(Math.random() * (prime  - 1));
-    for(x = 1; x <= available; x++) {
-        for(exp = 1, accum = coef[0]; exp < needed; exp++) accum = (accum + (coef[exp] * (Math.pow(x, exp) % prime) % prime)) % prime;
-        shares[x - 1] = accum;
+function split(number, number_of_shares, needed) {
+    var coef = [number]
+    var shares = [];
+
+    for(var coef_index = 1; coef_index < needed; coef_index++){
+       coef[coef_index] = rand256();
+    }
+    
+    for(var share_number = 1; share_number <= number_of_shares; share_number++) {
+       var sum = 0;
+       for (var coef_index =0; coef_index<needed; coef_index++){
+               sum += coef[coef_index]*Math.pow(share_number,coef_index);
+       }
+       
+       shares[share_number-1] = sum % prime; 
     }
     return shares;
 }
@@ -153,41 +165,41 @@ function decode(){
        }
 }
 
-function secret2numbers(s){
-       var i,j,coef,n,x,sum;
-       var shares = v('shares');
-       var treshold = v('treshold');
+function secret2numbers(string){
+       var string_pos,share_number,coef,char_code,x,sum;
+       var number_of_shares = valueOfField('shares');
+       var treshold             = valueOfField('treshold');
        var results = [];
-       var th=e('th','char');;
-       var ns=e('th','ascii');;
        
-       for (i=0; i<s.length;i++) {
-               n=s.charCodeAt(i);
-               th+=e('td',s[i]);
-               ns+=e('td',n);
-               
-               coef=[n];
-               for (j=1; j<treshold;j++) coef[j]=Math.floor(Math.random()*prime);
+       // create table elements for displaying the shares
+       var char_cells      = createElement('th','char');;
+       var char_code_cells = createElement('th','ascii');;
+       
+       // decompose string, for each character create secrets from asccii code
+       for (string_pos=0; string_pos<string.length;string_pos++) {
+               char_code        = string.charCodeAt(string_pos);
+               char_cells      += createElement('td',string[string_pos]);
+               char_code_cells += createElement('td',char_code);
                
-               results[i]=split(n,shares,treshold);
+               results[string_pos] = split(char_code,number_of_shares,treshold);
        }       
        
-       var code=e('tr',th)+e('tr',ns);
-       var pages=g('pages');
-       var explanation=v('explanation').replace('%n',v('treshold')).replace(/\n/g,'<br/>');
+       var code=createElement('tr',char_cells)+createElement('tr',char_code_cells);
+       var pages=getElement('pages');
+       var explanation=valueOfField('explanation').replace('%n',valueOfField('treshold')).replace(/\n/g,'<br/>');
        pages.innerHTML='';
-       for (j=0;j<shares;j++){
-               var line=e('th','share '+(j+1));
+       for (share_number=0;share_number<number_of_shares;share_number++){
+               var line=createElement('th','share '+(share_number+1));
                var page='';
-               for (i=0;i<s.length;i++){
-                       line+=e('td',results[i][j]);
-                       page+=e('span',results[i][j]);
+               for (string_pos=0;string_pos<string.length;string_pos++){
+                       line+=createElement('td',results[string_pos][share_number]);
+                       page+=createElement('span',results[string_pos][share_number]);
                }
-               code+=e('tr',line);             
-               pages.innerHTML+=e('div',  e('p',explanation)+e('p','Your share number: '+(j+1))+e('p',page)   );       
+               code+=createElement('tr',line);         
+               pages.innerHTML+=createElement('div',  createElement('p',explanation)+createElement('p','Your share number: '+(share_number+1))+createElement('p',page)   );    
        }
-       h('tab',code);
-       return n;
+       setHtmlOf('tab',code);
+       return char_code;
 }
 </script>
 </head>