Skip to content

Buffer to parse a hex string preventing Expo usage  #703

@trajano

Description

@trajano

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');
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions