From a3fbcea8436ee3fa057995df38820374dea22a25 Mon Sep 17 00:00:00 2001 From: Emi Simpson Date: Mon, 24 Oct 2022 15:10:30 -0400 Subject: [PATCH] Made a whole bunch of changes Really not in the mood for git hygine right now --- packages.dhall | 21 ++++----------------- spago.dhall | 4 +++- src/Main.purs | 25 +++++++++++++++++++------ 3 files changed, 26 insertions(+), 24 deletions(-) diff --git a/packages.dhall b/packages.dhall index 2dd31d6..fd97511 100644 --- a/packages.dhall +++ b/packages.dhall @@ -100,29 +100,16 @@ in upstream -} https://github.com/purescript/package-sets/releases/download/psc-0.15.4-20220901/packages.dhall sha256:f1531b29c21ac437ffe5666c1b6cc76f0a9c29d3c9d107ff047aa2567744994f -with base64 = +with base64-codec = { dependencies = [ "arraybuffer-types" - , "arrays" - , "assert" - , "console" - , "effect" - , "either" - , "encoding" - , "enums" - , "exceptions" - , "functions" - , "partial" + , "newtype" , "prelude" - , "quickcheck" - , "strings" - , "stringutils" - , "unicode" ] , repo = - "https://github.com/menelaos/purescript-b64" + "https://github.com/gardspirito/purescript-base64-codec.git" , version = - "v0.0.8" -- branch, tag, or commit hash + "master" -- branch, tag, or commit hash } with subtlecrypto = { dependencies = diff --git a/spago.dhall b/spago.dhall index 11f23eb..9e4d1ba 100644 --- a/spago.dhall +++ b/spago.dhall @@ -14,11 +14,13 @@ to generate this file without the comments in this block. , dependencies = [ "aff" , "arraybuffer-types" - , "b64" + , "arraybuffer-builder" + , "base64-codec" , "console" , "effect" , "either" , "fetch" + , "foldable-traversable" , "maybe" , "prelude" , "strings" diff --git a/src/Main.purs b/src/Main.purs index 9a75586..678b082 100644 --- a/src/Main.purs +++ b/src/Main.purs @@ -7,25 +7,33 @@ import Effect.Aff (Aff, launchAff) import Effect.Class (liftEffect) import Effect.Console (log) import Fetch (fetch) -import Data.Binary.Base64 (decode) -import Data.Either (Either, hush, note) +import Data.Base64 (decodeBase64, fromString) +import Data.Either (Either, note) import Data.Maybe (Maybe(..)) -import Data.ArrayBuffer.Types (ArrayBuffer, Uint8Array) +import Data.ArrayBuffer.Builder (execPut, putInt32be) +import Data.ArrayBuffer.Types (ArrayBuffer) +import Data.Foldable (traverse_) import Data.String (drop, indexOf', indexOf, splitAt) import Data.String.Pattern (Pattern(..)) -import Crypto.Subtle.Constants.AES (aesGCM) +import Crypto.Subtle.Constants.AES (aesGCM, t128) +import Crypto.Subtle.Encrypt (aesGCM, decrypt) as Alg import Crypto.Subtle.Key.Import (aes) import Crypto.Subtle.Key.Import (importKey) as SC import Crypto.Subtle.Key.Types (CryptoKey, decrypt, raw) +nonceE :: Effect ArrayBuffer +nonceE = execPut $ traverse_ putInt32be [0xd0, 0xc3, 0x75, 0x56, 0x58, 0xc1, 0x7e, 0x5f, 0xd6, 0xcc, 0xb6, 0x76] + data AviaryError = MalformedKey | MalformedUrl + | DecryptFailed instance showAviaryError :: Show AviaryError where show MalformedKey = "Malformed key!!" show MalformedUrl = "Malformed url!!" + show DecryptFailed = "Either the data provided by the server was bad, or valid but incorrect key. Expiration likely." type UrlInfo = { server :: String @@ -41,13 +49,18 @@ parseUrl url = note MalformedUrl do let { after: key, before: fileId } = splitAt poundIndex after Just { server, fileId, key: drop 1 key } -decodeKey :: String -> Either AviaryError Uint8Array -decodeKey key = note MalformedKey $ hush $ decode key +decodeKey :: String -> Either AviaryError ArrayBuffer +decodeKey key = note MalformedKey $ decodeBase64 <$> fromString key importKey :: ArrayBuffer -> Aff CryptoKey importKey key = SC.importKey raw key (aes aesGCM) false [decrypt] +decryptBlob :: CryptoKey -> ArrayBuffer -> ArrayBuffer -> Aff ArrayBuffer +decryptBlob key cyphertext nonce = + -- nonce <- makeAff (\_ -> nonceE) + Alg.decrypt (Alg.aesGCM nonce Nothing (Just t128)) key cyphertext + main_aff :: Aff Unit main_aff = do { status, text } <- fetch "https://envs.sh/Q_V.txt" {}