secret.html
authorStephan Richter <github@keawe.de>
Fri, 13 Oct 2017 13:32:43 +0000 (15:32 +0200)
committerStephan Richter <github@keawe.de>
Fri, 13 Oct 2017 13:32:43 +0000 (15:32 +0200)
src/secret.html

index 324cf459b737b0cd61234ca1dd70eb7165aade5f..39bb322d3ca86855d5aa7a6903c041356c9f0aa0 100644 (file)
@@ -62,19 +62,13 @@ function rand256(){
 
 /* Split number into the shares */
 function split(number, number_of_shares, needed) {
 
 /* Split number into the shares */
 function split(number, number_of_shares, needed) {
-    var coef = [number]
+    var coef = [number]; // first coefficient always equals the number itself.
     var shares = [];
     var shares = [];
-
-    for(var coef_index = 1; coef_index < needed; coef_index++){
-       coef[coef_index] = rand256();
-    }
     
     
+    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 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);
-       }
-       
+       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;
        shares[share_number-1] = sum % prime; 
     }
     return shares;
@@ -82,7 +76,10 @@ function split(number, number_of_shares, needed) {
 
 function collect(){
        var divs=document.getElementsByClassName('share');
 
 function collect(){
        var divs=document.getElementsByClassName('share');
-       var shares=[],lastValue;
+       var shares=[];
+       var string;
+       
+       // gather values from the input fields
        for (var div_index=0; div_index<divs.length; div_index++){
                var div=divs[div_index];
                var inputs = div.getElementsByTagName('input');
        for (var div_index=0; div_index<divs.length; div_index++){
                var div=divs[div_index];
                var inputs = div.getElementsByTagName('input');
@@ -100,6 +97,8 @@ function collect(){
                        }
                }
        }       
                        }
                }
        }       
+       
+       // the create new input fields on demand
        if (string!=''){
                var clone=div.cloneNode(true);
                var inputs=clone.getElementsByTagName('input');
        if (string!=''){
                var clone=div.cloneNode(true);
                var inputs=clone.getElementsByTagName('input');
@@ -140,37 +139,45 @@ function join(shares) {
     return sum;
 }
 
     return sum;
 }
 
-function decode(){
-       var shares_list=collect(); // fetch shares
-       var codeCount=null;
+/* get the number of codes from the list of shares */
+function numberOfCodesIn(shares_list){
+       var result=null;
        for (var i=0; i<shares_list.length; i++) {
        for (var i=0; i<shares_list.length; i++) {
-               if (codeCount == null){         
-                       codeCount = shares_list[i].parts.length;
+               if (result == null){            
+                       result = shares_list[i].parts.length;
                } else {
                } else {
-                       codeCount=Math.min(shares_list[i].parts.length,codeCount);
+                       result=Math.min(shares_list[i].parts.length,result);
                }               
        }
                }               
        }
+       return result;
+}
+
+/* fetches values from the input fields, decomposes each input string into several shares and reconstructs the original code/char for each position */
+function decode(){
+       var shares_list=collect(); // fetch shares
+       var ascii=getElement('ascii');
+       var result=getElement('result');        
        
        
-       var ascii=document.getElementById('ascii');
        ascii.innerHTML='ascii: ';
        result.innerHTML='Passphrase: ';
        
        ascii.innerHTML='ascii: ';
        result.innerHTML='Passphrase: ';
        
-       
+       var codeCount = numberOfCodesIn(shares_list);
        for (var codeIndex = 0; codeIndex<codeCount; codeIndex++){
                shares_for_code = [];
        for (var codeIndex = 0; codeIndex<codeCount; codeIndex++){
                shares_for_code = [];
-               for (var share_index=0; share_index<shares_list.length; share_index++){
-                       var share_number = shares_list[share_index].share_number;
-                       var share = shares_list[share_index].parts[codeIndex];
-                       shares_for_code[share_index] = { share_number, share };
-               }
+               
+               shares_list.forEach(function(share_set){
+                       var share_number = share_set.share_number;
+                       var share = share_set.parts[codeIndex];
+                       shares_for_code.push({ share_number, share });
+               });
                
                var code = join(shares_for_code);
                ascii.innerHTML += code+' ';
                result.innerHTML+= String.fromCharCode(code);
                
                var code = join(shares_for_code);
                ascii.innerHTML += code+' ';
                result.innerHTML+= String.fromCharCode(code);
-               
        }
 }
 
        }
 }
 
+/* decomposes the given secrets into single characters. Then, the ascii code of each caracter is treated as secre and split into keys */
 function secret2numbers(string){
        var string_pos,share_number,coef,char_code,x,sum;
        var number_of_shares = valueOfField('shares');
 function secret2numbers(string){
        var string_pos,share_number,coef,char_code,x,sum;
        var number_of_shares = valueOfField('shares');