1
1
import { Buffer } from 'buffer' ;
2
+ import { createHash } from 'crypto' ;
2
3
import { decodeObj , encodeAddress } from 'algosdk' ;
3
4
import axios from 'axios' ;
4
5
@@ -28,6 +29,10 @@ export function utf8ToBase64(str: string) {
28
29
return Buffer . from ( str ) . toString ( 'base64' ) ;
29
30
}
30
31
32
+ export function utf8ToBytes ( str : string ) : Uint8Array {
33
+ return new TextEncoder ( ) . encode ( str ) ;
34
+ }
35
+
31
36
export const hexToB64 = hexToBase64 ;
32
37
export function hexToBase64 ( str : string ) : string {
33
38
return Buffer . from ( str . replace ( / ^ 0 x / , '' ) , 'hex' ) . toString ( 'base64' ) ;
@@ -48,14 +53,21 @@ export function bytesToBase64(buffer: Uint8Array): string {
48
53
return Buffer . from ( buffer ) . toString ( 'base64' ) ;
49
54
}
50
55
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 ( ) ;
53
58
}
54
59
55
60
export function bytesToUtf8 ( bytes : Uint8Array ) : string {
56
61
return new TextDecoder ( ) . decode ( bytes ) ;
57
62
}
58
63
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
+ }
59
71
/**
60
72
* Convert object values to strings
61
73
* ==================================================
0 commit comments