Skip to content

Commit

Permalink
moved to a separate files
Browse files Browse the repository at this point in the history
  • Loading branch information
ilyhalight committed Sep 5, 2023
1 parent 1d55602 commit cf1b701
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/utils/getSignature.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import crypto from "crypto";
import { yandexHmacKey } from "../config/config.js";

export default async function getSignature(body) {
// Create a key from the HMAC secret
const utf8Encoder = new TextEncoder("utf-8");
const key = await crypto.subtle.importKey(
"raw",
utf8Encoder.encode(yandexHmacKey),
{ name: "HMAC", hash: { name: "SHA-256" } },
false,
["sign", "verify"],
);
// Sign the body with the key
const signature = await crypto.subtle.sign("HMAC", key, body);
// Convert the signature to a hex string
return Array.from(new Uint8Array(signature), (x) =>
x.toString(16).padStart(2, "0"),
).join("");
}
11 changes: 11 additions & 0 deletions src/utils/getUUID.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import crypto from "crypto";

export default function getUUID(isLower) {
const uuid = ([1e7] + 1e3 + 4e3 + 8e3 + 1e11).replace(/[018]/g, (c) =>
(
c ^
(crypto.getRandomValues(new Uint8Array(1))[0] & (15 >> (c / 4)))
).toString(16),
);
return isLower ? uuid : uuid.toUpperCase();
}

0 comments on commit cf1b701

Please sign in to comment.