xref: /expo/apps/test-suite/tests/Crypto.js (revision cb0e1ce3)
1import { Platform } from 'react-native';
2import * as Crypto from 'expo-crypto';
3
4const { CryptoEncoding, CryptoDigestAlgorithm } = Crypto;
5
6const testValue = 'Expo';
7
8const valueMapping = {
9  [CryptoEncoding.HEX]: {
10    [CryptoDigestAlgorithm.SHA1]: `c275355dc46ac171633935033d113e3872d595e5`,
11    [CryptoDigestAlgorithm.SHA256]: `f5e5cae536b49d394e1e72d4368d64b00a23298ec5ae11f3a9102a540e2532dc`,
12    [CryptoDigestAlgorithm.SHA384]: `aa356c88afdbd8a7a5a9c3133541af0035e4b04e82438a223aa1939240ccb6b3ca28294b5ac0f42703b15183f4c016fc`,
13    [CryptoDigestAlgorithm.SHA512]: `f1924f3e61aac4e4caa7fd566591b8abb541b0e642cd7bf0c71573267cfacf1b6dafe905bd6e42633bfba67c59774e070095e19a7c2078ac18ccd23245d76f1c`,
14    [CryptoDigestAlgorithm.MD2]: `fb85323c3b15b016e0351006e2f47bf7`,
15    [CryptoDigestAlgorithm.MD4]: `2d36099794ec182cbb36d02e1188fc1e`,
16    [CryptoDigestAlgorithm.MD5]: `c29f23f279126757ba18ec74d0d27cfa`,
17  },
18  [CryptoEncoding.BASE64]: {
19    [CryptoDigestAlgorithm.SHA1]: `wnU1XcRqwXFjOTUDPRE+OHLVleU=`,
20    [CryptoDigestAlgorithm.SHA256]: `9eXK5Ta0nTlOHnLUNo1ksAojKY7FrhHzqRAqVA4lMtw=`,
21    [CryptoDigestAlgorithm.SHA384]: `qjVsiK/b2KelqcMTNUGvADXksE6CQ4oiOqGTkkDMtrPKKClLWsD0JwOxUYP0wBb8`,
22    [CryptoDigestAlgorithm.SHA512]: `8ZJPPmGqxOTKp/1WZZG4q7VBsOZCzXvwxxVzJnz6zxttr+kFvW5CYzv7pnxZd04HAJXhmnwgeKwYzNIyRddvHA==`,
23    [CryptoDigestAlgorithm.MD2]: `+4UyPDsVsBbgNRAG4vR79w==`,
24    [CryptoDigestAlgorithm.MD4]: `LTYJl5TsGCy7NtAuEYj8Hg==`,
25    [CryptoDigestAlgorithm.MD5]: `wp8j8nkSZ1e6GOx00NJ8+g==`,
26  },
27};
28
29export const name = 'Crypto';
30
31const UNSUPPORTED = Platform.select({
32  web: ['MD2', 'MD4', 'MD5'],
33  default: [],
34});
35function supportedAlgorithm(algorithm) {
36  return !UNSUPPORTED.includes(algorithm);
37}
38
39export async function test({ describe, it, expect }) {
40  describe('Crypto', () => {
41    describe('digestStringAsync()', async () => {
42      it(`Invalid CryptoEncoding throws an error`, async () => {
43        let error = null;
44        try {
45          await Crypto.digestStringAsync(CryptoDigestAlgorithm.SHA1, testValue, {
46            encoding: 'INVALID',
47          });
48        } catch (e) {
49          error = e;
50        }
51        expect(error).not.toBeNull();
52      });
53
54      for (const encodingEntry of Object.entries(CryptoEncoding)) {
55        const [encodingKey, encoding] = encodingEntry;
56        describe(`Encoded with CryptoEncoding.${encodingKey}`, () => {
57          for (const entry of Object.entries(CryptoDigestAlgorithm)) {
58            const [key, algorithm] = entry;
59            it(`CryptoDigestAlgorithm.${key}`, async () => {
60              const targetValue = valueMapping[encoding][algorithm];
61              if (supportedAlgorithm(algorithm)) {
62                const value = await Crypto.digestStringAsync(algorithm, testValue, { encoding });
63                expect(value).toBe(targetValue);
64              } else {
65                let error = null;
66                try {
67                  await Crypto.digestStringAsync(algorithm, testValue, { encoding });
68                } catch (e) {
69                  error = e;
70                }
71                expect(error).not.toBeNull();
72              }
73            });
74          }
75        });
76      }
77    });
78  });
79}
80