Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support async getImage function #963

Open
everyx opened this issue Sep 3, 2023 · 0 comments
Open

support async getImage function #963

everyx opened this issue Sep 3, 2023 · 0 comments

Comments

@everyx
Copy link

everyx commented Sep 3, 2023

I am using a custom provider which signs URLs using HMAC-SHA256. If async getImage is not supported, the image URL may not be generated correctly.

If we aim to support #276 in the future, async getImage support will also be necessary.

import { subtle } from 'uncrypto';

async function sign(salt: string, target: string, secret: string, size = 32): Promise<string> {
  if (!salt || !secret) return '';

  const encoder = new TextEncoder();

  const secretEncoded = encoder.encode(secret);
  const key = await subtle.importKey(
    'raw',
    secretEncoded,
    { name: 'HMAC', hash: 'SHA-256' },
    false,
    ['sign'],
  );

  const saltEncoded = encoder.encode(salt);
  const targetEncoded = encoder.encode(target);
  const data = new Uint8Array([...saltEncoded, ...targetEncoded]);

  const signature = await subtle.sign('HMAC', key, data);

  return arrayBufferToBase64Url(signature.slice(0, size));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant