Commits

Anonymous committed 4b44e4b Draft

fixed toyDigest keeping state between calls

Comments (0)

Files changed (2)

crypto/test/digest.ceylon

 import crypto.toy { toyDigest }
 
 void digest() {
-    assert(nonempty bytes = codepointsOf("Hello world!!").sequence);
+    // off word bounds
+    testDigest("Hello world!!");
+    // on word bounds
+    testDigest("Hello world!");
+}
+
+void testDigest(String content) {
+    assert(nonempty bytes = codepointsOf(content).sequence);
     //print(bytes);
     value bouncyResult = bouncyDigest.digest(sha1, bytes);
     //print(bouncyResult);

crypto/toy/digest.ceylon

     n.leftLogicalShift(by).and(#ffffffff)
         .or(n.and(#ffffffff).rightLogicalShift(32 - by));
 
-class Buffer<Element>(size, Element default)
-        satisfies Iterable<Element, Nothing> {
+class Buffer<Element>(size, Element default) {
     assert(size > 0);
     shared Array<Element> array = arrayOfSize(size, default);
     variable Integer _position = 0;
     shared Integer position => _position;
-    shared actual Integer size;
+    shared Integer size;
     
     shared Integer available => size - position;
     
     shared void reset() {
         _position = 0;
     }
-
-    shared actual Iterator<Element> iterator() =>
-        array.skipping(position).iterator();
 }
 
 //void dp(Integer n) {
 //    print(formatInteger(n.and(#ffffffff), 2));
 //}
 
-shared object toyDigest
-        satisfies Digest<Sha1> {
+[Integer+] sha1Digest([Integer+] bytes) {
     value wordSize = 4;
     // additive constants
     value y1 = #5a827999;
             }
         }
     }
-    
-    void processWord({Integer+} word) {
-        block.put(bigEndianToInt32(word));
-        if (block.available == 0) {
-            processBlock();
-        }
-    }
-
-    void processSize(Integer bytesSize) {
-        if (block.position > 14) {
-            fillBlock(block.size);
-            processBlock();
-        }
-        
-        value bitLength = bytesSize.leftLogicalShift(3);
-        //print(bitLength);
-        
-        fillBlock(14);
-        block.put(bitLength.rightLogicalShift(32));
-        block.put(bitLength.and(#ffffffff));
-    }
 
     void processBlock() {
         assert(block.available == 0); // must be full
         //print(block.array);
         
         // expand 16 word block into 80 word block
-        block.reset();
-        for (byte in block) {
+        for (byte in block.array) {
             x.put(byte);
         }
         
         block.reset();
     }
     
-    shared actual [Integer+] digest(Sha1 algorithm, [Integer+] bytes) {
-        // update
-        value remainingBytes = bytes.size % wordSize;
-        for (wordI in (0:bytes.size - remainingBytes).by(wordSize)) {
-            assert(nonempty word = bytes.segment(wordI, wordSize));
-            processWord(word);
+    void processWord({Integer+} word) {
+        block.put(bigEndianToInt32(word));
+        if (block.available == 0) {
+            processBlock();
+        }
+    }
+    
+    void processSize(Integer bytesSize) {
+        if (block.position > 14) {
+            fillBlock(block.size);
+            processBlock();
         }
         
-        // update/finish
-        value lastWordInit = bytes.terminal(remainingBytes).withTrailing(128);
-        value lastWord = lastWordInit.chain([0].cycle(wordSize - lastWordInit.size));
-        processWord(lastWord);
+        value bitLength = bytesSize.leftLogicalShift(3);
+        //print(bitLength);
         
-        processSize(bytes.size); // don't count padding
-        processBlock();
-        
-        // doFinal
-        assert(nonempty result = concatenate(
-            int32ToBigEndian(h1),
-            int32ToBigEndian(h2),
-            int32ToBigEndian(h3),
-            int32ToBigEndian(h4),
-            int32ToBigEndian(h5)));
-        return result;
+        fillBlock(14);
+        block.put(bitLength.rightLogicalShift(32));
+        block.put(bitLength.and(#ffffffff));
     }
+    
+    // update
+    value remainingBytes = bytes.size % wordSize;
+    for (wordI in (0:bytes.size - remainingBytes).by(wordSize)) {
+        assert(nonempty word = bytes.segment(wordI, wordSize));
+        processWord(word);
+    }
+    
+    // update/finish
+    value lastWordInit = bytes.terminal(remainingBytes).withTrailing(128);
+    value lastWord = lastWordInit.chain([0].cycle(wordSize - lastWordInit.size));
+    processWord(lastWord);
+    
+    processSize(bytes.size); // don't count padding
+    processBlock();
+    
+    // doFinal
+    assert(nonempty result = concatenate(
+        int32ToBigEndian(h1),
+        int32ToBigEndian(h2),
+        int32ToBigEndian(h3),
+        int32ToBigEndian(h4),
+        int32ToBigEndian(h5)));
+    return result;
+}
+
+shared object toyDigest
+        satisfies Digest<Sha1> {
+    shared actual [Integer+] digest(Sha1 algorithm, [Integer+] bytes) =>
+        sha1Digest(bytes);
 }