expo-crypto 12.0.0 → 12.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +18 -0
- package/README.md +1 -1
- package/android/build.gradle +4 -4
- package/android/src/main/java/expo/modules/crypto/CryptoModule.kt +31 -0
- package/build/Crypto.d.ts +60 -1
- package/build/Crypto.d.ts.map +1 -1
- package/build/Crypto.js +136 -1
- package/build/Crypto.js.map +1 -1
- package/build/Crypto.types.d.ts +2 -2
- package/build/Crypto.types.d.ts.map +1 -1
- package/build/ExpoCrypto.web.d.ts +6 -0
- package/build/ExpoCrypto.web.d.ts.map +1 -1
- package/build/ExpoCrypto.web.js +18 -0
- package/build/ExpoCrypto.web.js.map +1 -1
- package/ios/CryptoModule.swift +47 -0
- package/package.json +5 -3
- package/src/Crypto.ts +146 -2
- package/src/ExpoCrypto.web.ts +20 -1
package/CHANGELOG.md
CHANGED
|
@@ -10,6 +10,24 @@
|
|
|
10
10
|
|
|
11
11
|
### 💡 Others
|
|
12
12
|
|
|
13
|
+
## 12.2.0 — 2023-02-03
|
|
14
|
+
|
|
15
|
+
### 🎉 New features
|
|
16
|
+
|
|
17
|
+
- Added a `digest` method to get a cryptographic digest of a typed array. ([#20886](https://github.com/expo/expo/pull/20886) by [@aleqsio](https://github.com/aleqsio))
|
|
18
|
+
|
|
19
|
+
### 💡 Others
|
|
20
|
+
|
|
21
|
+
- On Android bump `compileSdkVersion` and `targetSdkVersion` to `33`. ([#20721](https://github.com/expo/expo/pull/20721) by [@lukmccall](https://github.com/lukmccall))
|
|
22
|
+
|
|
23
|
+
## 12.1.0 — 2022-12-30
|
|
24
|
+
|
|
25
|
+
### 🎉 New features
|
|
26
|
+
|
|
27
|
+
- Added a `randomUUID` method to get a random UUIDv4 string. ([#20274](https://github.com/expo/expo/pull/20274) by [@aleqsio](https://github.com/aleqsio))
|
|
28
|
+
- Added a `getRandomValues` method to fill typed arrays. ([#20257](https://github.com/expo/expo/pull/20257) by [@aleqsio](https://github.com/aleqsio))
|
|
29
|
+
- Ported over `getRandomBytes`, `getRandomBytesAsync` methods from `expo-random`. ([#20217](https://github.com/expo/expo/pull/20217) by [@aleqsio](https://github.com/aleqsio))
|
|
30
|
+
|
|
13
31
|
## 12.0.0 — 2022-10-25
|
|
14
32
|
|
|
15
33
|
### 🛠 Breaking changes
|
package/README.md
CHANGED
|
@@ -4,7 +4,7 @@ Provides cryptography primitives.
|
|
|
4
4
|
|
|
5
5
|
# API documentation
|
|
6
6
|
|
|
7
|
-
- [Documentation for the main branch](https://github.com/expo/expo/blob/main/docs/pages/versions/unversioned/sdk/crypto.
|
|
7
|
+
- [Documentation for the main branch](https://github.com/expo/expo/blob/main/docs/pages/versions/unversioned/sdk/crypto.mdx)
|
|
8
8
|
- [Documentation for the latest stable release](https://docs.expo.dev/versions/latest/sdk/crypto/)
|
|
9
9
|
|
|
10
10
|
# Installation in managed Expo projects
|
package/android/build.gradle
CHANGED
|
@@ -3,7 +3,7 @@ apply plugin: 'kotlin-android'
|
|
|
3
3
|
apply plugin: 'maven-publish'
|
|
4
4
|
|
|
5
5
|
group = 'host.exp.exponent'
|
|
6
|
-
version = '12.
|
|
6
|
+
version = '12.2.0'
|
|
7
7
|
|
|
8
8
|
buildscript {
|
|
9
9
|
def expoModulesCorePlugin = new File(project(":expo-modules-core").projectDir.absolutePath, "ExpoModulesCorePlugin.gradle")
|
|
@@ -59,7 +59,7 @@ afterEvaluate {
|
|
|
59
59
|
}
|
|
60
60
|
|
|
61
61
|
android {
|
|
62
|
-
compileSdkVersion safeExtGet("compileSdkVersion",
|
|
62
|
+
compileSdkVersion safeExtGet("compileSdkVersion", 33)
|
|
63
63
|
|
|
64
64
|
compileOptions {
|
|
65
65
|
sourceCompatibility JavaVersion.VERSION_11
|
|
@@ -72,9 +72,9 @@ android {
|
|
|
72
72
|
|
|
73
73
|
defaultConfig {
|
|
74
74
|
minSdkVersion safeExtGet("minSdkVersion", 21)
|
|
75
|
-
targetSdkVersion safeExtGet("targetSdkVersion",
|
|
75
|
+
targetSdkVersion safeExtGet("targetSdkVersion", 33)
|
|
76
76
|
versionCode 25
|
|
77
|
-
versionName "12.
|
|
77
|
+
versionName "12.2.0"
|
|
78
78
|
}
|
|
79
79
|
lintOptions {
|
|
80
80
|
abortOnError false
|
|
@@ -3,14 +3,32 @@ package expo.modules.crypto
|
|
|
3
3
|
import android.util.Base64
|
|
4
4
|
import expo.modules.kotlin.modules.Module
|
|
5
5
|
import expo.modules.kotlin.modules.ModuleDefinition
|
|
6
|
+
import expo.modules.kotlin.typedarray.TypedArray
|
|
6
7
|
import java.security.MessageDigest
|
|
8
|
+
import java.security.SecureRandom
|
|
9
|
+
import java.util.UUID
|
|
7
10
|
|
|
8
11
|
class CryptoModule : Module() {
|
|
12
|
+
private val secureRandom by lazy { SecureRandom() }
|
|
13
|
+
|
|
9
14
|
override fun definition() = ModuleDefinition {
|
|
10
15
|
Name("ExpoCrypto")
|
|
11
16
|
|
|
12
17
|
Function("digestString", this@CryptoModule::digestString)
|
|
13
18
|
AsyncFunction("digestStringAsync", this@CryptoModule::digestString)
|
|
19
|
+
Function("getRandomBase64String", this@CryptoModule::getRandomBase64String)
|
|
20
|
+
AsyncFunction("getRandomBase64StringAsync", this@CryptoModule::getRandomBase64String)
|
|
21
|
+
Function("getRandomValues", this@CryptoModule::getRandomValues)
|
|
22
|
+
Function("digest", this@CryptoModule::digest)
|
|
23
|
+
Function("randomUUID") {
|
|
24
|
+
UUID.randomUUID().toString()
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
private fun getRandomBase64String(randomByteCount: Int): String {
|
|
29
|
+
val output = ByteArray(randomByteCount)
|
|
30
|
+
secureRandom.nextBytes(output)
|
|
31
|
+
return Base64.encodeToString(output, Base64.NO_WRAP)
|
|
14
32
|
}
|
|
15
33
|
|
|
16
34
|
private fun digestString(algorithm: DigestAlgorithm, data: String, options: DigestOptions): String {
|
|
@@ -30,4 +48,17 @@ class CryptoModule : Module() {
|
|
|
30
48
|
}
|
|
31
49
|
}
|
|
32
50
|
}
|
|
51
|
+
|
|
52
|
+
private fun digest(algorithm: DigestAlgorithm, output: TypedArray, data: TypedArray) {
|
|
53
|
+
val messageDigest = MessageDigest.getInstance(algorithm.value).apply { update(data.toDirectBuffer()) }
|
|
54
|
+
|
|
55
|
+
val digest: ByteArray = messageDigest.digest()
|
|
56
|
+
output.write(digest, output.byteOffset, output.byteLength)
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
private fun getRandomValues(typedArray: TypedArray) {
|
|
60
|
+
val array = ByteArray(typedArray.byteLength)
|
|
61
|
+
secureRandom.nextBytes(array)
|
|
62
|
+
typedArray.write(array, typedArray.byteOffset, typedArray.byteLength)
|
|
63
|
+
}
|
|
33
64
|
}
|
package/build/Crypto.d.ts
CHANGED
|
@@ -1,11 +1,27 @@
|
|
|
1
|
+
import { UintBasedTypedArray, IntBasedTypedArray } from 'expo-modules-core';
|
|
1
2
|
import { CryptoDigestAlgorithm, CryptoDigestOptions, Digest } from './Crypto.types';
|
|
2
3
|
export * from './Crypto.types';
|
|
4
|
+
/**
|
|
5
|
+
* Generates completely random bytes using native implementations. The `byteCount` property
|
|
6
|
+
* is a `number` indicating the number of bytes to generate in the form of a `Uint8Array`.
|
|
7
|
+
* Falls back to `Math.random` during development to prevent issues with React Native Debugger.
|
|
8
|
+
* @param byteCount - A number within the range from `0` to `1024`. Anything else will throw a `TypeError`.
|
|
9
|
+
* @return An array of random bytes with the same length as the `byteCount`.
|
|
10
|
+
*/
|
|
11
|
+
export declare function getRandomBytes(byteCount: number): Uint8Array;
|
|
12
|
+
/**
|
|
13
|
+
* Generates completely random bytes using native implementations. The `byteCount` property
|
|
14
|
+
* is a `number` indicating the number of bytes to generate in the form of a `Uint8Array`.
|
|
15
|
+
* @param byteCount - A number within the range from `0` to `1024`. Anything else will throw a `TypeError`.
|
|
16
|
+
* @return A promise that fulfills with an array of random bytes with the same length as the `byteCount`.
|
|
17
|
+
*/
|
|
18
|
+
export declare function getRandomBytesAsync(byteCount: number): Promise<Uint8Array>;
|
|
3
19
|
/**
|
|
4
20
|
* The `digestStringAsync()` method of `Crypto` generates a digest of the supplied `data` string with the provided digest `algorithm`.
|
|
5
21
|
* A digest is a short fixed-length value derived from some variable-length input. **Cryptographic digests** should exhibit _collision-resistance_,
|
|
6
22
|
* meaning that it's very difficult to generate multiple inputs that have equal digest values.
|
|
7
23
|
* You can specify the returned string format as one of `CryptoEncoding`. By default, the resolved value will be formatted as a `HEX` string.
|
|
8
|
-
* On web, this method can only be called from a secure origin (
|
|
24
|
+
* On web, this method can only be called from a secure origin (HTTPS) otherwise, an error will be thrown.
|
|
9
25
|
*
|
|
10
26
|
* @param algorithm The cryptographic hash function to use to transform a block of data into a fixed-size output.
|
|
11
27
|
* @param data The value that will be used to generate a digest.
|
|
@@ -21,4 +37,47 @@ export * from './Crypto.types';
|
|
|
21
37
|
* ```
|
|
22
38
|
*/
|
|
23
39
|
export declare function digestStringAsync(algorithm: CryptoDigestAlgorithm, data: string, options?: CryptoDigestOptions): Promise<Digest>;
|
|
40
|
+
/**
|
|
41
|
+
* The `getRandomValues()` method of `Crypto` fills a provided `TypedArray` with cryptographically secure random values.
|
|
42
|
+
*
|
|
43
|
+
* @param typedArray An integer based [`TypedArray`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray) to fill with cryptographically secure random values. It modifies the input array in place.
|
|
44
|
+
* @return The input array filled with cryptographically secure random values.
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* ```ts
|
|
48
|
+
* const byteArray = new Uint8Array(16);
|
|
49
|
+
* Crypto.getRandomValues(byteArray);
|
|
50
|
+
* console.log('Your lucky bytes: ' + byteArray);
|
|
51
|
+
* ```
|
|
52
|
+
*/
|
|
53
|
+
export declare function getRandomValues<T extends IntBasedTypedArray | UintBasedTypedArray>(typedArray: T): T;
|
|
54
|
+
/**
|
|
55
|
+
* The `randomUUID()` method returns a unique identifier based on the V4 UUID spec (RFC4122).
|
|
56
|
+
* It uses cryptographically secure random values to generate the UUID.
|
|
57
|
+
*
|
|
58
|
+
* @return A string containing a newly generated UUIDv4 identifier
|
|
59
|
+
* @example
|
|
60
|
+
* ```ts
|
|
61
|
+
* const UUID = Crypto.randomUUID();
|
|
62
|
+
* console.log('Your UUID: ' + UUID);
|
|
63
|
+
* ```
|
|
64
|
+
*/
|
|
65
|
+
export declare function randomUUID(): any;
|
|
66
|
+
/**
|
|
67
|
+
* The `digest()` method of `Crypto` generates a digest of the supplied `TypedArray` of bytes `data` with the provided digest `algorithm`.
|
|
68
|
+
* A digest is a short fixed-length value derived from some variable-length input. **Cryptographic digests** should exhibit _collision-resistance_,
|
|
69
|
+
* meaning that it's very difficult to generate multiple inputs that have equal digest values.
|
|
70
|
+
* On web, this method can only be called from a secure origin (HTTPS) otherwise, an error will be thrown.
|
|
71
|
+
*
|
|
72
|
+
* @param algorithm The cryptographic hash function to use to transform a block of data into a fixed-size output.
|
|
73
|
+
* @param data The value that will be used to generate a digest.
|
|
74
|
+
* @return A Promise which fulfills with an ArrayBuffer representing the hashed input.
|
|
75
|
+
* @example
|
|
76
|
+
* ```ts
|
|
77
|
+
* const array = new Uint8Array([1, 2, 3, 4, 5]);
|
|
78
|
+
* const digest = await Crypto.digest(Crypto.CryptoDigestAlgorithm.SHA512, array);
|
|
79
|
+
* console.log('Your digest: ' + digest);
|
|
80
|
+
* ```
|
|
81
|
+
*/
|
|
82
|
+
export declare function digest(algorithm: CryptoDigestAlgorithm, data: BufferSource): Promise<ArrayBuffer>;
|
|
24
83
|
//# sourceMappingURL=Crypto.d.ts.map
|
package/build/Crypto.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Crypto.d.ts","sourceRoot":"","sources":["../src/Crypto.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"Crypto.d.ts","sourceRoot":"","sources":["../src/Crypto.ts"],"names":[],"mappings":"AACA,OAAO,EAAuB,mBAAmB,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAC;AAEjG,OAAO,EAAE,qBAAqB,EAAkB,mBAAmB,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAC;AAKpG,cAAc,gBAAgB,CAAC;AAW/B;;;;;;GAMG;AACH,wBAAgB,cAAc,CAAC,SAAS,EAAE,MAAM,GAAG,UAAU,CAqB5D;AAGD;;;;;GAKG;AACH,wBAAsB,mBAAmB,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,UAAU,CAAC,CAWhF;AA0CD;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAsB,iBAAiB,CACrC,SAAS,EAAE,qBAAqB,EAChC,IAAI,EAAE,MAAM,EACZ,OAAO,GAAE,mBAAsD,GAC9D,OAAO,CAAC,MAAM,CAAC,CAUjB;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,eAAe,CAAC,CAAC,SAAS,kBAAkB,GAAG,mBAAmB,EAChF,UAAU,EAAE,CAAC,GACZ,CAAC,CAGH;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,UAAU,QAEzB;AAYD;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,MAAM,CAAC,SAAS,EAAE,qBAAqB,EAAE,IAAI,EAAE,YAAY,GAAG,OAAO,CAAC,WAAW,CAAC,CAcjG"}
|
package/build/Crypto.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { toByteArray } from 'base64-js';
|
|
1
2
|
import { UnavailabilityError } from 'expo-modules-core';
|
|
2
3
|
import { CryptoDigestAlgorithm, CryptoEncoding } from './Crypto.types';
|
|
3
4
|
import ExpoCrypto from './ExpoCrypto';
|
|
@@ -8,6 +9,67 @@ class CryptoError extends TypeError {
|
|
|
8
9
|
super(`expo-crypto: ${message}`);
|
|
9
10
|
}
|
|
10
11
|
}
|
|
12
|
+
// @needsAudit
|
|
13
|
+
/**
|
|
14
|
+
* Generates completely random bytes using native implementations. The `byteCount` property
|
|
15
|
+
* is a `number` indicating the number of bytes to generate in the form of a `Uint8Array`.
|
|
16
|
+
* Falls back to `Math.random` during development to prevent issues with React Native Debugger.
|
|
17
|
+
* @param byteCount - A number within the range from `0` to `1024`. Anything else will throw a `TypeError`.
|
|
18
|
+
* @return An array of random bytes with the same length as the `byteCount`.
|
|
19
|
+
*/
|
|
20
|
+
export function getRandomBytes(byteCount) {
|
|
21
|
+
assertByteCount(byteCount, 'getRandomBytes');
|
|
22
|
+
const validByteCount = Math.floor(byteCount);
|
|
23
|
+
if (__DEV__) {
|
|
24
|
+
if (!global.nativeCallSyncHook || global.__REMOTEDEV__) {
|
|
25
|
+
// remote javascript debugging is enabled
|
|
26
|
+
const array = new Uint8Array(validByteCount);
|
|
27
|
+
for (let i = 0; i < validByteCount; i++) {
|
|
28
|
+
array[i] = Math.floor(Math.random() * 256);
|
|
29
|
+
}
|
|
30
|
+
return array;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
if (ExpoCrypto.getRandomBytes) {
|
|
34
|
+
return ExpoCrypto.getRandomBytes(validByteCount);
|
|
35
|
+
}
|
|
36
|
+
else if (ExpoCrypto.getRandomBase64String) {
|
|
37
|
+
const base64 = ExpoCrypto.getRandomBase64String(validByteCount);
|
|
38
|
+
return toByteArray(base64);
|
|
39
|
+
}
|
|
40
|
+
else {
|
|
41
|
+
throw new UnavailabilityError('expo-crypto', 'getRandomBytes');
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
// @needsAudit
|
|
45
|
+
/**
|
|
46
|
+
* Generates completely random bytes using native implementations. The `byteCount` property
|
|
47
|
+
* is a `number` indicating the number of bytes to generate in the form of a `Uint8Array`.
|
|
48
|
+
* @param byteCount - A number within the range from `0` to `1024`. Anything else will throw a `TypeError`.
|
|
49
|
+
* @return A promise that fulfills with an array of random bytes with the same length as the `byteCount`.
|
|
50
|
+
*/
|
|
51
|
+
export async function getRandomBytesAsync(byteCount) {
|
|
52
|
+
assertByteCount(byteCount, 'getRandomBytesAsync');
|
|
53
|
+
const validByteCount = Math.floor(byteCount);
|
|
54
|
+
if (ExpoCrypto.getRandomBytesAsync) {
|
|
55
|
+
return await ExpoCrypto.getRandomBytesAsync(validByteCount);
|
|
56
|
+
}
|
|
57
|
+
else if (ExpoCrypto.getRandomBase64StringAsync) {
|
|
58
|
+
const base64 = await ExpoCrypto.getRandomBase64StringAsync(validByteCount);
|
|
59
|
+
return toByteArray(base64);
|
|
60
|
+
}
|
|
61
|
+
else {
|
|
62
|
+
throw new UnavailabilityError('expo-crypto', 'getRandomBytesAsync');
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
function assertByteCount(value, methodName) {
|
|
66
|
+
if (typeof value !== 'number' ||
|
|
67
|
+
isNaN(value) ||
|
|
68
|
+
Math.floor(value) < 0 ||
|
|
69
|
+
Math.floor(value) > 1024) {
|
|
70
|
+
throw new TypeError(`expo-crypto: ${methodName}(${value}) expected a valid number from range 0...1024`);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
11
73
|
function assertAlgorithm(algorithm) {
|
|
12
74
|
if (!Object.values(CryptoDigestAlgorithm).includes(algorithm)) {
|
|
13
75
|
throw new CryptoError(`Invalid algorithm provided. Expected one of: CryptoDigestAlgorithm.${Object.keys(CryptoDigestAlgorithm).join(', AlgCryptoDigestAlgorithmorithm.')}`);
|
|
@@ -29,7 +91,7 @@ function assertEncoding(encoding) {
|
|
|
29
91
|
* A digest is a short fixed-length value derived from some variable-length input. **Cryptographic digests** should exhibit _collision-resistance_,
|
|
30
92
|
* meaning that it's very difficult to generate multiple inputs that have equal digest values.
|
|
31
93
|
* You can specify the returned string format as one of `CryptoEncoding`. By default, the resolved value will be formatted as a `HEX` string.
|
|
32
|
-
* On web, this method can only be called from a secure origin (
|
|
94
|
+
* On web, this method can only be called from a secure origin (HTTPS) otherwise, an error will be thrown.
|
|
33
95
|
*
|
|
34
96
|
* @param algorithm The cryptographic hash function to use to transform a block of data into a fixed-size output.
|
|
35
97
|
* @param data The value that will be used to generate a digest.
|
|
@@ -53,4 +115,77 @@ export async function digestStringAsync(algorithm, data, options = { encoding: C
|
|
|
53
115
|
assertEncoding(options.encoding);
|
|
54
116
|
return await ExpoCrypto.digestStringAsync(algorithm, data, options);
|
|
55
117
|
}
|
|
118
|
+
/**
|
|
119
|
+
* The `getRandomValues()` method of `Crypto` fills a provided `TypedArray` with cryptographically secure random values.
|
|
120
|
+
*
|
|
121
|
+
* @param typedArray An integer based [`TypedArray`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray) to fill with cryptographically secure random values. It modifies the input array in place.
|
|
122
|
+
* @return The input array filled with cryptographically secure random values.
|
|
123
|
+
*
|
|
124
|
+
* @example
|
|
125
|
+
* ```ts
|
|
126
|
+
* const byteArray = new Uint8Array(16);
|
|
127
|
+
* Crypto.getRandomValues(byteArray);
|
|
128
|
+
* console.log('Your lucky bytes: ' + byteArray);
|
|
129
|
+
* ```
|
|
130
|
+
*/
|
|
131
|
+
export function getRandomValues(typedArray) {
|
|
132
|
+
ExpoCrypto.getRandomValues(typedArray);
|
|
133
|
+
return typedArray;
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* The `randomUUID()` method returns a unique identifier based on the V4 UUID spec (RFC4122).
|
|
137
|
+
* It uses cryptographically secure random values to generate the UUID.
|
|
138
|
+
*
|
|
139
|
+
* @return A string containing a newly generated UUIDv4 identifier
|
|
140
|
+
* @example
|
|
141
|
+
* ```ts
|
|
142
|
+
* const UUID = Crypto.randomUUID();
|
|
143
|
+
* console.log('Your UUID: ' + UUID);
|
|
144
|
+
* ```
|
|
145
|
+
*/
|
|
146
|
+
export function randomUUID() {
|
|
147
|
+
return ExpoCrypto.randomUUID();
|
|
148
|
+
}
|
|
149
|
+
const digestLengths = {
|
|
150
|
+
[CryptoDigestAlgorithm.SHA1]: 20,
|
|
151
|
+
[CryptoDigestAlgorithm.SHA256]: 32,
|
|
152
|
+
[CryptoDigestAlgorithm.SHA384]: 48,
|
|
153
|
+
[CryptoDigestAlgorithm.SHA512]: 64,
|
|
154
|
+
[CryptoDigestAlgorithm.MD2]: 16,
|
|
155
|
+
[CryptoDigestAlgorithm.MD4]: 16,
|
|
156
|
+
[CryptoDigestAlgorithm.MD5]: 16,
|
|
157
|
+
};
|
|
158
|
+
/**
|
|
159
|
+
* The `digest()` method of `Crypto` generates a digest of the supplied `TypedArray` of bytes `data` with the provided digest `algorithm`.
|
|
160
|
+
* A digest is a short fixed-length value derived from some variable-length input. **Cryptographic digests** should exhibit _collision-resistance_,
|
|
161
|
+
* meaning that it's very difficult to generate multiple inputs that have equal digest values.
|
|
162
|
+
* On web, this method can only be called from a secure origin (HTTPS) otherwise, an error will be thrown.
|
|
163
|
+
*
|
|
164
|
+
* @param algorithm The cryptographic hash function to use to transform a block of data into a fixed-size output.
|
|
165
|
+
* @param data The value that will be used to generate a digest.
|
|
166
|
+
* @return A Promise which fulfills with an ArrayBuffer representing the hashed input.
|
|
167
|
+
* @example
|
|
168
|
+
* ```ts
|
|
169
|
+
* const array = new Uint8Array([1, 2, 3, 4, 5]);
|
|
170
|
+
* const digest = await Crypto.digest(Crypto.CryptoDigestAlgorithm.SHA512, array);
|
|
171
|
+
* console.log('Your digest: ' + digest);
|
|
172
|
+
* ```
|
|
173
|
+
*/
|
|
174
|
+
export function digest(algorithm, data) {
|
|
175
|
+
return new Promise((resolve, reject) => {
|
|
176
|
+
try {
|
|
177
|
+
if (typeof ExpoCrypto.digestAsync === 'function') {
|
|
178
|
+
resolve(ExpoCrypto.digestAsync(algorithm, data));
|
|
179
|
+
}
|
|
180
|
+
else {
|
|
181
|
+
const output = new Uint8Array(digestLengths[algorithm]);
|
|
182
|
+
ExpoCrypto.digest(algorithm, output, data);
|
|
183
|
+
resolve(output.buffer);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
catch (error) {
|
|
187
|
+
reject(error);
|
|
188
|
+
}
|
|
189
|
+
});
|
|
190
|
+
}
|
|
56
191
|
//# sourceMappingURL=Crypto.js.map
|
package/build/Crypto.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Crypto.js","sourceRoot":"","sources":["../src/Crypto.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,MAAM,mBAAmB,CAAC;AAExD,OAAO,EAAE,qBAAqB,EAAE,cAAc,EAA+B,MAAM,gBAAgB,CAAC;AACpG,OAAO,UAAU,MAAM,cAAc,CAAC;AAEtC,cAAc,gBAAgB,CAAC;AAE/B,MAAM,WAAY,SAAQ,SAAS;IACjC,IAAI,GAAG,YAAY,CAAC;IAEpB,YAAY,OAAe;QACzB,KAAK,CAAC,gBAAgB,OAAO,EAAE,CAAC,CAAC;IACnC,CAAC;CACF;AAED,SAAS,eAAe,CAAC,SAAgC;IACvD,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,qBAAqB,CAAC,CAAC,QAAQ,CAAC,SAAS,CAAC,EAAE;QAC7D,MAAM,IAAI,WAAW,CACnB,sEAAsE,MAAM,CAAC,IAAI,CAC/E,qBAAqB,CACtB,CAAC,IAAI,CAAC,mCAAmC,CAAC,EAAE,CAC9C,CAAC;KACH;AACH,CAAC;AAED,SAAS,UAAU,CAAC,IAAY;IAC9B,IAAI,OAAO,IAAI,KAAK,QAAQ,EAAE;QAC5B,MAAM,IAAI,WAAW,CAAC,2CAA2C,CAAC,CAAC;KACpE;AACH,CAAC;AAED,SAAS,cAAc,CAAC,QAAwB;IAC9C,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,cAAc,CAAC,CAAC,QAAQ,CAAC,QAAQ,CAAC,EAAE;QACrD,MAAM,IAAI,WAAW,CACnB,8DAA8D,MAAM,CAAC,IAAI,CACvE,cAAc,CACf,CAAC,IAAI,CAAC,mBAAmB,CAAC,EAAE,CAC9B,CAAC;KACH;AACH,CAAC;AAED,cAAc;AACd;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,CAAC,KAAK,UAAU,iBAAiB,CACrC,SAAgC,EAChC,IAAY,EACZ,UAA+B,EAAE,QAAQ,EAAE,cAAc,CAAC,GAAG,EAAE;IAE/D,IAAI,CAAC,UAAU,CAAC,iBAAiB,EAAE;QACjC,MAAM,IAAI,mBAAmB,CAAC,aAAa,EAAE,mBAAmB,CAAC,CAAC;KACnE;IAED,eAAe,CAAC,SAAS,CAAC,CAAC;IAC3B,UAAU,CAAC,IAAI,CAAC,CAAC;IACjB,cAAc,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC;IAEjC,OAAO,MAAM,UAAU,CAAC,iBAAiB,CAAC,SAAS,EAAE,IAAI,EAAE,OAAO,CAAC,CAAC;AACtE,CAAC","sourcesContent":["import { UnavailabilityError } from 'expo-modules-core';\n\nimport { CryptoDigestAlgorithm, CryptoEncoding, CryptoDigestOptions, Digest } from './Crypto.types';\nimport ExpoCrypto from './ExpoCrypto';\n\nexport * from './Crypto.types';\n\nclass CryptoError extends TypeError {\n code = 'ERR_CRYPTO';\n\n constructor(message: string) {\n super(`expo-crypto: ${message}`);\n }\n}\n\nfunction assertAlgorithm(algorithm: CryptoDigestAlgorithm): void {\n if (!Object.values(CryptoDigestAlgorithm).includes(algorithm)) {\n throw new CryptoError(\n `Invalid algorithm provided. Expected one of: CryptoDigestAlgorithm.${Object.keys(\n CryptoDigestAlgorithm\n ).join(', AlgCryptoDigestAlgorithmorithm.')}`\n );\n }\n}\n\nfunction assertData(data: string): void {\n if (typeof data !== 'string') {\n throw new CryptoError(`Invalid data provided. Expected a string.`);\n }\n}\n\nfunction assertEncoding(encoding: CryptoEncoding): void {\n if (!Object.values(CryptoEncoding).includes(encoding)) {\n throw new CryptoError(\n `Invalid encoding provided. Expected one of: CryptoEncoding.${Object.keys(\n CryptoEncoding\n ).join(', CryptoEncoding.')}`\n );\n }\n}\n\n// @needsAudit\n/**\n * The `digestStringAsync()` method of `Crypto` generates a digest of the supplied `data` string with the provided digest `algorithm`.\n * A digest is a short fixed-length value derived from some variable-length input. **Cryptographic digests** should exhibit _collision-resistance_,\n * meaning that it's very difficult to generate multiple inputs that have equal digest values.\n * You can specify the returned string format as one of `CryptoEncoding`. By default, the resolved value will be formatted as a `HEX` string.\n * On web, this method can only be called from a secure origin (https) otherwise an error will be thrown.\n *\n * @param algorithm The cryptographic hash function to use to transform a block of data into a fixed-size output.\n * @param data The value that will be used to generate a digest.\n * @param options Format of the digest string. Defaults to: `CryptoDigestOptions.HEX`.\n * @return Return a Promise which fulfills with a value representing the hashed input.\n *\n * @example\n * ```ts\n * const digest = await Crypto.digestStringAsync(\n * Crypto.CryptoDigestAlgorithm.SHA512,\n * '🥓 Easy to Digest! 💙'\n * );\n * ```\n */\nexport async function digestStringAsync(\n algorithm: CryptoDigestAlgorithm,\n data: string,\n options: CryptoDigestOptions = { encoding: CryptoEncoding.HEX }\n): Promise<Digest> {\n if (!ExpoCrypto.digestStringAsync) {\n throw new UnavailabilityError('expo-crypto', 'digestStringAsync');\n }\n\n assertAlgorithm(algorithm);\n assertData(data);\n assertEncoding(options.encoding);\n\n return await ExpoCrypto.digestStringAsync(algorithm, data, options);\n}\n"]}
|
|
1
|
+
{"version":3,"file":"Crypto.js","sourceRoot":"","sources":["../src/Crypto.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,WAAW,CAAC;AACxC,OAAO,EAAE,mBAAmB,EAA2C,MAAM,mBAAmB,CAAC;AAEjG,OAAO,EAAE,qBAAqB,EAAE,cAAc,EAA+B,MAAM,gBAAgB,CAAC;AACpG,OAAO,UAAU,MAAM,cAAc,CAAC;AAItC,cAAc,gBAAgB,CAAC;AAE/B,MAAM,WAAY,SAAQ,SAAS;IACjC,IAAI,GAAG,YAAY,CAAC;IAEpB,YAAY,OAAe;QACzB,KAAK,CAAC,gBAAgB,OAAO,EAAE,CAAC,CAAC;IACnC,CAAC;CACF;AAED,cAAc;AACd;;;;;;GAMG;AACH,MAAM,UAAU,cAAc,CAAC,SAA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IAAI;YACF,IAAI,OAAO,UAAU,CAAC,WAAW,KAAK,UAAU,EAAE;gBAChD,OAAO,CAAC,UAAU,CAAC,WAAW,CAAC,SAAS,EAAE,IAAI,CAAC,CAAC,CAAC;aAClD;iBAAM;gBACL,MAAM,MAAM,GAAG,IAAI,UAAU,CAAC,aAAa,CAAC,SAAS,CAAC,CAAC,CAAC;gBACxD,UAAU,CAAC,MAAM,CAAC,SAAS,EAAE,MAAM,EAAE,IAAI,CAAC,CAAC;gBAC3C,OAAO,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;aACxB;SACF;QAAC,OAAO,KAAK,EAAE;YACd,MAAM,CAAC,KAAK,CAAC,CAAC;SACf;IACH,CAAC,CAAC,CAAC;AACL,CAAC","sourcesContent":["import { toByteArray } from 'base64-js';\nimport { UnavailabilityError, UintBasedTypedArray, IntBasedTypedArray } from 'expo-modules-core';\n\nimport { CryptoDigestAlgorithm, CryptoEncoding, CryptoDigestOptions, Digest } from './Crypto.types';\nimport ExpoCrypto from './ExpoCrypto';\n\ndeclare const global: any;\n\nexport * from './Crypto.types';\n\nclass CryptoError extends TypeError {\n code = 'ERR_CRYPTO';\n\n constructor(message: string) {\n super(`expo-crypto: ${message}`);\n }\n}\n\n// @needsAudit\n/**\n * Generates completely random bytes using native implementations. The `byteCount` property\n * is a `number` indicating the number of bytes to generate in the form of a `Uint8Array`.\n * Falls back to `Math.random` during development to prevent issues with React Native Debugger.\n * @param byteCount - A number within the range from `0` to `1024`. Anything else will throw a `TypeError`.\n * @return An array of random bytes with the same length as the `byteCount`.\n */\nexport function getRandomBytes(byteCount: number): Uint8Array {\n assertByteCount(byteCount, 'getRandomBytes');\n const validByteCount = Math.floor(byteCount);\n if (__DEV__) {\n if (!global.nativeCallSyncHook || global.__REMOTEDEV__) {\n // remote javascript debugging is enabled\n const array = new Uint8Array(validByteCount);\n for (let i = 0; i < validByteCount; i++) {\n array[i] = Math.floor(Math.random() * 256);\n }\n return array;\n }\n }\n if (ExpoCrypto.getRandomBytes) {\n return ExpoCrypto.getRandomBytes(validByteCount);\n } else if (ExpoCrypto.getRandomBase64String) {\n const base64 = ExpoCrypto.getRandomBase64String(validByteCount);\n return toByteArray(base64);\n } else {\n throw new UnavailabilityError('expo-crypto', 'getRandomBytes');\n }\n}\n\n// @needsAudit\n/**\n * Generates completely random bytes using native implementations. The `byteCount` property\n * is a `number` indicating the number of bytes to generate in the form of a `Uint8Array`.\n * @param byteCount - A number within the range from `0` to `1024`. Anything else will throw a `TypeError`.\n * @return A promise that fulfills with an array of random bytes with the same length as the `byteCount`.\n */\nexport async function getRandomBytesAsync(byteCount: number): Promise<Uint8Array> {\n assertByteCount(byteCount, 'getRandomBytesAsync');\n const validByteCount = Math.floor(byteCount);\n if (ExpoCrypto.getRandomBytesAsync) {\n return await ExpoCrypto.getRandomBytesAsync(validByteCount);\n } else if (ExpoCrypto.getRandomBase64StringAsync) {\n const base64 = await ExpoCrypto.getRandomBase64StringAsync(validByteCount);\n return toByteArray(base64);\n } else {\n throw new UnavailabilityError('expo-crypto', 'getRandomBytesAsync');\n }\n}\n\nfunction assertByteCount(value: any, methodName: string): void {\n if (\n typeof value !== 'number' ||\n isNaN(value) ||\n Math.floor(value) < 0 ||\n Math.floor(value) > 1024\n ) {\n throw new TypeError(\n `expo-crypto: ${methodName}(${value}) expected a valid number from range 0...1024`\n );\n }\n}\n\nfunction assertAlgorithm(algorithm: CryptoDigestAlgorithm): void {\n if (!Object.values(CryptoDigestAlgorithm).includes(algorithm)) {\n throw new CryptoError(\n `Invalid algorithm provided. Expected one of: CryptoDigestAlgorithm.${Object.keys(\n CryptoDigestAlgorithm\n ).join(', AlgCryptoDigestAlgorithmorithm.')}`\n );\n }\n}\n\nfunction assertData(data: string): void {\n if (typeof data !== 'string') {\n throw new CryptoError(`Invalid data provided. Expected a string.`);\n }\n}\n\nfunction assertEncoding(encoding: CryptoEncoding): void {\n if (!Object.values(CryptoEncoding).includes(encoding)) {\n throw new CryptoError(\n `Invalid encoding provided. Expected one of: CryptoEncoding.${Object.keys(\n CryptoEncoding\n ).join(', CryptoEncoding.')}`\n );\n }\n}\n\n// @needsAudit\n/**\n * The `digestStringAsync()` method of `Crypto` generates a digest of the supplied `data` string with the provided digest `algorithm`.\n * A digest is a short fixed-length value derived from some variable-length input. **Cryptographic digests** should exhibit _collision-resistance_,\n * meaning that it's very difficult to generate multiple inputs that have equal digest values.\n * You can specify the returned string format as one of `CryptoEncoding`. By default, the resolved value will be formatted as a `HEX` string.\n * On web, this method can only be called from a secure origin (HTTPS) otherwise, an error will be thrown.\n *\n * @param algorithm The cryptographic hash function to use to transform a block of data into a fixed-size output.\n * @param data The value that will be used to generate a digest.\n * @param options Format of the digest string. Defaults to: `CryptoDigestOptions.HEX`.\n * @return Return a Promise which fulfills with a value representing the hashed input.\n *\n * @example\n * ```ts\n * const digest = await Crypto.digestStringAsync(\n * Crypto.CryptoDigestAlgorithm.SHA512,\n * '🥓 Easy to Digest! 💙'\n * );\n * ```\n */\nexport async function digestStringAsync(\n algorithm: CryptoDigestAlgorithm,\n data: string,\n options: CryptoDigestOptions = { encoding: CryptoEncoding.HEX }\n): Promise<Digest> {\n if (!ExpoCrypto.digestStringAsync) {\n throw new UnavailabilityError('expo-crypto', 'digestStringAsync');\n }\n\n assertAlgorithm(algorithm);\n assertData(data);\n assertEncoding(options.encoding);\n\n return await ExpoCrypto.digestStringAsync(algorithm, data, options);\n}\n\n/**\n * The `getRandomValues()` method of `Crypto` fills a provided `TypedArray` with cryptographically secure random values.\n *\n * @param typedArray An integer based [`TypedArray`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray) to fill with cryptographically secure random values. It modifies the input array in place.\n * @return The input array filled with cryptographically secure random values.\n *\n * @example\n * ```ts\n * const byteArray = new Uint8Array(16);\n * Crypto.getRandomValues(byteArray);\n * console.log('Your lucky bytes: ' + byteArray);\n * ```\n */\nexport function getRandomValues<T extends IntBasedTypedArray | UintBasedTypedArray>(\n typedArray: T\n): T {\n ExpoCrypto.getRandomValues(typedArray);\n return typedArray;\n}\n\n/**\n * The `randomUUID()` method returns a unique identifier based on the V4 UUID spec (RFC4122).\n * It uses cryptographically secure random values to generate the UUID.\n *\n * @return A string containing a newly generated UUIDv4 identifier\n * @example\n * ```ts\n * const UUID = Crypto.randomUUID();\n * console.log('Your UUID: ' + UUID);\n * ```\n */\nexport function randomUUID() {\n return ExpoCrypto.randomUUID();\n}\n\nconst digestLengths = {\n [CryptoDigestAlgorithm.SHA1]: 20,\n [CryptoDigestAlgorithm.SHA256]: 32,\n [CryptoDigestAlgorithm.SHA384]: 48,\n [CryptoDigestAlgorithm.SHA512]: 64,\n [CryptoDigestAlgorithm.MD2]: 16,\n [CryptoDigestAlgorithm.MD4]: 16,\n [CryptoDigestAlgorithm.MD5]: 16,\n};\n\n/**\n * The `digest()` method of `Crypto` generates a digest of the supplied `TypedArray` of bytes `data` with the provided digest `algorithm`.\n * A digest is a short fixed-length value derived from some variable-length input. **Cryptographic digests** should exhibit _collision-resistance_,\n * meaning that it's very difficult to generate multiple inputs that have equal digest values.\n * On web, this method can only be called from a secure origin (HTTPS) otherwise, an error will be thrown.\n *\n * @param algorithm The cryptographic hash function to use to transform a block of data into a fixed-size output.\n * @param data The value that will be used to generate a digest.\n * @return A Promise which fulfills with an ArrayBuffer representing the hashed input.\n * @example\n * ```ts\n * const array = new Uint8Array([1, 2, 3, 4, 5]);\n * const digest = await Crypto.digest(Crypto.CryptoDigestAlgorithm.SHA512, array);\n * console.log('Your digest: ' + digest);\n * ```\n */\nexport function digest(algorithm: CryptoDigestAlgorithm, data: BufferSource): Promise<ArrayBuffer> {\n return new Promise((resolve, reject) => {\n try {\n if (typeof ExpoCrypto.digestAsync === 'function') {\n resolve(ExpoCrypto.digestAsync(algorithm, data));\n } else {\n const output = new Uint8Array(digestLengths[algorithm]);\n ExpoCrypto.digest(algorithm, output, data);\n resolve(output.buffer);\n }\n } catch (error) {\n reject(error);\n }\n });\n}\n"]}
|
package/build/Crypto.types.d.ts
CHANGED
|
@@ -42,11 +42,11 @@ export declare enum CryptoEncoding {
|
|
|
42
42
|
*/
|
|
43
43
|
BASE64 = "base64"
|
|
44
44
|
}
|
|
45
|
-
export
|
|
45
|
+
export type CryptoDigestOptions = {
|
|
46
46
|
/**
|
|
47
47
|
* Format the digest is returned in.
|
|
48
48
|
*/
|
|
49
49
|
encoding: CryptoEncoding;
|
|
50
50
|
};
|
|
51
|
-
export
|
|
51
|
+
export type Digest = string;
|
|
52
52
|
//# sourceMappingURL=Crypto.types.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Crypto.types.d.ts","sourceRoot":"","sources":["../src/Crypto.types.ts"],"names":[],"mappings":"AACA;;GAEG;AACH,oBAAY,qBAAqB;IAC/B;;OAEG;IACH,IAAI,UAAU;IACd;;OAEG;IACH,MAAM,YAAY;IAClB;;OAEG;IACH,MAAM,YAAY;IAClB;;OAEG;IACH,MAAM,YAAY;IAClB;;;OAGG;IACH,GAAG,QAAQ;IACX;;;OAGG;IACH,GAAG,QAAQ;IACX;;;;OAIG;IACH,GAAG,QAAQ;CACZ;AAGD,oBAAY,cAAc;IACxB,GAAG,QAAQ;IACX;;OAEG;IACH,MAAM,WAAW;CAClB;AAGD,
|
|
1
|
+
{"version":3,"file":"Crypto.types.d.ts","sourceRoot":"","sources":["../src/Crypto.types.ts"],"names":[],"mappings":"AACA;;GAEG;AACH,oBAAY,qBAAqB;IAC/B;;OAEG;IACH,IAAI,UAAU;IACd;;OAEG;IACH,MAAM,YAAY;IAClB;;OAEG;IACH,MAAM,YAAY;IAClB;;OAEG;IACH,MAAM,YAAY;IAClB;;;OAGG;IACH,GAAG,QAAQ;IACX;;;OAGG;IACH,GAAG,QAAQ;IACX;;;;OAIG;IACH,GAAG,QAAQ;CACZ;AAGD,oBAAY,cAAc;IACxB,GAAG,QAAQ;IACX;;OAEG;IACH,MAAM,WAAW;CAClB;AAGD,MAAM,MAAM,mBAAmB,GAAG;IAChC;;OAEG;IACH,QAAQ,EAAE,cAAc,CAAC;CAC1B,CAAC;AAGF,MAAM,MAAM,MAAM,GAAG,MAAM,CAAC"}
|
|
@@ -1,7 +1,13 @@
|
|
|
1
|
+
import { TypedArray } from 'expo-modules-core';
|
|
1
2
|
import { CryptoDigestAlgorithm, CryptoDigestOptions } from './Crypto.types';
|
|
2
3
|
declare const _default: {
|
|
3
4
|
readonly name: string;
|
|
4
5
|
digestStringAsync(algorithm: CryptoDigestAlgorithm, data: string, options: CryptoDigestOptions): Promise<string>;
|
|
6
|
+
getRandomBytes(length: number): Uint8Array;
|
|
7
|
+
getRandomBytesAsync(length: number): Promise<Uint8Array>;
|
|
8
|
+
getRandomValues(typedArray: TypedArray): TypedArray;
|
|
9
|
+
randomUUID(): string;
|
|
10
|
+
digestAsync(algorithm: AlgorithmIdentifier, data: ArrayBuffer): Promise<ArrayBuffer>;
|
|
5
11
|
};
|
|
6
12
|
export default _default;
|
|
7
13
|
//# sourceMappingURL=ExpoCrypto.web.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ExpoCrypto.web.d.ts","sourceRoot":"","sources":["../src/ExpoCrypto.web.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"ExpoCrypto.web.d.ts","sourceRoot":"","sources":["../src/ExpoCrypto.web.ts"],"names":[],"mappings":"AAAA,OAAO,EAAc,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAE3D,OAAO,EAAE,qBAAqB,EAAkB,mBAAmB,EAAE,MAAM,gBAAgB,CAAC;;;iCAS7E,qBAAqB,QAC1B,MAAM,WACH,mBAAmB,GAC3B,QAAQ,MAAM,CAAC;2BAiBK,MAAM,GAAG,UAAU;gCAIR,MAAM,GAAG,QAAQ,UAAU,CAAC;gCAIlC,UAAU;;2BAMf,mBAAmB,QAAQ,WAAW,GAAG,QAAQ,WAAW,CAAC;;AAvCtF,wBA0CE"}
|
package/build/ExpoCrypto.web.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { CodedError } from 'expo-modules-core';
|
|
2
2
|
import { CryptoEncoding } from './Crypto.types';
|
|
3
|
+
const getCrypto = () => window.crypto ?? window.msCrypto;
|
|
3
4
|
export default {
|
|
4
5
|
get name() {
|
|
5
6
|
return 'ExpoCrypto';
|
|
@@ -19,6 +20,23 @@ export default {
|
|
|
19
20
|
}
|
|
20
21
|
throw new CodedError('ERR_CRYPTO_DIGEST', 'Invalid encoding type provided.');
|
|
21
22
|
},
|
|
23
|
+
getRandomBytes(length) {
|
|
24
|
+
const array = new Uint8Array(length);
|
|
25
|
+
return getCrypto().getRandomValues(array);
|
|
26
|
+
},
|
|
27
|
+
async getRandomBytesAsync(length) {
|
|
28
|
+
const array = new Uint8Array(length);
|
|
29
|
+
return getCrypto().getRandomValues(array);
|
|
30
|
+
},
|
|
31
|
+
getRandomValues(typedArray) {
|
|
32
|
+
return getCrypto().getRandomValues(typedArray);
|
|
33
|
+
},
|
|
34
|
+
randomUUID() {
|
|
35
|
+
return getCrypto().randomUUID();
|
|
36
|
+
},
|
|
37
|
+
digestAsync(algorithm, data) {
|
|
38
|
+
return getCrypto().subtle.digest(algorithm, data);
|
|
39
|
+
},
|
|
22
40
|
};
|
|
23
41
|
function hexString(buffer) {
|
|
24
42
|
const byteArray = new Uint8Array(buffer);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ExpoCrypto.web.js","sourceRoot":"","sources":["../src/ExpoCrypto.web.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,
|
|
1
|
+
{"version":3,"file":"ExpoCrypto.web.js","sourceRoot":"","sources":["../src/ExpoCrypto.web.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAc,MAAM,mBAAmB,CAAC;AAE3D,OAAO,EAAyB,cAAc,EAAuB,MAAM,gBAAgB,CAAC;AAE5F,MAAM,SAAS,GAAG,GAAW,EAAE,CAAC,MAAM,CAAC,MAAM,IAAK,M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U,CAAC,CAAC;IACjD,CAAC;IACD,UAAU;QACR,OAAO,SAAS,EAAE,CAAC,UAAU,EAAE,CAAC;IAClC,CAAC;IACD,WAAW,CAAC,SAA8B,EAAE,IAAiB;QAC3D,OAAO,SAAS,EAAE,CAAC,MAAM,CAAC,MAAM,CAAC,SAAS,EAAE,IAAI,CAAC,CAAC;IACpD,CAAC;CACF,CAAC;AAEF,SAAS,SAAS,CAAC,MAAmB;IACpC,MAAM,SAAS,GAAG,IAAI,UAAU,CAAC,MAAM,CAAC,CAAC;IAEzC,MAAM,QAAQ,GAAG,CAAC,GAAG,SAAS,CAAC,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE;QAC5C,MAAM,OAAO,GAAG,KAAK,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC;QACnC,MAAM,aAAa,GAAG,OAAO,CAAC,QAAQ,CAAC,CAAC,EAAE,GAAG,CAAC,CAAC;QAC/C,OAAO,aAAa,CAAC;IACvB,CAAC,CAAC,CAAC;IAEH,OAAO,QAAQ,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;AAC3B,CAAC","sourcesContent":["import { CodedError, TypedArray } from 'expo-modules-core';\n\nimport { CryptoDigestAlgorithm, CryptoEncoding, CryptoDigestOptions } from './Crypto.types';\n\nconst getCrypto = (): Crypto => window.crypto ?? (window as any).msCrypto;\n\nexport default {\n get name(): string {\n return 'ExpoCrypto';\n },\n async digestStringAsync(\n algorithm: CryptoDigestAlgorithm,\n data: string,\n options: CryptoDigestOptions\n ): Promise<string> {\n if (!crypto.subtle) {\n throw new CodedError(\n 'ERR_CRYPTO_UNAVAILABLE',\n 'Access to the WebCrypto API is restricted to secure origins (https).'\n );\n }\n const encoder = new TextEncoder();\n const buffer = encoder.encode(data);\n const hashedData = await crypto.subtle.digest(algorithm, buffer);\n if (options.encoding === CryptoEncoding.HEX) {\n return hexString(hashedData);\n } else if (options.encoding === CryptoEncoding.BASE64) {\n return btoa(String.fromCharCode(...new Uint8Array(hashedData)));\n }\n throw new CodedError('ERR_CRYPTO_DIGEST', 'Invalid encoding type provided.');\n },\n getRandomBytes(length: number): Uint8Array {\n const array = new Uint8Array(length);\n return getCrypto().getRandomValues(array);\n },\n async getRandomBytesAsync(length: number): Promise<Uint8Array> {\n const array = new Uint8Array(length);\n return getCrypto().getRandomValues(array);\n },\n getRandomValues(typedArray: TypedArray) {\n return getCrypto().getRandomValues(typedArray);\n },\n randomUUID() {\n return getCrypto().randomUUID();\n },\n digestAsync(algorithm: AlgorithmIdentifier, data: ArrayBuffer): Promise<ArrayBuffer> {\n return getCrypto().subtle.digest(algorithm, data);\n },\n};\n\nfunction hexString(buffer: ArrayBuffer): string {\n const byteArray = new Uint8Array(buffer);\n\n const hexCodes = [...byteArray].map((value) => {\n const hexCode = value.toString(16);\n const paddedHexCode = hexCode.padStart(2, '0');\n return paddedHexCode;\n });\n\n return hexCodes.join('');\n}\n"]}
|
package/ios/CryptoModule.swift
CHANGED
|
@@ -10,9 +10,31 @@ public class CryptoModule: Module {
|
|
|
10
10
|
AsyncFunction("digestStringAsync", digestString)
|
|
11
11
|
|
|
12
12
|
Function("digestString", digestString)
|
|
13
|
+
|
|
14
|
+
AsyncFunction("getRandomBase64StringAsync", getRandomBase64String)
|
|
15
|
+
|
|
16
|
+
Function("getRandomBase64String", getRandomBase64String)
|
|
17
|
+
|
|
18
|
+
Function("getRandomValues", getRandomValues)
|
|
19
|
+
|
|
20
|
+
Function("digest", digest)
|
|
21
|
+
|
|
22
|
+
Function("randomUUID") {
|
|
23
|
+
UUID().uuidString
|
|
24
|
+
}
|
|
13
25
|
}
|
|
14
26
|
}
|
|
15
27
|
|
|
28
|
+
private func getRandomBase64String(length: Int) throws -> String {
|
|
29
|
+
var bytes = [UInt8](repeating: 0, count: length)
|
|
30
|
+
let status = SecRandomCopyBytes(kSecRandomDefault, length, &bytes)
|
|
31
|
+
|
|
32
|
+
guard status == errSecSuccess else {
|
|
33
|
+
throw FailedGeneratingRandomBytesException(status)
|
|
34
|
+
}
|
|
35
|
+
return Data(bytes).base64EncodedString()
|
|
36
|
+
}
|
|
37
|
+
|
|
16
38
|
private func digestString(algorithm: DigestAlgorithm, str: String, options: DigestOptions) throws -> String {
|
|
17
39
|
guard let data = str.data(using: .utf8) else {
|
|
18
40
|
throw LossyConversionException()
|
|
@@ -33,8 +55,33 @@ private func digestString(algorithm: DigestAlgorithm, str: String, options: Dige
|
|
|
33
55
|
}
|
|
34
56
|
}
|
|
35
57
|
|
|
58
|
+
private func getRandomValues(array: TypedArray) throws -> TypedArray {
|
|
59
|
+
let status = SecRandomCopyBytes(
|
|
60
|
+
kSecRandomDefault,
|
|
61
|
+
array.byteLength,
|
|
62
|
+
array.rawPointer
|
|
63
|
+
)
|
|
64
|
+
|
|
65
|
+
guard status == errSecSuccess else {
|
|
66
|
+
throw FailedGeneratingRandomBytesException(status)
|
|
67
|
+
}
|
|
68
|
+
return array
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
private func digest(algorithm: DigestAlgorithm, output: TypedArray, data: TypedArray) {
|
|
72
|
+
let length = Int(algorithm.digestLength)
|
|
73
|
+
let outputPtr = output.rawPointer.assumingMemoryBound(to: UInt8.self)
|
|
74
|
+
algorithm.digest(data.rawPointer, UInt32(data.byteLength), outputPtr)
|
|
75
|
+
}
|
|
76
|
+
|
|
36
77
|
private class LossyConversionException: Exception {
|
|
37
78
|
override var reason: String {
|
|
38
79
|
"Unable to convert given string without losing some information"
|
|
39
80
|
}
|
|
40
81
|
}
|
|
82
|
+
|
|
83
|
+
private class FailedGeneratingRandomBytesException: GenericException<OSStatus> {
|
|
84
|
+
override var reason: String {
|
|
85
|
+
"Generating random bytes has failed with OSStatus code: \(param)"
|
|
86
|
+
}
|
|
87
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "expo-crypto",
|
|
3
|
-
"version": "12.
|
|
3
|
+
"version": "12.2.0",
|
|
4
4
|
"description": "Expo universal module for crypto",
|
|
5
5
|
"main": "build/Crypto.js",
|
|
6
6
|
"types": "build/Crypto.d.ts",
|
|
@@ -38,12 +38,14 @@
|
|
|
38
38
|
"jest": {
|
|
39
39
|
"preset": "expo-module-scripts"
|
|
40
40
|
},
|
|
41
|
-
"dependencies": {
|
|
41
|
+
"dependencies": {
|
|
42
|
+
"base64-js": "^1.3.0"
|
|
43
|
+
},
|
|
42
44
|
"devDependencies": {
|
|
43
45
|
"expo-module-scripts": "^3.0.0"
|
|
44
46
|
},
|
|
45
47
|
"peerDependencies": {
|
|
46
48
|
"expo": "*"
|
|
47
49
|
},
|
|
48
|
-
"gitHead": "
|
|
50
|
+
"gitHead": "1815e2eaad8c753588c7b1eb74420174a28e01f4"
|
|
49
51
|
}
|
package/src/Crypto.ts
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { toByteArray } from 'base64-js';
|
|
2
|
+
import { UnavailabilityError, UintBasedTypedArray, IntBasedTypedArray } from 'expo-modules-core';
|
|
2
3
|
|
|
3
4
|
import { CryptoDigestAlgorithm, CryptoEncoding, CryptoDigestOptions, Digest } from './Crypto.types';
|
|
4
5
|
import ExpoCrypto from './ExpoCrypto';
|
|
5
6
|
|
|
7
|
+
declare const global: any;
|
|
8
|
+
|
|
6
9
|
export * from './Crypto.types';
|
|
7
10
|
|
|
8
11
|
class CryptoError extends TypeError {
|
|
@@ -13,6 +16,70 @@ class CryptoError extends TypeError {
|
|
|
13
16
|
}
|
|
14
17
|
}
|
|
15
18
|
|
|
19
|
+
// @needsAudit
|
|
20
|
+
/**
|
|
21
|
+
* Generates completely random bytes using native implementations. The `byteCount` property
|
|
22
|
+
* is a `number` indicating the number of bytes to generate in the form of a `Uint8Array`.
|
|
23
|
+
* Falls back to `Math.random` during development to prevent issues with React Native Debugger.
|
|
24
|
+
* @param byteCount - A number within the range from `0` to `1024`. Anything else will throw a `TypeError`.
|
|
25
|
+
* @return An array of random bytes with the same length as the `byteCount`.
|
|
26
|
+
*/
|
|
27
|
+
export function getRandomBytes(byteCount: number): Uint8Array {
|
|
28
|
+
assertByteCount(byteCount, 'getRandomBytes');
|
|
29
|
+
const validByteCount = Math.floor(byteCount);
|
|
30
|
+
if (__DEV__) {
|
|
31
|
+
if (!global.nativeCallSyncHook || global.__REMOTEDEV__) {
|
|
32
|
+
// remote javascript debugging is enabled
|
|
33
|
+
const array = new Uint8Array(validByteCount);
|
|
34
|
+
for (let i = 0; i < validByteCount; i++) {
|
|
35
|
+
array[i] = Math.floor(Math.random() * 256);
|
|
36
|
+
}
|
|
37
|
+
return array;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
if (ExpoCrypto.getRandomBytes) {
|
|
41
|
+
return ExpoCrypto.getRandomBytes(validByteCount);
|
|
42
|
+
} else if (ExpoCrypto.getRandomBase64String) {
|
|
43
|
+
const base64 = ExpoCrypto.getRandomBase64String(validByteCount);
|
|
44
|
+
return toByteArray(base64);
|
|
45
|
+
} else {
|
|
46
|
+
throw new UnavailabilityError('expo-crypto', 'getRandomBytes');
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// @needsAudit
|
|
51
|
+
/**
|
|
52
|
+
* Generates completely random bytes using native implementations. The `byteCount` property
|
|
53
|
+
* is a `number` indicating the number of bytes to generate in the form of a `Uint8Array`.
|
|
54
|
+
* @param byteCount - A number within the range from `0` to `1024`. Anything else will throw a `TypeError`.
|
|
55
|
+
* @return A promise that fulfills with an array of random bytes with the same length as the `byteCount`.
|
|
56
|
+
*/
|
|
57
|
+
export async function getRandomBytesAsync(byteCount: number): Promise<Uint8Array> {
|
|
58
|
+
assertByteCount(byteCount, 'getRandomBytesAsync');
|
|
59
|
+
const validByteCount = Math.floor(byteCount);
|
|
60
|
+
if (ExpoCrypto.getRandomBytesAsync) {
|
|
61
|
+
return await ExpoCrypto.getRandomBytesAsync(validByteCount);
|
|
62
|
+
} else if (ExpoCrypto.getRandomBase64StringAsync) {
|
|
63
|
+
const base64 = await ExpoCrypto.getRandomBase64StringAsync(validByteCount);
|
|
64
|
+
return toByteArray(base64);
|
|
65
|
+
} else {
|
|
66
|
+
throw new UnavailabilityError('expo-crypto', 'getRandomBytesAsync');
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function assertByteCount(value: any, methodName: string): void {
|
|
71
|
+
if (
|
|
72
|
+
typeof value !== 'number' ||
|
|
73
|
+
isNaN(value) ||
|
|
74
|
+
Math.floor(value) < 0 ||
|
|
75
|
+
Math.floor(value) > 1024
|
|
76
|
+
) {
|
|
77
|
+
throw new TypeError(
|
|
78
|
+
`expo-crypto: ${methodName}(${value}) expected a valid number from range 0...1024`
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
16
83
|
function assertAlgorithm(algorithm: CryptoDigestAlgorithm): void {
|
|
17
84
|
if (!Object.values(CryptoDigestAlgorithm).includes(algorithm)) {
|
|
18
85
|
throw new CryptoError(
|
|
@@ -45,7 +112,7 @@ function assertEncoding(encoding: CryptoEncoding): void {
|
|
|
45
112
|
* A digest is a short fixed-length value derived from some variable-length input. **Cryptographic digests** should exhibit _collision-resistance_,
|
|
46
113
|
* meaning that it's very difficult to generate multiple inputs that have equal digest values.
|
|
47
114
|
* You can specify the returned string format as one of `CryptoEncoding`. By default, the resolved value will be formatted as a `HEX` string.
|
|
48
|
-
* On web, this method can only be called from a secure origin (
|
|
115
|
+
* On web, this method can only be called from a secure origin (HTTPS) otherwise, an error will be thrown.
|
|
49
116
|
*
|
|
50
117
|
* @param algorithm The cryptographic hash function to use to transform a block of data into a fixed-size output.
|
|
51
118
|
* @param data The value that will be used to generate a digest.
|
|
@@ -75,3 +142,80 @@ export async function digestStringAsync(
|
|
|
75
142
|
|
|
76
143
|
return await ExpoCrypto.digestStringAsync(algorithm, data, options);
|
|
77
144
|
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* The `getRandomValues()` method of `Crypto` fills a provided `TypedArray` with cryptographically secure random values.
|
|
148
|
+
*
|
|
149
|
+
* @param typedArray An integer based [`TypedArray`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray) to fill with cryptographically secure random values. It modifies the input array in place.
|
|
150
|
+
* @return The input array filled with cryptographically secure random values.
|
|
151
|
+
*
|
|
152
|
+
* @example
|
|
153
|
+
* ```ts
|
|
154
|
+
* const byteArray = new Uint8Array(16);
|
|
155
|
+
* Crypto.getRandomValues(byteArray);
|
|
156
|
+
* console.log('Your lucky bytes: ' + byteArray);
|
|
157
|
+
* ```
|
|
158
|
+
*/
|
|
159
|
+
export function getRandomValues<T extends IntBasedTypedArray | UintBasedTypedArray>(
|
|
160
|
+
typedArray: T
|
|
161
|
+
): T {
|
|
162
|
+
ExpoCrypto.getRandomValues(typedArray);
|
|
163
|
+
return typedArray;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* The `randomUUID()` method returns a unique identifier based on the V4 UUID spec (RFC4122).
|
|
168
|
+
* It uses cryptographically secure random values to generate the UUID.
|
|
169
|
+
*
|
|
170
|
+
* @return A string containing a newly generated UUIDv4 identifier
|
|
171
|
+
* @example
|
|
172
|
+
* ```ts
|
|
173
|
+
* const UUID = Crypto.randomUUID();
|
|
174
|
+
* console.log('Your UUID: ' + UUID);
|
|
175
|
+
* ```
|
|
176
|
+
*/
|
|
177
|
+
export function randomUUID() {
|
|
178
|
+
return ExpoCrypto.randomUUID();
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
const digestLengths = {
|
|
182
|
+
[CryptoDigestAlgorithm.SHA1]: 20,
|
|
183
|
+
[CryptoDigestAlgorithm.SHA256]: 32,
|
|
184
|
+
[CryptoDigestAlgorithm.SHA384]: 48,
|
|
185
|
+
[CryptoDigestAlgorithm.SHA512]: 64,
|
|
186
|
+
[CryptoDigestAlgorithm.MD2]: 16,
|
|
187
|
+
[CryptoDigestAlgorithm.MD4]: 16,
|
|
188
|
+
[CryptoDigestAlgorithm.MD5]: 16,
|
|
189
|
+
};
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* The `digest()` method of `Crypto` generates a digest of the supplied `TypedArray` of bytes `data` with the provided digest `algorithm`.
|
|
193
|
+
* A digest is a short fixed-length value derived from some variable-length input. **Cryptographic digests** should exhibit _collision-resistance_,
|
|
194
|
+
* meaning that it's very difficult to generate multiple inputs that have equal digest values.
|
|
195
|
+
* On web, this method can only be called from a secure origin (HTTPS) otherwise, an error will be thrown.
|
|
196
|
+
*
|
|
197
|
+
* @param algorithm The cryptographic hash function to use to transform a block of data into a fixed-size output.
|
|
198
|
+
* @param data The value that will be used to generate a digest.
|
|
199
|
+
* @return A Promise which fulfills with an ArrayBuffer representing the hashed input.
|
|
200
|
+
* @example
|
|
201
|
+
* ```ts
|
|
202
|
+
* const array = new Uint8Array([1, 2, 3, 4, 5]);
|
|
203
|
+
* const digest = await Crypto.digest(Crypto.CryptoDigestAlgorithm.SHA512, array);
|
|
204
|
+
* console.log('Your digest: ' + digest);
|
|
205
|
+
* ```
|
|
206
|
+
*/
|
|
207
|
+
export function digest(algorithm: CryptoDigestAlgorithm, data: BufferSource): Promise<ArrayBuffer> {
|
|
208
|
+
return new Promise((resolve, reject) => {
|
|
209
|
+
try {
|
|
210
|
+
if (typeof ExpoCrypto.digestAsync === 'function') {
|
|
211
|
+
resolve(ExpoCrypto.digestAsync(algorithm, data));
|
|
212
|
+
} else {
|
|
213
|
+
const output = new Uint8Array(digestLengths[algorithm]);
|
|
214
|
+
ExpoCrypto.digest(algorithm, output, data);
|
|
215
|
+
resolve(output.buffer);
|
|
216
|
+
}
|
|
217
|
+
} catch (error) {
|
|
218
|
+
reject(error);
|
|
219
|
+
}
|
|
220
|
+
});
|
|
221
|
+
}
|
package/src/ExpoCrypto.web.ts
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
|
-
import { CodedError } from 'expo-modules-core';
|
|
1
|
+
import { CodedError, TypedArray } from 'expo-modules-core';
|
|
2
2
|
|
|
3
3
|
import { CryptoDigestAlgorithm, CryptoEncoding, CryptoDigestOptions } from './Crypto.types';
|
|
4
4
|
|
|
5
|
+
const getCrypto = (): Crypto => window.crypto ?? (window as any).msCrypto;
|
|
6
|
+
|
|
5
7
|
export default {
|
|
6
8
|
get name(): string {
|
|
7
9
|
return 'ExpoCrypto';
|
|
@@ -27,6 +29,23 @@ export default {
|
|
|
27
29
|
}
|
|
28
30
|
throw new CodedError('ERR_CRYPTO_DIGEST', 'Invalid encoding type provided.');
|
|
29
31
|
},
|
|
32
|
+
getRandomBytes(length: number): Uint8Array {
|
|
33
|
+
const array = new Uint8Array(length);
|
|
34
|
+
return getCrypto().getRandomValues(array);
|
|
35
|
+
},
|
|
36
|
+
async getRandomBytesAsync(length: number): Promise<Uint8Array> {
|
|
37
|
+
const array = new Uint8Array(length);
|
|
38
|
+
return getCrypto().getRandomValues(array);
|
|
39
|
+
},
|
|
40
|
+
getRandomValues(typedArray: TypedArray) {
|
|
41
|
+
return getCrypto().getRandomValues(typedArray);
|
|
42
|
+
},
|
|
43
|
+
randomUUID() {
|
|
44
|
+
return getCrypto().randomUUID();
|
|
45
|
+
},
|
|
46
|
+
digestAsync(algorithm: AlgorithmIdentifier, data: ArrayBuffer): Promise<ArrayBuffer> {
|
|
47
|
+
return getCrypto().subtle.digest(algorithm, data);
|
|
48
|
+
},
|
|
30
49
|
};
|
|
31
50
|
|
|
32
51
|
function hexString(buffer: ArrayBuffer): string {
|