Skip to content

Commit

Permalink
Adds functions to access digest
Browse files Browse the repository at this point in the history
  • Loading branch information
MatthewJohnHeath committed Sep 4, 2024
1 parent c3712d7 commit c9ab4e5
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions crates/compiler/builtins/roc/Crypt.roc
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ module [
addBytes,
digest,
hashSha256,
digest256ToBytes,
Sha256,
Digest256,
]

import List
import Num exposing [U8, U64, U128]
import Num exposing [U8, U64, U128, Int]
import Num

Sha256 := {
location : U64,
Expand All @@ -17,14 +19,27 @@ Sha256 := {
Digest256 := {
firstHalf : U128,
secondHalf : U128,
}
} implements[Eq, Hash]

emptySha256 : {} -> Sha256

addBytes : Sha256, List.List U8 -> Sha256
addBytes : Sha256, List U8 -> Sha256

digest : Sha256 -> Digest256

hashSha256 : List U8 -> Digest256
hashSha256 = \bytes ->
digest (addBytes (emptySha256 {}) bytes)

u128Bytes : U128 -> List U8
u128Bytes = \ number ->
loop = \ n, bytes, place ->
if place == 16 then bytes
else
newByte = n |> Num.bitwiseAnd 255 |> Num.toU8
loop (Num.shiftRightBy n 8) (List.append bytes newByte) (place + 1)
loop number [] 0

digest256ToBytes : Digest256 -> List U8
digest256ToBytes \ digest256 ->
List.append (u128Bytes digest256.firstHalf) (u128Bytes digest256.secondHalf)

0 comments on commit c9ab4e5

Please sign in to comment.