Skip to content

Commit 0934dfb

Browse files
authored
[core] Add SHA256 functions for React-Native (#30080)
### Packages impacted by this PR - @azure/core-rest-pipeline - @typespec/ts-http-runtime ### Issues associated with this PR - #30065 ### Describe the problem that is addressed by this PR Adds web crypto APIs for React-Native calls. Requires the consumer to polyfill the Web Crypto APIs in order for these modules to work. ### What are the possible designs available to address the problem? If there are more than one possible design, why was the one in this PR chosen? ### Are there test cases added in this PR? _(If not, why?)_ ### Provide a list of related PRs _(if any)_ ### Command used to generate this PR:**_(Applicable only to SDK release request PRs)_ ### Checklists - [ ] Added impacted package name to the issue description - [ ] Does this PR needs any fixes in the SDK Generator?** _(If so, create an Issue in the [Autorest/typescript](https://github.com/Azure/autorest.typescript) repository and link it here)_ - [ ] Added a changelog (if necessary)
1 parent 9488fe4 commit 0934dfb

21 files changed

+540
-489
lines changed

sdk/core/core-util/.tshy/browser.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
"../src/**/*.tsx"
77
],
88
"exclude": [
9+
".././src/bytesEncoding-react-native.mts",
10+
".././src/sha256-react-native.mts",
911
".././src/uuidUtils-react-native.mts"
1012
],
1113
"compilerOptions": {

sdk/core/core-util/.tshy/commonjs.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
"../src/bytesEncoding-browser.mts",
1111
"../src/sha256-browser.mts",
1212
"../src/uuidUtils-browser.mts",
13+
"../src/bytesEncoding-react-native.mts",
14+
"../src/sha256-react-native.mts",
1315
"../src/uuidUtils-react-native.mts"
1416
],
1517
"compilerOptions": {

sdk/core/core-util/.tshy/esm.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
".././src/bytesEncoding-browser.mts",
1010
".././src/sha256-browser.mts",
1111
".././src/uuidUtils-browser.mts",
12+
".././src/bytesEncoding-react-native.mts",
13+
".././src/sha256-react-native.mts",
1214
".././src/uuidUtils-react-native.mts"
1315
],
1416
"compilerOptions": {
Lines changed: 1 addition & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -1,131 +1,4 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT license.
33

4-
declare global {
5-
// stub these out for the browser
6-
function btoa(input: string): string;
7-
function atob(input: string): string;
8-
}
9-
10-
/** The supported character encoding type */
11-
export type EncodingType = "utf-8" | "base64" | "base64url" | "hex";
12-
13-
/**
14-
* The helper that transforms bytes with specific character encoding into string
15-
* @param bytes - the uint8array bytes
16-
* @param format - the format we use to encode the byte
17-
* @returns a string of the encoded string
18-
*/
19-
export function uint8ArrayToString(bytes: Uint8Array, format: EncodingType): string {
20-
switch (format) {
21-
case "utf-8":
22-
return uint8ArrayToUtf8String(bytes);
23-
case "base64":
24-
return uint8ArrayToBase64(bytes);
25-
case "base64url":
26-
return uint8ArrayToBase64Url(bytes);
27-
case "hex":
28-
return uint8ArrayToHexString(bytes);
29-
}
30-
}
31-
32-
/**
33-
* The helper that transforms string to specific character encoded bytes array.
34-
* @param value - the string to be converted
35-
* @param format - the format we use to decode the value
36-
* @returns a uint8array
37-
*/
38-
export function stringToUint8Array(value: string, format: EncodingType): Uint8Array {
39-
switch (format) {
40-
case "utf-8":
41-
return utf8StringToUint8Array(value);
42-
case "base64":
43-
return base64ToUint8Array(value);
44-
case "base64url":
45-
return base64UrlToUint8Array(value);
46-
case "hex":
47-
return hexStringToUint8Array(value);
48-
}
49-
}
50-
51-
/**
52-
* Decodes a Uint8Array into a Base64 string.
53-
* @internal
54-
*/
55-
export function uint8ArrayToBase64(bytes: Uint8Array): string {
56-
return btoa([...bytes].map((x) => String.fromCharCode(x)).join(""));
57-
}
58-
59-
/**
60-
* Decodes a Uint8Array into a Base64Url string.
61-
* @internal
62-
*/
63-
export function uint8ArrayToBase64Url(bytes: Uint8Array): string {
64-
return uint8ArrayToBase64(bytes).replace(/\+/g, "-").replace(/\//g, "_").replace(/=/g, "");
65-
}
66-
67-
/**
68-
* Decodes a Uint8Array into a javascript string.
69-
* @internal
70-
*/
71-
export function uint8ArrayToUtf8String(bytes: Uint8Array): string {
72-
const decoder = new TextDecoder();
73-
const dataString = decoder.decode(bytes);
74-
return dataString;
75-
}
76-
77-
/**
78-
* Decodes a Uint8Array into a hex string
79-
* @internal
80-
*/
81-
export function uint8ArrayToHexString(bytes: Uint8Array): string {
82-
return [...bytes].map((x) => x.toString(16).padStart(2, "0")).join("");
83-
}
84-
85-
/**
86-
* Encodes a JavaScript string into a Uint8Array.
87-
* @internal
88-
*/
89-
export function utf8StringToUint8Array(value: string): Uint8Array {
90-
return new TextEncoder().encode(value);
91-
}
92-
93-
/**
94-
* Encodes a Base64 string into a Uint8Array.
95-
* @internal
96-
*/
97-
export function base64ToUint8Array(value: string): Uint8Array {
98-
return new Uint8Array([...atob(value)].map((x) => x.charCodeAt(0)));
99-
}
100-
101-
/**
102-
* Encodes a Base64Url string into a Uint8Array.
103-
* @internal
104-
*/
105-
export function base64UrlToUint8Array(value: string): Uint8Array {
106-
const base64String = value.replace(/-/g, "+").replace(/_/g, "/");
107-
return base64ToUint8Array(base64String);
108-
}
109-
110-
const hexDigits = new Set("0123456789abcdefABCDEF");
111-
112-
/**
113-
* Encodes a hex string into a Uint8Array
114-
* @internal
115-
*/
116-
export function hexStringToUint8Array(value: string): Uint8Array {
117-
// If value has odd length, the last character will be ignored, consistent with NodeJS Buffer behavior
118-
const bytes = new Uint8Array(value.length / 2);
119-
for (let i = 0; i < value.length / 2; ++i) {
120-
const highNibble = value[2 * i];
121-
const lowNibble = value[2 * i + 1];
122-
if (!hexDigits.has(highNibble) || !hexDigits.has(lowNibble)) {
123-
// Replicate Node Buffer behavior by exiting early when we encounter an invalid byte
124-
return bytes.slice(0, i);
125-
}
126-
127-
bytes[i] = parseInt(`${highNibble}${lowNibble}`, 16);
128-
}
129-
130-
return bytes;
131-
}
4+
export * from "./bytesEncoding.common.js";
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT license.
3+
4+
export * from "./bytesEncoding.common.js";
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT license.
3+
4+
declare global {
5+
// stub these out for the browser
6+
function btoa(input: string): string;
7+
function atob(input: string): string;
8+
}
9+
10+
/** The supported character encoding type */
11+
export type EncodingType = "utf-8" | "base64" | "base64url" | "hex";
12+
13+
/**
14+
* The helper that transforms bytes with specific character encoding into string
15+
* @param bytes - the uint8array bytes
16+
* @param format - the format we use to encode the byte
17+
* @returns a string of the encoded string
18+
*/
19+
export function uint8ArrayToString(bytes: Uint8Array, format: EncodingType): string {
20+
switch (format) {
21+
case "utf-8":
22+
return uint8ArrayToUtf8String(bytes);
23+
case "base64":
24+
return uint8ArrayToBase64(bytes);
25+
case "base64url":
26+
return uint8ArrayToBase64Url(bytes);
27+
case "hex":
28+
return uint8ArrayToHexString(bytes);
29+
}
30+
}
31+
32+
/**
33+
* The helper that transforms string to specific character encoded bytes array.
34+
* @param value - the string to be converted
35+
* @param format - the format we use to decode the value
36+
* @returns a uint8array
37+
*/
38+
export function stringToUint8Array(value: string, format: EncodingType): Uint8Array {
39+
switch (format) {
40+
case "utf-8":
41+
return utf8StringToUint8Array(value);
42+
case "base64":
43+
return base64ToUint8Array(value);
44+
case "base64url":
45+
return base64UrlToUint8Array(value);
46+
case "hex":
47+
return hexStringToUint8Array(value);
48+
}
49+
}
50+
51+
/**
52+
* Decodes a Uint8Array into a Base64 string.
53+
* @internal
54+
*/
55+
export function uint8ArrayToBase64(bytes: Uint8Array): string {
56+
return btoa([...bytes].map((x) => String.fromCharCode(x)).join(""));
57+
}
58+
59+
/**
60+
* Decodes a Uint8Array into a Base64Url string.
61+
* @internal
62+
*/
63+
export function uint8ArrayToBase64Url(bytes: Uint8Array): string {
64+
return uint8ArrayToBase64(bytes).replace(/\+/g, "-").replace(/\//g, "_").replace(/=/g, "");
65+
}
66+
67+
/**
68+
* Decodes a Uint8Array into a javascript string.
69+
* @internal
70+
*/
71+
export function uint8ArrayToUtf8String(bytes: Uint8Array): string {
72+
const decoder = new TextDecoder();
73+
const dataString = decoder.decode(bytes);
74+
return dataString;
75+
}
76+
77+
/**
78+
* Decodes a Uint8Array into a hex string
79+
* @internal
80+
*/
81+
export function uint8ArrayToHexString(bytes: Uint8Array): string {
82+
return [...bytes].map((x) => x.toString(16).padStart(2, "0")).join("");
83+
}
84+
85+
/**
86+
* Encodes a JavaScript string into a Uint8Array.
87+
* @internal
88+
*/
89+
export function utf8StringToUint8Array(value: string): Uint8Array {
90+
return new TextEncoder().encode(value);
91+
}
92+
93+
/**
94+
* Encodes a Base64 string into a Uint8Array.
95+
* @internal
96+
*/
97+
export function base64ToUint8Array(value: string): Uint8Array {
98+
return new Uint8Array([...atob(value)].map((x) => x.charCodeAt(0)));
99+
}
100+
101+
/**
102+
* Encodes a Base64Url string into a Uint8Array.
103+
* @internal
104+
*/
105+
export function base64UrlToUint8Array(value: string): Uint8Array {
106+
const base64String = value.replace(/-/g, "+").replace(/_/g, "/");
107+
return base64ToUint8Array(base64String);
108+
}
109+
110+
const hexDigits = new Set("0123456789abcdefABCDEF");
111+
112+
/**
113+
* Encodes a hex string into a Uint8Array
114+
* @internal
115+
*/
116+
export function hexStringToUint8Array(value: string): Uint8Array {
117+
// If value has odd length, the last character will be ignored, consistent with NodeJS Buffer behavior
118+
const bytes = new Uint8Array(value.length / 2);
119+
for (let i = 0; i < value.length / 2; ++i) {
120+
const highNibble = value[2 * i];
121+
const lowNibble = value[2 * i + 1];
122+
if (!hexDigits.has(highNibble) || !hexDigits.has(lowNibble)) {
123+
// Replicate Node Buffer behavior by exiting early when we encounter an invalid byte
124+
return bytes.slice(0, i);
125+
}
126+
127+
bytes[i] = parseInt(`${highNibble}${lowNibble}`, 16);
128+
}
129+
130+
return bytes;
131+
}

0 commit comments

Comments
 (0)