1import * as Crypto from 'expo-crypto';
2import { CryptoDigestAlgorithm, CryptoEncoding } from 'expo-crypto';
3import React from 'react';
4import { ScrollView, StyleSheet } from 'react-native';
5
6import FunctionDemo, { FunctionDescription } from '../components/FunctionDemo';
7
8const GET_RANDOM_BYTES: FunctionDescription = {
9  name: 'getRandomBytes',
10  parameters: [
11    {
12      name: 'byteCount',
13      type: 'number',
14      values: [10, 128, 512, 1024],
15    },
16  ],
17  actions: Crypto.getRandomBytes,
18};
19
20const GET_RANDOM_BYTES_ASYNC: FunctionDescription = {
21  name: 'getRandomBytesAsync',
22  parameters: [
23    {
24      name: 'byteCount',
25      type: 'number',
26      values: [10, 128, 512, 1024],
27    },
28  ],
29  actions: Crypto.getRandomBytesAsync,
30};
31
32const DIGEST_STRING: FunctionDescription = {
33  name: 'digestString',
34  parameters: [
35    {
36      name: 'algorithm',
37      type: 'string',
38      values: [
39        CryptoDigestAlgorithm.MD2,
40        CryptoDigestAlgorithm.MD5,
41        CryptoDigestAlgorithm.SHA1,
42        CryptoDigestAlgorithm.SHA256,
43        CryptoDigestAlgorithm.SHA384,
44        CryptoDigestAlgorithm.SHA512,
45      ],
46    },
47    {
48      name: 'data',
49      type: 'string',
50      values: ["I'm a string", "I'm not a number"],
51    },
52    {
53      name: 'options',
54      type: 'object',
55      properties: [
56        {
57          name: 'encoding',
58          type: 'string',
59          values: [CryptoEncoding.BASE64, CryptoEncoding.HEX],
60        },
61      ],
62    },
63  ],
64  actions: Crypto.digestStringAsync,
65};
66
67const FUNCTIONS_DESCRIPTIONS = [GET_RANDOM_BYTES, GET_RANDOM_BYTES_ASYNC, DIGEST_STRING];
68
69function CryptoScreen() {
70  return (
71    <ScrollView contentContainerStyle={styles.container}>
72      {FUNCTIONS_DESCRIPTIONS.map((props, idx) => (
73        <FunctionDemo key={idx} namespace="Crypto" {...props} />
74      ))}
75    </ScrollView>
76  );
77}
78
79CryptoScreen.navigationOptions = {
80  title: 'Crypto',
81};
82
83const styles = StyleSheet.create({
84  container: {
85    padding: 10,
86    justifyContent: 'center',
87  },
88});
89
90export default CryptoScreen;
91