Javascript-Code überarbeitet. Scheint jetzt zu funktionieren.
authorStephan Richter <github@keawe.de>
Fri, 13 Oct 2017 13:08:55 +0000 (15:08 +0200)
committerStephan Richter <github@keawe.de>
Fri, 13 Oct 2017 13:08:55 +0000 (15:08 +0200)
src/secret.html

index dd2a677a073890e11af1ce2eac7cb2c168ce933d..324cf459b737b0cd61234ca1dd70eb7165aade5f 100644 (file)
@@ -80,35 +80,27 @@ function split(number, number_of_shares, needed) {
     return shares;
 }
 
-function share(index,string){
-       this.index=index;
-       this.codes=string.split(' ');
-       this.codeCount=function(){return this.codes.length};
-       this.code=function(i){return this.codes[i];};
-       this.pick=function(i){return [this.index,this.codes[i]]};
-       this.string=function(){
-               return 'Share '+this.index+': '+this.codes.join(' / ');
-       };
-}
-
 function collect(){
        var divs=document.getElementsByClassName('share');
-       var shares=[],lastValue,div,inputs,num;
-       for (i=0; i<divs.length; i++){
-               div=divs[i];
-               inputs = div.getElementsByTagName('input');
-               num=0;                  
+       var shares=[],lastValue;
+       for (var div_index=0; div_index<divs.length; div_index++){
+               var div=divs[div_index];
+               var inputs = div.getElementsByTagName('input');
+               var share_number=0;                     
                for (var j=0; j<inputs.length;j++){
                        var input = inputs[j];
                        if (input.className == 'num') {
-                               num=input.value;
+                               share_number=input.value;
                        } else {
-                               lastValue=input.value;
-                               if (num>0 && lastValue!='') shares[i]=new share(num,lastValue);
+                               string=input.value;
+                               if (share_number>0 && string!='') {
+                                       var parts = string.split(' ');
+                                       shares[div_index] = { share_number, parts };
+                               }
                        }
                }
-       } 
-       if (lastValue!=''){
+       }       
+       if (string!=''){
                var clone=div.cloneNode(true);
                var inputs=clone.getElementsByTagName('input');
                for (var j=0; j<inputs.length; j++) inputs[j].value='';
@@ -132,36 +124,50 @@ function modInverse(k) {
 }
 
 function join(shares) {
-    var accum, count, formula, startposition, nextposition, value, numerator, denominator;
-    for(formula = accum = 0; formula < shares.length; formula++) {
-        for(count = 0, numerator = denominator = 1; count < shares.length; count++) {
-            if(formula == count) continue; // If not the same value
-            startposition = shares[formula][0];
-            nextposition = shares[count][0];
-            numerator = (numerator * -nextposition) % prime;
-            denominator = (denominator * (startposition - nextposition)) % prime;
+    var sum = 0;
+    
+    for(j = 0; j < shares.length; j++) {
+        var numerator = 1;
+        var denominator = 1;
+        for(m = 0; m < shares.length; m++) {
+            if(j == m) continue; // If not the same value
+            numerator = (numerator * shares[m].share_number) % prime;
+            denominator = (denominator * (shares[m].share_number - shares[j].share_number)) % prime;
         }
-        value = shares[formula][1];
-        accum = (prime + accum + (value * numerator * modInverse(denominator))) % prime;
+        sum = (prime + sum + (shares[j].share * numerator * modInverse(denominator))) % prime;
     }
-    return accum;
+    
+    return sum;
 }
 
 function decode(){
-       var shares=collect(); // fetch shares
+       var shares_list=collect(); // fetch shares
        var codeCount=null;
-       for (var i=0; i<shares.length; i++) codeCount=(codeCount==null)?shares[i].codeCount():Math.min(shares[i].codeCount(),codeCount);
+       for (var i=0; i<shares_list.length; i++) {
+               if (codeCount == null){         
+                       codeCount = shares_list[i].parts.length;
+               } else {
+                       codeCount=Math.min(shares_list[i].parts.length,codeCount);
+               }               
+       }
+       
        var ascii=document.getElementById('ascii');
        ascii.innerHTML='ascii: ';
        result.innerHTML='Passphrase: ';
-       for (codeIndex=0; codeIndex<codeCount; codeIndex++){
-               var shareSet=[];
-               for (var i=0; i<shares.length; i++){
-                       shareSet[i]=shares[i].pick(codeIndex);
+       
+       
+       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 };
                }
-               var r=join(shareSet);
-               ascii.innerHTML += r+' ';
-               result.innerHTML+= String.fromCharCode(r);
+               
+               var code = join(shares_for_code);
+               ascii.innerHTML += code+' ';
+               result.innerHTML+= String.fromCharCode(code);
+               
        }
 }