@azure/core-util
Advanced tools
Comparing version 1.5.1-alpha.20231019.1 to 1.6.0-alpha.20231023.3
@@ -17,2 +17,4 @@ // Copyright (c) Microsoft Corporation. | ||
return uint8ArrayToBase64Url(bytes); | ||
case "hex": | ||
return uint8ArrayToHexString(bytes); | ||
} | ||
@@ -34,2 +36,4 @@ } | ||
return base64UrlToUint8Array(value); | ||
case "hex": | ||
return hexStringToUint8Array(value); | ||
} | ||
@@ -41,6 +45,4 @@ } | ||
*/ | ||
export function uint8ArrayToBase64(uint8Array) { | ||
const decoder = new TextDecoder(); | ||
const dataString = decoder.decode(uint8Array); | ||
return btoa(dataString); | ||
export function uint8ArrayToBase64(bytes) { | ||
return btoa([...bytes].map((x) => String.fromCharCode(x)).join("")); | ||
} | ||
@@ -58,8 +60,15 @@ /** | ||
*/ | ||
export function uint8ArrayToUtf8String(uint8Array) { | ||
export function uint8ArrayToUtf8String(bytes) { | ||
const decoder = new TextDecoder(); | ||
const dataString = decoder.decode(uint8Array); | ||
const dataString = decoder.decode(bytes); | ||
return dataString; | ||
} | ||
/** | ||
* Decodes a Uint8Array into a hex string | ||
* @internal | ||
*/ | ||
export function uint8ArrayToHexString(bytes) { | ||
return [...bytes].map((x) => x.toString(16).padStart(2, "0")).join(""); | ||
} | ||
/** | ||
* Encodes a JavaScript string into a Uint8Array. | ||
@@ -86,2 +95,21 @@ * @internal | ||
} | ||
const hexDigits = new Set("0123456789abcdefABCDEF"); | ||
/** | ||
* Encodes a hex string into a Uint8Array | ||
* @internal | ||
*/ | ||
export function hexStringToUint8Array(value) { | ||
// If value has odd length, the last character will be ignored, consistent with NodeJS Buffer behavior | ||
const bytes = new Uint8Array(value.length / 2); | ||
for (let i = 0; i < value.length / 2; ++i) { | ||
const highNibble = value[2 * i]; | ||
const lowNibble = value[2 * i + 1]; | ||
if (!hexDigits.has(highNibble) || !hexDigits.has(lowNibble)) { | ||
// Replicate Node Buffer behavior by exiting early when we encounter an invalid byte | ||
return bytes.slice(0, i); | ||
} | ||
bytes[i] = parseInt(`${highNibble}${lowNibble}`, 16); | ||
} | ||
return bytes; | ||
} | ||
//# sourceMappingURL=bytesEncoding.browser.js.map |
@@ -10,10 +10,3 @@ // Copyright (c) Microsoft Corporation. | ||
export function uint8ArrayToString(bytes, format) { | ||
switch (format) { | ||
case "utf-8": | ||
return uint8ArrayToUtf8String(bytes); | ||
case "base64": | ||
return uint8ArrayToBase64(bytes); | ||
case "base64url": | ||
return uint8ArrayToBase64Url(bytes); | ||
} | ||
return Buffer.from(bytes).toString(format); | ||
} | ||
@@ -27,53 +20,4 @@ /** | ||
export function stringToUint8Array(value, format) { | ||
switch (format) { | ||
case "utf-8": | ||
return utf8StringToUint8Array(value); | ||
case "base64": | ||
return base64ToUint8Array(value); | ||
case "base64url": | ||
return base64UrlToUint8Array(value); | ||
} | ||
return Buffer.from(value, format); | ||
} | ||
/** | ||
* Decodes a Uint8Array into a Base64 string. | ||
* @internal | ||
*/ | ||
export function uint8ArrayToBase64(bytes) { | ||
return Buffer.from(bytes).toString("base64"); | ||
} | ||
/** | ||
* Decodes a Uint8Array into a Base64Url string. | ||
* @internal | ||
*/ | ||
export function uint8ArrayToBase64Url(bytes) { | ||
return Buffer.from(bytes).toString("base64url"); | ||
} | ||
/** | ||
* Decodes a Uint8Array into a javascript string. | ||
* @internal | ||
*/ | ||
export function uint8ArrayToUtf8String(bytes) { | ||
return Buffer.from(bytes).toString("utf-8"); | ||
} | ||
/** | ||
* Encodes a JavaScript string into a Uint8Array. | ||
* @internal | ||
*/ | ||
export function utf8StringToUint8Array(value) { | ||
return Buffer.from(value); | ||
} | ||
/** | ||
* Encodes a Base64 string into a Uint8Array. | ||
* @internal | ||
*/ | ||
export function base64ToUint8Array(value) { | ||
return Buffer.from(value, "base64"); | ||
} | ||
/** | ||
* Encodes a Base64Url string into a Uint8Array. | ||
* @internal | ||
*/ | ||
export function base64UrlToUint8Array(value) { | ||
return Buffer.from(value, "base64url"); | ||
} | ||
//# sourceMappingURL=bytesEncoding.js.map |
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT license. | ||
import { base64ToBytes, bufferToBase64 } from "./base64.browser"; | ||
import { bufferToHex } from "./hex"; | ||
import { utf8ToBytes } from "./utf8.browser"; | ||
import { stringToUint8Array, uint8ArrayToString } from "./bytesEncoding.browser"; | ||
let subtleCrypto; | ||
@@ -29,4 +27,4 @@ /** | ||
const crypto = getCrypto(); | ||
const keyBytes = base64ToBytes(key); | ||
const stringToSignBytes = utf8ToBytes(stringToSign); | ||
const keyBytes = stringToUint8Array(key, "base64"); | ||
const stringToSignBytes = stringToUint8Array(stringToSign, "utf-8"); | ||
const cryptoKey = await crypto.importKey("raw", keyBytes, { | ||
@@ -40,8 +38,3 @@ name: "HMAC", | ||
}, cryptoKey, stringToSignBytes); | ||
switch (encoding) { | ||
case "base64": | ||
return bufferToBase64(signature); | ||
case "hex": | ||
return bufferToHex(signature); | ||
} | ||
return uint8ArrayToString(new Uint8Array(signature), encoding); | ||
} | ||
@@ -54,11 +47,6 @@ /** | ||
export async function computeSha256Hash(content, encoding) { | ||
const contentBytes = utf8ToBytes(content); | ||
const contentBytes = stringToUint8Array(content, "utf-8"); | ||
const digest = await getCrypto().digest({ name: "SHA-256" }, contentBytes); | ||
switch (encoding) { | ||
case "base64": | ||
return bufferToBase64(digest); | ||
case "hex": | ||
return bufferToHex(digest); | ||
} | ||
return uint8ArrayToString(new Uint8Array(digest), encoding); | ||
} | ||
//# sourceMappingURL=sha256.browser.js.map |
@@ -324,10 +324,3 @@ 'use strict'; | ||
function uint8ArrayToString(bytes, format) { | ||
switch (format) { | ||
case "utf-8": | ||
return uint8ArrayToUtf8String(bytes); | ||
case "base64": | ||
return uint8ArrayToBase64(bytes); | ||
case "base64url": | ||
return uint8ArrayToBase64Url(bytes); | ||
} | ||
return Buffer.from(bytes).toString(format); | ||
} | ||
@@ -341,53 +334,4 @@ /** | ||
function stringToUint8Array(value, format) { | ||
switch (format) { | ||
case "utf-8": | ||
return utf8StringToUint8Array(value); | ||
case "base64": | ||
return base64ToUint8Array(value); | ||
case "base64url": | ||
return base64UrlToUint8Array(value); | ||
} | ||
return Buffer.from(value, format); | ||
} | ||
/** | ||
* Decodes a Uint8Array into a Base64 string. | ||
* @internal | ||
*/ | ||
function uint8ArrayToBase64(bytes) { | ||
return Buffer.from(bytes).toString("base64"); | ||
} | ||
/** | ||
* Decodes a Uint8Array into a Base64Url string. | ||
* @internal | ||
*/ | ||
function uint8ArrayToBase64Url(bytes) { | ||
return Buffer.from(bytes).toString("base64url"); | ||
} | ||
/** | ||
* Decodes a Uint8Array into a javascript string. | ||
* @internal | ||
*/ | ||
function uint8ArrayToUtf8String(bytes) { | ||
return Buffer.from(bytes).toString("utf-8"); | ||
} | ||
/** | ||
* Encodes a JavaScript string into a Uint8Array. | ||
* @internal | ||
*/ | ||
function utf8StringToUint8Array(value) { | ||
return Buffer.from(value); | ||
} | ||
/** | ||
* Encodes a Base64 string into a Uint8Array. | ||
* @internal | ||
*/ | ||
function base64ToUint8Array(value) { | ||
return Buffer.from(value, "base64"); | ||
} | ||
/** | ||
* Encodes a Base64Url string into a Uint8Array. | ||
* @internal | ||
*/ | ||
function base64UrlToUint8Array(value) { | ||
return Buffer.from(value, "base64url"); | ||
} | ||
@@ -394,0 +338,0 @@ exports.cancelablePromiseRace = cancelablePromiseRace; |
{ | ||
"name": "@azure/core-util", | ||
"version": "1.5.1-alpha.20231019.1", | ||
"version": "1.6.0-alpha.20231023.3", | ||
"description": "Core library for shared utility methods", | ||
@@ -5,0 +5,0 @@ "sdk-type": "client", |
@@ -67,3 +67,3 @@ import { AbortSignalLike } from '@azure/abort-controller'; | ||
/** The supported character encoding type */ | ||
export declare type EncodingType = "utf-8" | "base64" | "base64url"; | ||
export declare type EncodingType = "utf-8" | "base64" | "base64url" | "hex"; | ||
/** | ||
@@ -70,0 +70,0 @@ * Given what is thought to be an error object, return the message if possible. |
@@ -77,3 +77,3 @@ import { AbortSignalLike } from '@azure/abort-controller'; | ||
/** The supported character encoding type */ | ||
export declare type EncodingType = "utf-8" | "base64" | "base64url"; | ||
export declare type EncodingType = "utf-8" | "base64" | "base64url" | "hex"; | ||
@@ -80,0 +80,0 @@ /** |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
114532
39
1184