worker crypto

i wast trying to implement api to in cloudflare worker to generate deviceId but my function uses crypto library and i don't find the same function in cloudflare crypto:
using nodejs crypto lib.

function getDeviceI() {
let id = crypto.randomBytes(20).toString("binary");
let hmac = crypto.createHmac(
"sha1",
Buffer.from(
"E7309ECC0953C6FA60005B2765F99DBBC965C8E9",
"hex"
)
);
hmac.update(Buffer.from(Buffer.from("19", "hex") + id, "binary"));
return "19" + Buffer.from(id, "binary").toString("hex") + hmac.digest("hex");
}
correct deviceId:19CEB4EB9340402020E054720BEBAF280A8CEA373CC6E00629C9CEB1B3CB14FD5ABC0D4F5896DBAD4E
using nodejs crypto lib.

function getDeviceI() {
let id = crypto.randomBytes(20).toString("binary");
let hmac = crypto.createHmac(
"sha1",
Buffer.from(
"E7309ECC0953C6FA60005B2765F99DBBC965C8E9",
"hex"
)
);
hmac.update(Buffer.from(Buffer.from("19", "hex") + id, "binary"));
return "19" + Buffer.from(id, "binary").toString("hex") + hmac.digest("hex");
}
correct deviceId:19CEB4EB9340402020E054720BEBAF280A8CEA373CC6E00629C9CEB1B3CB14FD5ABC0D4F5896DBAD4E
can anyone help
4 Replies
Steved
Steved16mo ago
Web Crypto · Cloudflare Workers docs
The Web Crypto API provides a set of low-level functions for common cryptographic tasks. The Workers Runtime implements the full surface of this API, …
bᥲkᥙg᥆
bᥲkᥙg᥆OP16mo ago
async function getDeviceID() {
const randomBytes = new Uint8Array(20);
crypto.getRandomValues(randomBytes);
const id = Array.from(randomBytes, byte => byte.toString(16).padStart(2, '0')).join('');

const secretKeyHex = "E7309ECC0953C6FA60005B2765F99DBBC965C8E9";
const secretKeyBuffer = new TextEncoder().encode(secretKeyHex);

const hmacKey = await crypto.subtle.importKey("raw", secretKeyBuffer, { name: "HMAC", hash: { name: "SHA-1" } }, false, ["sign"]);

const data = new TextEncoder().encode("19" + id);
const hmac = await crypto.subtle.sign("HMAC", hmacKey, data);

const hmacHex = Array.from(new Uint8Array(hmac)).map(byte => byte.toString(16).padStart(2, '0')).join('');

const deviceIdentifier = "19" + id + hmacHex;

return deviceIdentifier;
}

export default {
async fetch(request, env, ctx) {
return new Response(await getDeviceID());
},
};
async function getDeviceID() {
const randomBytes = new Uint8Array(20);
crypto.getRandomValues(randomBytes);
const id = Array.from(randomBytes, byte => byte.toString(16).padStart(2, '0')).join('');

const secretKeyHex = "E7309ECC0953C6FA60005B2765F99DBBC965C8E9";
const secretKeyBuffer = new TextEncoder().encode(secretKeyHex);

const hmacKey = await crypto.subtle.importKey("raw", secretKeyBuffer, { name: "HMAC", hash: { name: "SHA-1" } }, false, ["sign"]);

const data = new TextEncoder().encode("19" + id);
const hmac = await crypto.subtle.sign("HMAC", hmacKey, data);

const hmacHex = Array.from(new Uint8Array(hmac)).map(byte => byte.toString(16).padStart(2, '0')).join('');

const deviceIdentifier = "19" + id + hmacHex;

return deviceIdentifier;
}

export default {
async fetch(request, env, ctx) {
return new Response(await getDeviceID());
},
};
not generating the correct deviceId
from base64 import b64decode, b64encode
from typing import Union
from hashlib import sha1
from hmac import new

PREFIX = bytes.fromhex("19")
SIG_KEY = bytes.fromhex("DFA5ED192DDA6E88A12FE12130DC6206B1251E44")
DEVICE_KEY = bytes.fromhex("E7309ECC0953C6FA60005B2765F99DBBC965C8E9")

def gen_deviceId(data: bytes = None) -> str:
if isinstance(data, str): data = bytes(data, 'utf-8')
identifier = PREFIX + (data or os.urandom(20))
mac = new(DEVICE_KEY, identifier, sha1)
return f"{identifier.hex()}{mac.hexdigest()}".upper()
from base64 import b64decode, b64encode
from typing import Union
from hashlib import sha1
from hmac import new

PREFIX = bytes.fromhex("19")
SIG_KEY = bytes.fromhex("DFA5ED192DDA6E88A12FE12130DC6206B1251E44")
DEVICE_KEY = bytes.fromhex("E7309ECC0953C6FA60005B2765F99DBBC965C8E9")

def gen_deviceId(data: bytes = None) -> str:
if isinstance(data, str): data = bytes(data, 'utf-8')
identifier = PREFIX + (data or os.urandom(20))
mac = new(DEVICE_KEY, identifier, sha1)
return f"{identifier.hex()}{mac.hexdigest()}".upper()
here is equivalant code in python
cg
cg16mo ago
Would crypto.randomUUID() suffice?
bᥲkᥙg᥆
bᥲkᥙg᥆OP16mo ago
nah i have to generate valid deviceId since it's being used in api
Want results from more Discord servers?
Add your server