Skip to content

Commit 9319c68

Browse files
committed
added encoding methods
1 parent 1b6f475 commit 9319c68

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

src/helpers/encoding.ts

+14-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Buffer } from 'buffer';
2+
import { createHash } from 'crypto';
23
import { decodeObj, encodeAddress } from 'algosdk';
34
import axios from 'axios';
45

@@ -28,6 +29,10 @@ export function utf8ToBase64(str: string) {
2829
return Buffer.from(str).toString('base64');
2930
}
3031

32+
export function utf8ToBytes(str: string): Uint8Array {
33+
return new TextEncoder().encode(str);
34+
}
35+
3136
export const hexToB64 = hexToBase64;
3237
export function hexToBase64(str: string): string {
3338
return Buffer.from(str.replace(/^0x/, ''), 'hex').toString('base64');
@@ -48,14 +53,21 @@ export function bytesToBase64(buffer: Uint8Array): string {
4853
return Buffer.from(buffer).toString('base64');
4954
}
5055

51-
export function utf8ToBytes(str: string): Uint8Array {
52-
return new TextEncoder().encode(str);
56+
export function bytesToHex(bytes: Uint8Array): string {
57+
return Buffer.from(bytes).toString('hex').toUpperCase();
5358
}
5459

5560
export function bytesToUtf8(bytes: Uint8Array): string {
5661
return new TextDecoder().decode(bytes);
5762
}
5863

64+
export async function sha256(bytes: Uint8Array): Promise<Uint8Array> {
65+
return new Uint8Array(await crypto.subtle.digest('SHA-256', bytes));
66+
}
67+
68+
export async function sha512(bytes: Uint8Array): Promise<Uint8Array> {
69+
return new Uint8Array(await crypto.subtle.digest('SHA-512', bytes));
70+
}
5971
/**
6072
* Convert object values to strings
6173
* ==================================================

0 commit comments

Comments
 (0)