crypto-js#WordArray TypeScript Examples

The following examples show how to use crypto-js#WordArray. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: crypto.ts    From magic-js with MIT License 5 votes vote down vote up
function base64URLEncodeFromByteArray(arg: WordArray | ArrayBuffer): string {
  const makeURLSafe = (base64: string) => {
    return base64.replace(/\+/g, '-').replace(/\//g, '_').replace(/=/g, '');
  };

  if (arg instanceof ArrayBuffer) {
    const bytes = new Uint8Array(arg);
    const utf8Binary = Array.from(bytes)
      .map((value) => String.fromCharCode(value))
      .join('');

    const base64 = btoa(utf8Binary);
    return makeURLSafe(base64);
  }

  return makeURLSafe(Base64.stringify(arg));
}