1import * as Crypto from 'expo-crypto'; 2import { CryptoDigestAlgorithm, CryptoEncoding } from 'expo-crypto'; 3import React from 'react'; 4import { ScrollView, StyleSheet, Text } 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 GET_RANDOM_VALUES: FunctionDescription = { 33 name: 'getRandomValues', 34 parameters: [ 35 { 36 name: 'array', 37 type: 'enum', 38 values: [ 39 { 40 name: 'new Uint16Array(10)', 41 value: new Uint16Array(10), 42 }, 43 { 44 name: 'new Int8Array(100)', 45 value: new Int8Array(100), 46 }, 47 { 48 name: 'new Uint8ClampedArray(1)', 49 value: new Uint8ClampedArray(1), 50 }, 51 ], 52 }, 53 ], 54 actions: Crypto.getRandomValues, 55}; 56 57const RANDOM_UUID: FunctionDescription = { 58 name: 'randomUUID', 59 actions: Crypto.randomUUID, 60}; 61 62const DIGEST_STRING: FunctionDescription = { 63 name: 'digestString', 64 parameters: [ 65 { 66 name: 'algorithm', 67 type: 'string', 68 values: [ 69 CryptoDigestAlgorithm.MD2, 70 CryptoDigestAlgorithm.MD5, 71 CryptoDigestAlgorithm.SHA1, 72 CryptoDigestAlgorithm.SHA256, 73 CryptoDigestAlgorithm.SHA384, 74 CryptoDigestAlgorithm.SHA512, 75 ], 76 }, 77 { 78 name: 'data', 79 type: 'string', 80 values: ["I'm a string", "I'm not a number"], 81 }, 82 { 83 name: 'options', 84 type: 'object', 85 properties: [ 86 { 87 name: 'encoding', 88 type: 'string', 89 values: [CryptoEncoding.BASE64, CryptoEncoding.HEX], 90 }, 91 ], 92 }, 93 ], 94 actions: Crypto.digestStringAsync, 95}; 96 97const DIGEST: FunctionDescription = { 98 name: 'digest', 99 parameters: [ 100 { 101 name: 'algorithm', 102 type: 'string', 103 values: [ 104 CryptoDigestAlgorithm.SHA1, 105 CryptoDigestAlgorithm.SHA256, 106 CryptoDigestAlgorithm.SHA384, 107 CryptoDigestAlgorithm.SHA512, 108 CryptoDigestAlgorithm.MD2, 109 CryptoDigestAlgorithm.MD5, 110 CryptoDigestAlgorithm.MD4, 111 ], 112 }, 113 { 114 name: 'data', 115 type: 'enum', 116 values: [ 117 { name: 'new Uint8Array(10).fill(1)', value: new Uint8Array(10).fill(1) }, 118 { name: 'new Int8Array(100).fill(2)', value: new Int8Array(100).fill(2) }, 119 ], 120 }, 121 ], 122 actions: Crypto.digest, 123 renderAdditionalResult: (result: ArrayBuffer) => { 124 return <Text>{new Uint8Array(result).map((byte, idx) => Number(byte)).join(', ')}</Text>; 125 }, 126}; 127 128const FUNCTIONS_DESCRIPTIONS = [ 129 GET_RANDOM_BYTES, 130 GET_RANDOM_BYTES_ASYNC, 131 DIGEST_STRING, 132 GET_RANDOM_VALUES, 133 RANDOM_UUID, 134 DIGEST, 135]; 136 137function CryptoScreen() { 138 return ( 139 <ScrollView contentContainerStyle={styles.container}> 140 {FUNCTIONS_DESCRIPTIONS.map((props, idx) => ( 141 <FunctionDemo key={idx} namespace="Crypto" {...props} /> 142 ))} 143 </ScrollView> 144 ); 145} 146 147CryptoScreen.navigationOptions = { 148 title: 'Crypto', 149}; 150 151const styles = StyleSheet.create({ 152 container: { 153 padding: 10, 154 justifyContent: 'center', 155 }, 156}); 157 158export default CryptoScreen; 159