Made a whole bunch of changes

Really not in the mood for git hygine right now
This commit is contained in:
Emi Simpson 2022-10-24 15:10:30 -04:00
parent 6885935089
commit a3fbcea843
Signed by: Emi
GPG Key ID: A12F2C2FFDC3D847
3 changed files with 26 additions and 24 deletions

View File

@ -100,29 +100,16 @@ in upstream
-} -}
https://github.com/purescript/package-sets/releases/download/psc-0.15.4-20220901/packages.dhall sha256:f1531b29c21ac437ffe5666c1b6cc76f0a9c29d3c9d107ff047aa2567744994f https://github.com/purescript/package-sets/releases/download/psc-0.15.4-20220901/packages.dhall sha256:f1531b29c21ac437ffe5666c1b6cc76f0a9c29d3c9d107ff047aa2567744994f
with base64 = with base64-codec =
{ dependencies = { dependencies =
[ "arraybuffer-types" [ "arraybuffer-types"
, "arrays" , "newtype"
, "assert"
, "console"
, "effect"
, "either"
, "encoding"
, "enums"
, "exceptions"
, "functions"
, "partial"
, "prelude" , "prelude"
, "quickcheck"
, "strings"
, "stringutils"
, "unicode"
] ]
, repo = , repo =
"https://github.com/menelaos/purescript-b64" "https://github.com/gardspirito/purescript-base64-codec.git"
, version = , version =
"v0.0.8" -- branch, tag, or commit hash "master" -- branch, tag, or commit hash
} }
with subtlecrypto = with subtlecrypto =
{ dependencies = { dependencies =

View File

@ -14,11 +14,13 @@ to generate this file without the comments in this block.
, dependencies = , dependencies =
[ "aff" [ "aff"
, "arraybuffer-types" , "arraybuffer-types"
, "b64" , "arraybuffer-builder"
, "base64-codec"
, "console" , "console"
, "effect" , "effect"
, "either" , "either"
, "fetch" , "fetch"
, "foldable-traversable"
, "maybe" , "maybe"
, "prelude" , "prelude"
, "strings" , "strings"

View File

@ -7,25 +7,33 @@ import Effect.Aff (Aff, launchAff)
import Effect.Class (liftEffect) import Effect.Class (liftEffect)
import Effect.Console (log) import Effect.Console (log)
import Fetch (fetch) import Fetch (fetch)
import Data.Binary.Base64 (decode) import Data.Base64 (decodeBase64, fromString)
import Data.Either (Either, hush, note) import Data.Either (Either, note)
import Data.Maybe (Maybe(..)) 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 (drop, indexOf', indexOf, splitAt)
import Data.String.Pattern (Pattern(..)) 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 (aes)
import Crypto.Subtle.Key.Import (importKey) as SC import Crypto.Subtle.Key.Import (importKey) as SC
import Crypto.Subtle.Key.Types (CryptoKey, decrypt, raw) 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 data AviaryError
= MalformedKey = MalformedKey
| MalformedUrl | MalformedUrl
| DecryptFailed
instance showAviaryError :: Show AviaryError where instance showAviaryError :: Show AviaryError where
show MalformedKey = "Malformed key!!" show MalformedKey = "Malformed key!!"
show MalformedUrl = "Malformed url!!" show MalformedUrl = "Malformed url!!"
show DecryptFailed = "Either the data provided by the server was bad, or valid but incorrect key. Expiration likely."
type UrlInfo = type UrlInfo =
{ server :: String { server :: String
@ -41,13 +49,18 @@ parseUrl url = note MalformedUrl do
let { after: key, before: fileId } = splitAt poundIndex after let { after: key, before: fileId } = splitAt poundIndex after
Just { server, fileId, key: drop 1 key } Just { server, fileId, key: drop 1 key }
decodeKey :: String -> Either AviaryError Uint8Array decodeKey :: String -> Either AviaryError ArrayBuffer
decodeKey key = note MalformedKey $ hush $ decode key decodeKey key = note MalformedKey $ decodeBase64 <$> fromString key
importKey :: ArrayBuffer -> Aff CryptoKey importKey :: ArrayBuffer -> Aff CryptoKey
importKey key = importKey key =
SC.importKey raw key (aes aesGCM) false [decrypt] 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 :: Aff Unit
main_aff = do main_aff = do
{ status, text } <- fetch "https://envs.sh/Q_V.txt" {} { status, text } <- fetch "https://envs.sh/Q_V.txt" {}