expo-device-crypto 0.1.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/.eslintrc.js +5 -0
- package/README.md +132 -0
- package/android/build.gradle +47 -0
- package/android/src/main/AndroidManifest.xml +2 -0
- package/android/src/main/java/expo/modules/devicecrypto/DeviceCryptoModule.kt +385 -0
- package/build/DeviceCrypto.types.d.ts +101 -0
- package/build/DeviceCrypto.types.d.ts.map +1 -0
- package/build/DeviceCrypto.types.js +26 -0
- package/build/DeviceCrypto.types.js.map +1 -0
- package/build/DeviceCryptoModule.d.ts +86 -0
- package/build/DeviceCryptoModule.d.ts.map +1 -0
- package/build/DeviceCryptoModule.js +132 -0
- package/build/DeviceCryptoModule.js.map +1 -0
- package/build/index.d.ts +3 -0
- package/build/index.d.ts.map +1 -0
- package/build/index.js +3 -0
- package/build/index.js.map +1 -0
- package/expo-module.config.json +9 -0
- package/ios/DeviceCrypto.podspec +23 -0
- package/ios/DeviceCrypto.swift +404 -0
- package/package.json +55 -0
- package/src/DeviceCrypto.types.ts +106 -0
- package/src/DeviceCryptoModule.ts +171 -0
- package/src/index.ts +2 -0
- package/tsconfig.json +9 -0
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
import { NativeModule, requireNativeModule } from "expo";
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
AuthCheckResult,
|
|
5
|
+
GenerateKeyPairOptions,
|
|
6
|
+
GenerateKeyPairResult,
|
|
7
|
+
GetPublicKeyOptions,
|
|
8
|
+
AuthMethod,
|
|
9
|
+
SignOptions,
|
|
10
|
+
DecryptOptions,
|
|
11
|
+
VerifyOptions,
|
|
12
|
+
SigningAlgorithm,
|
|
13
|
+
EncryptOptions,
|
|
14
|
+
EncryptionAlgorithm,
|
|
15
|
+
} from "./DeviceCrypto.types";
|
|
16
|
+
|
|
17
|
+
function base64ToPem(base64: string, label = "DATA") {
|
|
18
|
+
const normalized = base64.replace(/\s+/g, "");
|
|
19
|
+
const wrapped = normalized.match(/.{1,64}/g)?.join("\n") ?? "";
|
|
20
|
+
|
|
21
|
+
return `-----BEGIN ${label}-----\n${wrapped}\n-----END ${label}-----\n`;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
declare class DeviceCryptoModule extends NativeModule {
|
|
25
|
+
isAuthCheckAvailable(): AuthCheckResult;
|
|
26
|
+
generateKeyPair(alias: string, o: any): GenerateKeyPairResult;
|
|
27
|
+
getPublicKey(alias: string): string | null;
|
|
28
|
+
removeKeyPair(alias: string): boolean;
|
|
29
|
+
aliases(): string[];
|
|
30
|
+
sign(alias: string, data: string, o: any): Promise<string | null>;
|
|
31
|
+
verify(
|
|
32
|
+
alias: string,
|
|
33
|
+
data: string,
|
|
34
|
+
signature: string,
|
|
35
|
+
o: any
|
|
36
|
+
): boolean | null;
|
|
37
|
+
encrypt(alias: string, data: string, o: any): Promise<string | null>;
|
|
38
|
+
decrypt(alias: string, data: string, o: any): Promise<string | null>;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const module = requireNativeModule<DeviceCryptoModule>("DeviceCrypto");
|
|
42
|
+
|
|
43
|
+
export default {
|
|
44
|
+
/**
|
|
45
|
+
* Checks if the biometric or passcode authentication is available.
|
|
46
|
+
* @returns The result of the operation.
|
|
47
|
+
*/
|
|
48
|
+
isAuthCheckAvailable: module.isAuthCheckAvailable,
|
|
49
|
+
/**
|
|
50
|
+
* Creates a new ECDSA P‑256 key pair for the given alias, if it doesn’t already exist.
|
|
51
|
+
* @param alias - The alias to use for the key pair.
|
|
52
|
+
* @returns The result of the operation.
|
|
53
|
+
*/
|
|
54
|
+
generateKeyPair: async (alias: string, options?: GenerateKeyPairOptions) => {
|
|
55
|
+
const o = {
|
|
56
|
+
algoType: options?.algorithmType,
|
|
57
|
+
reqAuth: options?.requireAuthentication ?? false,
|
|
58
|
+
authMethod: options?.authMethod ?? AuthMethod.PASSCODE_OR_BIOMETRIC,
|
|
59
|
+
};
|
|
60
|
+
return module.generateKeyPair(alias, o);
|
|
61
|
+
},
|
|
62
|
+
/**
|
|
63
|
+
* Returns the public key for the given alias.
|
|
64
|
+
* @param alias - The alias to use for the key pair.
|
|
65
|
+
* @param options - The options for the operation.
|
|
66
|
+
* @returns The public key.
|
|
67
|
+
*/
|
|
68
|
+
getPublicKey: (alias: string, options?: GetPublicKeyOptions) => {
|
|
69
|
+
const publicKey = module.getPublicKey(alias);
|
|
70
|
+
if (!publicKey) return null;
|
|
71
|
+
if (options?.format === "PEM") {
|
|
72
|
+
return base64ToPem(publicKey, "PUBLIC KEY");
|
|
73
|
+
}
|
|
74
|
+
return base64ToPem(publicKey, "PUBLIC KEY");
|
|
75
|
+
},
|
|
76
|
+
/**
|
|
77
|
+
* Removes the key pair for the given alias.
|
|
78
|
+
* @param alias - The alias to use for the key pair.
|
|
79
|
+
* @returns True if the key pair was removed, false otherwise.
|
|
80
|
+
*/
|
|
81
|
+
removeKeyPair: module.removeKeyPair,
|
|
82
|
+
/**
|
|
83
|
+
* Lists the aliases for the key pairs.
|
|
84
|
+
* @returns The aliases.
|
|
85
|
+
*/
|
|
86
|
+
aliases: module.aliases,
|
|
87
|
+
/**
|
|
88
|
+
* Signs the given data with the private key for the given alias.
|
|
89
|
+
* @param alias - The alias to use for the key pair.
|
|
90
|
+
* @param data - The data to sign.
|
|
91
|
+
* @param options - The options for the operation.
|
|
92
|
+
* @default {
|
|
93
|
+
* algoType: SigningAlgorithm.ECDSA_SHA256,
|
|
94
|
+
* promptTitle: "Unlock",
|
|
95
|
+
* promptSubtitle: "Enter your PIN to continue",
|
|
96
|
+
* authMethod: AuthMethod.PASSCODE_OR_BIOMETRIC
|
|
97
|
+
* }
|
|
98
|
+
* @returns The signature.
|
|
99
|
+
*/
|
|
100
|
+
sign: async (alias: string, data: string, options?: SignOptions) => {
|
|
101
|
+
const o = {
|
|
102
|
+
title: options?.promptTitle ?? "Unlock",
|
|
103
|
+
subtitle: options?.promptSubtitle ?? "Enter your PIN to continue",
|
|
104
|
+
authMethod: options?.authMethod ?? AuthMethod.PASSCODE_OR_BIOMETRIC,
|
|
105
|
+
algoType: options?.algorithmType ?? SigningAlgorithm.ECDSA_SECP256R1_SHA256,
|
|
106
|
+
};
|
|
107
|
+
return module.sign(alias, data, o);
|
|
108
|
+
},
|
|
109
|
+
/**
|
|
110
|
+
* Verifies the given data with the signature for the given alias.
|
|
111
|
+
* @param alias - The alias to use for the key pair.
|
|
112
|
+
* @param data - The data to verify.
|
|
113
|
+
* @param signature - The signature to verify.
|
|
114
|
+
* @param options - The options for the operation.
|
|
115
|
+
* @default {
|
|
116
|
+
* algoType: SigningAlgorithm.ECDSA_SHA256,
|
|
117
|
+
* }
|
|
118
|
+
* @returns True if the signature is valid, false otherwise.
|
|
119
|
+
*/
|
|
120
|
+
verify: async (
|
|
121
|
+
alias: string,
|
|
122
|
+
data: string,
|
|
123
|
+
signature: string,
|
|
124
|
+
options?: VerifyOptions
|
|
125
|
+
) => {
|
|
126
|
+
const o = {
|
|
127
|
+
algoType: options?.algorithmType ?? SigningAlgorithm.ECDSA_SECP256R1_SHA256,
|
|
128
|
+
};
|
|
129
|
+
return module.verify(alias, data, signature, o);
|
|
130
|
+
},
|
|
131
|
+
/**
|
|
132
|
+
* Signs the given data with the private key for the given alias.
|
|
133
|
+
* @param alias - The alias to use for the key pair.
|
|
134
|
+
* @param data - The data to sign in UTF-8 format.
|
|
135
|
+
* @param options - The options for the operation.
|
|
136
|
+
* @default {
|
|
137
|
+
* algoType: EncryptionAlgorithm.RSA_2048_PKCS1,
|
|
138
|
+
* }
|
|
139
|
+
* @returns The signature.
|
|
140
|
+
*/
|
|
141
|
+
encrypt: async (alias: string, data: string, options?: EncryptOptions) => {
|
|
142
|
+
const o = {
|
|
143
|
+
algoType: options?.algorithmType ?? EncryptionAlgorithm.RSA_2048_PKCS1,
|
|
144
|
+
};
|
|
145
|
+
return module.encrypt(alias, data, o);
|
|
146
|
+
},
|
|
147
|
+
/**
|
|
148
|
+
* Decrypts the given data with the private key for the given alias.
|
|
149
|
+
* @param alias - The alias to use for the key pair.
|
|
150
|
+
* @param data - The data to decrypt in Base64 format.
|
|
151
|
+
* @param options - The options for the operation.
|
|
152
|
+
* @default {
|
|
153
|
+
* algoType: EncryptionAlgorithm.RSA_2048_PKCS1,
|
|
154
|
+
* promptTitle: "Unlock",
|
|
155
|
+
* promptSubtitle: "Enter your PIN to continue",
|
|
156
|
+
* authMethod: AuthMethod.PASSCODE_OR_BIOMETRIC
|
|
157
|
+
* }
|
|
158
|
+
* @returns The decrypted data.
|
|
159
|
+
*/
|
|
160
|
+
decrypt: async (alias: string, data: string, options?: DecryptOptions) => {
|
|
161
|
+
const o = {
|
|
162
|
+
title: options?.promptTitle ?? "Unlock",
|
|
163
|
+
subtitle: options?.promptSubtitle ?? "Enter your PIN to continue",
|
|
164
|
+
authMethod: options?.authMethod ?? AuthMethod.PASSCODE_OR_BIOMETRIC,
|
|
165
|
+
algoType: options?.algorithmType ?? EncryptionAlgorithm.RSA_2048_PKCS1,
|
|
166
|
+
};
|
|
167
|
+
return module.decrypt(alias, data, o);
|
|
168
|
+
},
|
|
169
|
+
};
|
|
170
|
+
|
|
171
|
+
export type { GenerateKeyPairResult };
|
package/src/index.ts
ADDED
package/tsconfig.json
ADDED