[crypto] fix tests

parent 56b10f97
Pipeline #4578 failed with stage
in 0 seconds
//import crypto from 'crypto'; import { subtle } from '../../src/external-deps/crypto.js';
async function getSHA256Hash(input) { async function getSHA256Hash(input) {
const textAsBuffer = new TextEncoder().encode(input); const textAsBuffer = new TextEncoder().encode(input);
const hashBuffer = await globalThis.crypto.subtle.digest("SHA-256", textAsBuffer); const hashBuffer = await subtle.digest("SHA-256", textAsBuffer);
const hashArray = Array.from(new Uint8Array(hashBuffer)); const hashArray = Array.from(new Uint8Array(hashBuffer));
const hash = hashArray const hash = hashArray
.map((item) => item.toString(16).padStart(2, "0")) .map((item) => item.toString(16).padStart(2, "0"))
......
module Gargantext.Utils.Crypto where module Gargantext.Utils.Crypto where
import Control.Promise (Promise, toAffE) import Control.Promise (Promise, toAffE)
import Data.Array as Array
import Data.Set (Set) import Data.Set (Set)
import Data.Set as Set import Data.Set as Set
import Data.Array as Array import Data.String (joinWith)
import Effect (Effect) import Effect (Effect)
import Effect.Aff (Aff) import Effect.Aff (Aff)
...@@ -39,7 +40,7 @@ instance IsHashable (Array String) where ...@@ -39,7 +40,7 @@ instance IsHashable (Array String) where
hash = hash <<< concat hash = hash <<< concat
where where
concat :: Array Hash -> String concat :: Array Hash -> String
concat = Array.foldl (<>) "" concat = joinWith "" <<< Array.sort
instance IsHashable (Set String) where instance IsHashable (Set String) where
......
// https://siteultra.com/blog/using-subtle-in-a-javascript-module-from-both-the-browser-and-nodejs
let subtle = undefined;
if (typeof window !== 'undefined' && window.crypto && window.crypto.subtle) {
subtle = window.crypto.subtle;
} else {
// Import 'crypto' module in Node.js environment
subtle = require('crypto').subtle;
}
exports.subtle = subtle;
...@@ -124,7 +124,7 @@ spec = ...@@ -124,7 +124,7 @@ spec =
it "Hash List with backend works" do it "Hash List with backend works" do
let list = ["a","b"] let list = ["a","b"]
let hashed = "ab19ec537f09499b26f0f62eed7aefad46ab9f498e06a7328ce8e8ef90da6d86" let hashed = "fb8e20fc2e4c3f248c60c39bd652f3c1347298bb977b8b4d5903b85055620603"
-- ^ hash from backend with text above -- ^ hash from backend with text above
h <- Crypto.hash list h <- Crypto.hash list
h`shouldEqual` hashed h`shouldEqual` hashed
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment