Skip to content

Commit

Permalink
Remove appends.
Browse files Browse the repository at this point in the history
  • Loading branch information
q-uint committed Mar 3, 2022
1 parent 0ad29ff commit 7a8e713
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 8 deletions.
20 changes: 16 additions & 4 deletions src/SHA/SHA2.mo
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Array "mo:base/Array";
import Buffer "mo:base/Buffer";
import Binary "mo:encoding/Binary";
import Iter "mo:base/Iter";
import Nat32 "mo:base/Nat32";
Expand Down Expand Up @@ -52,7 +53,15 @@ module {
public func size() : Nat { hashSize; };

public func sum(bs : [Nat8]) : [Nat8] {
Array.append(bs, checkSum());
let cs = checkSum();
let size = bs.size();
Array.tabulate<Nat8>(
size + cs.size(),
func (x : Nat) {
if (x < size) return bs[x];
cs[x - size];
}
);
};

public func checkSum() : [Nat8] {
Expand All @@ -71,12 +80,15 @@ module {
));
};
write(Binary.BigEndian.fromNat64(n << 3));
var digest : [Nat8] = [];
let digest = Buffer.Buffer<Nat8>(32);
label l for (i in h.keys()) {
if (i == 7 and hashSize == 28) { break l; };
digest := Array.append(digest, Binary.BigEndian.fromNat32(h[i]));
let n = Binary.BigEndian.fromNat32(h[i]);
for (v in n.vals()) {
digest.add(v);
};
};
digest;
digest.toArray();
};

public func write(bs : [Nat8]) : () {
Expand Down
3 changes: 2 additions & 1 deletion src/Utilities.mo
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ module Utilities {
src : [T],
) : Nat {
let l = dst.size();
if (l < n) return 0;
for (i in src.keys()) {
if (l <= i) return l;
if (l <= n + i) return l;
dst[n + i] := src[i];
};
src.size();
Expand Down
2 changes: 0 additions & 2 deletions test/SHA256.mo
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import Blob "mo:base/Blob";
import Hex "mo:encoding/Hex";
import Text "mo:base/Text";

import Debug "mo:base/Debug";

import SHA256 "../src/SHA/SHA256";

let sum256 = SHA256.sum(Blob.toArray(Text.encodeUtf8("hello world\n")));
Expand Down
2 changes: 1 addition & 1 deletion vessel.dhall
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
dependencies = [ "base", "encoding" ],
compiler = Some "0.6.10"
compiler = Some "0.6.22"
}

0 comments on commit 7a8e713

Please sign in to comment.