expo-crypto 12.1.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 +10 -0
- package/android/build.gradle +4 -4
- package/android/src/main/java/expo/modules/crypto/CryptoModule.kt +8 -0
- package/build/Crypto.d.ts +18 -1
- package/build/Crypto.d.ts.map +1 -1
- package/build/Crypto.js +43 -1
- package/build/Crypto.js.map +1 -1
- package/build/ExpoCrypto.web.d.ts +1 -0
- package/build/ExpoCrypto.web.d.ts.map +1 -1
- package/build/ExpoCrypto.web.js +3 -0
- package/build/ExpoCrypto.web.js.map +1 -1
- package/ios/CryptoModule.swift +8 -0
- package/package.json +2 -2
- package/src/Crypto.ts +43 -1
- package/src/ExpoCrypto.web.ts +3 -0
package/CHANGELOG.md
CHANGED
|
@@ -10,6 +10,16 @@
|
|
|
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
|
+
|
|
13
23
|
## 12.1.0 — 2022-12-30
|
|
14
24
|
|
|
15
25
|
### 🎉 New features
|
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
|
|
@@ -19,6 +19,7 @@ class CryptoModule : Module() {
|
|
|
19
19
|
Function("getRandomBase64String", this@CryptoModule::getRandomBase64String)
|
|
20
20
|
AsyncFunction("getRandomBase64StringAsync", this@CryptoModule::getRandomBase64String)
|
|
21
21
|
Function("getRandomValues", this@CryptoModule::getRandomValues)
|
|
22
|
+
Function("digest", this@CryptoModule::digest)
|
|
22
23
|
Function("randomUUID") {
|
|
23
24
|
UUID.randomUUID().toString()
|
|
24
25
|
}
|
|
@@ -48,6 +49,13 @@ class CryptoModule : Module() {
|
|
|
48
49
|
}
|
|
49
50
|
}
|
|
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
|
+
|
|
51
59
|
private fun getRandomValues(typedArray: TypedArray) {
|
|
52
60
|
val array = ByteArray(typedArray.byteLength)
|
|
53
61
|
secureRandom.nextBytes(array)
|
package/build/Crypto.d.ts
CHANGED
|
@@ -21,7 +21,7 @@ export declare function getRandomBytesAsync(byteCount: number): Promise<Uint8Arr
|
|
|
21
21
|
* A digest is a short fixed-length value derived from some variable-length input. **Cryptographic digests** should exhibit _collision-resistance_,
|
|
22
22
|
* meaning that it's very difficult to generate multiple inputs that have equal digest values.
|
|
23
23
|
* You can specify the returned string format as one of `CryptoEncoding`. By default, the resolved value will be formatted as a `HEX` string.
|
|
24
|
-
* 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.
|
|
25
25
|
*
|
|
26
26
|
* @param algorithm The cryptographic hash function to use to transform a block of data into a fixed-size output.
|
|
27
27
|
* @param data The value that will be used to generate a digest.
|
|
@@ -63,4 +63,21 @@ export declare function getRandomValues<T extends IntBasedTypedArray | UintBased
|
|
|
63
63
|
* ```
|
|
64
64
|
*/
|
|
65
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>;
|
|
66
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":"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"}
|
|
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
|
@@ -91,7 +91,7 @@ function assertEncoding(encoding) {
|
|
|
91
91
|
* A digest is a short fixed-length value derived from some variable-length input. **Cryptographic digests** should exhibit _collision-resistance_,
|
|
92
92
|
* meaning that it's very difficult to generate multiple inputs that have equal digest values.
|
|
93
93
|
* You can specify the returned string format as one of `CryptoEncoding`. By default, the resolved value will be formatted as a `HEX` string.
|
|
94
|
-
* 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.
|
|
95
95
|
*
|
|
96
96
|
* @param algorithm The cryptographic hash function to use to transform a block of data into a fixed-size output.
|
|
97
97
|
* @param data The value that will be used to generate a digest.
|
|
@@ -146,4 +146,46 @@ export function getRandomValues(typedArray) {
|
|
|
146
146
|
export function randomUUID() {
|
|
147
147
|
return ExpoCrypto.randomUUID();
|
|
148
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
|
+
}
|
|
149
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,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,SAAiB;IAC9C,eAAe,CAAC,SAAS,EAAE,gBAAgB,CAAC,CAAC;IAC7C,MAAM,cAAc,GAAG,IAAI,CAAC,KAAK,CAAC,SAAS,CAAC,CAAC;IAC7C,IAAI,OAAO,EAAE;QACX,IAAI,CAAC,MAAM,CAAC,kBAAkB,IAAI,MAAM,CAAC,aAAa,EAAE;YACtD,yCAAyC;YACzC,MAAM,KAAK,GAAG,IAAI,UAAU,CAAC,cAAc,CAAC,CAAC;YAC7C,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,cAAc,EAAE,CAAC,EAAE,EAAE;gBACvC,KAAK,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC,KAAK,CAAC,IAA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,CAAC;AAED;;;;;;;;;;;;GAYG;AACH,MAAM,UAAU,eAAe,CAC7B,UAAa;IAEb,UAAU,CAAC,eAAe,CAAC,UAAU,CAAC,CAAC;IACvC,OAAO,UAAU,CAAC;AACpB,CAAC;AAED;;;;;;;;;;GAUG;AACH,MAAM,UAAU,UAAU;IACxB,OAAO,UAAU,CAAC,UAAU,EAAE,CAAC;AACjC,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"]}
|
|
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,SAAiB;IAC9C,eAAe,CAAC,SAAS,EAAE,gBAAgB,CAAC,CAAC;IAC7C,MAAM,cAAc,GAAG,IAAI,CAAC,KAAK,CAAC,SAAS,CAAC,CAAC;IAC7C,IAAI,OAAO,EAAE;QACX,IAAI,CAAC,MAAM,CAAC,kBAAkB,IAAI,MAAM,CAAC,aAAa,EAAE;YACtD,yCAAyC;YACzC,MAAM,KAAK,GAAG,IAAI,UAAU,CAAC,cAAc,CAAC,CAAC;YAC7C,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,cAAc,EAAE,CAAC,EAAE,EAAE;gBACvC,KAAK,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,MAAM,EAAE,GAAG,GAAG,CAAC,CAAC;aAC5C;YACD,OAAO,KAAK,CAAC;SACd;KACF;IACD,IAAI,UAAU,CAAC,cAAc,EAAE;QAC7B,OAAO,UAAU,CAAC,cAAc,CAAC,cAAc,CAAC,CAAC;KAClD;SAAM,IAAI,UAAU,CAAC,qBAAqB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U,CAAC,UAAU,EAAE,CAAC;AACjC,CAAC;AAED,MAAM,aAAa,GAAG;IACpB,CAAC,qBAAqB,CAAC,IAAI,CAAC,EAAE,EAAE;IAChC,CAAC,qBAAqB,CAAC,MAAM,CAAC,EAAE,EAAE;IAClC,CAAC,qBAAqB,CAAC,MAAM,CAAC,EAAE,EAAE;IAClC,CAAC,qBAAqB,CAAC,MAAM,CAAC,EAAE,EAAE;IAClC,CAAC,qBAAqB,CAAC,GAAG,CAAC,EAAE,EAAE;IAC/B,CAAC,qBAAqB,CAAC,GAAG,CAAC,EAAE,EAAE;IAC/B,CAAC,qBAAqB,CAAC,GAAG,CAAC,EAAE,EAAE;CAChC,CAAC;AAEF;;;;;;;;;;;;;;;GAeG;AACH,MAAM,UAAU,MAAM,CAAC,SAAgC,EAAE,IAAkB;IACzE,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;QACrC,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"]}
|
|
@@ -7,6 +7,7 @@ declare const _default: {
|
|
|
7
7
|
getRandomBytesAsync(length: number): Promise<Uint8Array>;
|
|
8
8
|
getRandomValues(typedArray: TypedArray): TypedArray;
|
|
9
9
|
randomUUID(): string;
|
|
10
|
+
digestAsync(algorithm: AlgorithmIdentifier, data: ArrayBuffer): Promise<ArrayBuffer>;
|
|
10
11
|
};
|
|
11
12
|
export default _default;
|
|
12
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":"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
|
|
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 +1 @@
|
|
|
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,MAAc,CAAC,QAAQ,CAAC;AAE1E,eAAe;IACb,IAAI,IAAI;QACN,OAAO,YAAY,CAAC;IACtB,CAAC;IACD,KAAK,CAAC,iBAAiB,CACrB,SAAgC,EAChC,IAAY,EACZ,OAA4B;QAE5B,IAAI,CAAC,MAAM,CAAC,MAAM,EAAE;YAClB,MAAM,IAAI,UAAU,CAClB,wBAAwB,EACxB,sEAAsE,CACvE,CAAC;SACH;QACD,MAAM,OAAO,GAAG,IAAI,WAAW,EAAE,CAAC;QAClC,MAAM,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;QACpC,MAAM,UAAU,GAAG,MAAM,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,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,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};\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"]}
|
|
1
|
+
{"version":3,"file":"ExpoCrypto.web.js","sourceRoot":"","sources":["../src/ExpoCrypto.web.ts"],"names":[],"mappings":"AAAA,OAAO,E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AAM,CAAC,CAAC;QACrC,OAAO,SAAS,EAAE,CAAC,eAAe,CAAC,KAAK,CAAC,CAAC;IAC5C,CAAC;IACD,eAAe,CAAC,UAAsB;QACpC,OAAO,SAAS,EAAE,CAAC,eAAe,CAAC,UAAU,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
|
@@ -17,6 +17,8 @@ public class CryptoModule: Module {
|
|
|
17
17
|
|
|
18
18
|
Function("getRandomValues", getRandomValues)
|
|
19
19
|
|
|
20
|
+
Function("digest", digest)
|
|
21
|
+
|
|
20
22
|
Function("randomUUID") {
|
|
21
23
|
UUID().uuidString
|
|
22
24
|
}
|
|
@@ -66,6 +68,12 @@ private func getRandomValues(array: TypedArray) throws -> TypedArray {
|
|
|
66
68
|
return array
|
|
67
69
|
}
|
|
68
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
|
+
|
|
69
77
|
private class LossyConversionException: Exception {
|
|
70
78
|
override var reason: String {
|
|
71
79
|
"Unable to convert given string without losing some information"
|
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",
|
|
@@ -47,5 +47,5 @@
|
|
|
47
47
|
"peerDependencies": {
|
|
48
48
|
"expo": "*"
|
|
49
49
|
},
|
|
50
|
-
"gitHead": "
|
|
50
|
+
"gitHead": "1815e2eaad8c753588c7b1eb74420174a28e01f4"
|
|
51
51
|
}
|
package/src/Crypto.ts
CHANGED
|
@@ -112,7 +112,7 @@ function assertEncoding(encoding: CryptoEncoding): void {
|
|
|
112
112
|
* A digest is a short fixed-length value derived from some variable-length input. **Cryptographic digests** should exhibit _collision-resistance_,
|
|
113
113
|
* meaning that it's very difficult to generate multiple inputs that have equal digest values.
|
|
114
114
|
* You can specify the returned string format as one of `CryptoEncoding`. By default, the resolved value will be formatted as a `HEX` string.
|
|
115
|
-
* 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.
|
|
116
116
|
*
|
|
117
117
|
* @param algorithm The cryptographic hash function to use to transform a block of data into a fixed-size output.
|
|
118
118
|
* @param data The value that will be used to generate a digest.
|
|
@@ -177,3 +177,45 @@ export function getRandomValues<T extends IntBasedTypedArray | UintBasedTypedArr
|
|
|
177
177
|
export function randomUUID() {
|
|
178
178
|
return ExpoCrypto.randomUUID();
|
|
179
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
|
@@ -43,6 +43,9 @@ export default {
|
|
|
43
43
|
randomUUID() {
|
|
44
44
|
return getCrypto().randomUUID();
|
|
45
45
|
},
|
|
46
|
+
digestAsync(algorithm: AlgorithmIdentifier, data: ArrayBuffer): Promise<ArrayBuffer> {
|
|
47
|
+
return getCrypto().subtle.digest(algorithm, data);
|
|
48
|
+
},
|
|
46
49
|
};
|
|
47
50
|
|
|
48
51
|
function hexString(buffer: ArrayBuffer): string {
|