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

Buffer to parse a hex string preventing Expo usage #703

Open
trajano opened this issue Dec 27, 2023 · 1 comment
Open

Buffer to parse a hex string preventing Expo usage #703

trajano opened this issue Dec 27, 2023 · 1 comment

Comments

@trajano
Copy link

trajano commented Dec 27, 2023

There's a bunch of Buffer.from(newSecret, 'hex') code that is preventing this library from working in Expo specifically the one in

function hotpDigestToToken(hexDigest, digits) {
  const digest = Buffer.from(hexDigest, 'hex');
...
}

Which I can't find a way to override.

Also In my attempt, I used Uint8Array which I think may be a better choice than passing hex strings.

This is my workaround for now

const hexToUint8Array = (hexString) => {
  const length = hexString.length;
  const uint8Array = new Uint8Array(length / 2);

  for (let i = 0, j = 0; i < length; i += 2, j++) {
    uint8Array[j] = parseInt(hexString.substring(i, i + 2), 16);
  }
return uint8Array;

}
function hotpDigestToToken(hexDigest, digits) {
  const digest = hexToUint8Array(hexDigest);
  const offset = digest[digest.length - 1] & 0xf;
  const binary = (digest[offset] & 0x7f) << 24 | (digest[offset + 1] & 0xff) << 16 | (digest[offset + 2] & 0xff) << 8 | digest[offset + 3] & 0xff;
  const token = binary % Math.pow(10, digits);
  return padStart(String(token), digits, '0');
}
@trajano
Copy link
Author

trajano commented Dec 29, 2023

Also to avoid having to modify the code I just added a simplistic polyfill for this one scenario

if (!global.Buffer) {
  global.Buffer = {} as typeof global.Buffer;

  global.Buffer.from = (...args: any[]): any => {
    const hexString = args[0] as string;
    const length = hexString.length;
    const uint8Array = new Uint8Array(length / 2);

    for (let i = 0, j = 0; i < length; i += 2, j++) {
      uint8Array[j] = parseInt(hexString.substring(i, i + 2), 16);
    }
    return uint8Array;
  };
}

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