From 6885935089bfda3aa8683646c55649f7098f2fea Mon Sep 17 00:00:00 2001 From: Emi Simpson Date: Wed, 19 Oct 2022 17:43:54 -0400 Subject: [PATCH] Attempt to implement subtlecrypto key importing (untested) --- packages.dhall | 21 +++++++++++++++++++++ spago.dhall | 1 + src/Main.purs | 10 +++++++++- 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/packages.dhall b/packages.dhall index 4d73ae4..2dd31d6 100644 --- a/packages.dhall +++ b/packages.dhall @@ -124,3 +124,24 @@ with base64 = , version = "v0.0.8" -- branch, tag, or commit hash } +with subtlecrypto = + { dependencies = + [ "aff" + , "aff-promise" + , "arraybuffer-types" + , "console" + , "effect" + , "either" + , "exceptions" + , "foreign" + , "functions" + , "maybe" + , "prelude" + , "transformers" + , "tuples" + , "unsafe-coerce" + ] + , repo = + "https://github.com/xc-jp/purescript-subtlecrypto.git" + , version = "master" + } diff --git a/spago.dhall b/spago.dhall index 58e43eb..11f23eb 100644 --- a/spago.dhall +++ b/spago.dhall @@ -22,6 +22,7 @@ to generate this file without the comments in this block. , "maybe" , "prelude" , "strings" + , "subtlecrypto" ] , packages = ./packages.dhall , sources = [ "src/**/*.purs", "test/**/*.purs" ] diff --git a/src/Main.purs b/src/Main.purs index 69cfdd1..9a75586 100644 --- a/src/Main.purs +++ b/src/Main.purs @@ -9,12 +9,16 @@ import Effect.Console (log) import Fetch (fetch) import Data.Binary.Base64 (decode) import Data.Either (Either, hush, note) -import Data.ArrayBuffer.Types (Uint8Array) import Data.Maybe (Maybe(..)) import Data.ArrayBuffer.Types (ArrayBuffer, Uint8Array) import Data.String (drop, indexOf', indexOf, splitAt) import Data.String.Pattern (Pattern(..)) +import Crypto.Subtle.Constants.AES (aesGCM) +import Crypto.Subtle.Key.Import (aes) +import Crypto.Subtle.Key.Import (importKey) as SC +import Crypto.Subtle.Key.Types (CryptoKey, decrypt, raw) + data AviaryError = MalformedKey | MalformedUrl @@ -40,6 +44,10 @@ parseUrl url = note MalformedUrl do decodeKey :: String -> Either AviaryError Uint8Array decodeKey key = note MalformedKey $ hush $ decode key +importKey :: ArrayBuffer -> Aff CryptoKey +importKey key = + SC.importKey raw key (aes aesGCM) false [decrypt] + main_aff :: Aff Unit main_aff = do { status, text } <- fetch "https://envs.sh/Q_V.txt" {}