Skip to content

Commit

Permalink
$mol_base64: safe -> url
Browse files Browse the repository at this point in the history
  • Loading branch information
jin committed Nov 22, 2023
1 parent e358d5e commit 62ad2cb
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 18 deletions.
4 changes: 2 additions & 2 deletions base64/safe/safe.ts → base64/url/url.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
namespace $ {

export function $mol_base64_encode_safe( buffer: Uint8Array ) {
export function $mol_base64_url_encode( buffer: Uint8Array ) {
return $mol_base64_encode( buffer ).replace( /\+/g, '-' ).replace( /\//g, '_' ).replace( /=/g, '' )
}

export function $mol_base64_safe_decode( str: string ) {
export function $mol_base64_url_decode( str: string ) {
return $mol_base64_decode( str.replace( /-/g, '+' ).replace( /_/g, '/' ) )
}

Expand Down
20 changes: 10 additions & 10 deletions crypto/auditor/auditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ namespace $ {

static async from( serial: string | Uint8Array ) {
if( typeof serial !== 'string' ) {
serial = $mol_base64_encode_safe( serial.subarray( 0, 32 ) )
+ $mol_base64_encode_safe( serial.subarray( 32, 64 ) )
serial = $mol_base64_url_encode( serial.subarray( 0, 32 ) )
+ $mol_base64_url_encode( serial.subarray( 32, 64 ) )
}
return new this(
await $mol_crypto_native.subtle.importKey(
Expand Down Expand Up @@ -79,8 +79,8 @@ namespace $ {
async toArray(): Promise< Uint8Array > {
const { x, y, d } = await $mol_crypto_native.subtle.exportKey( 'jwk', this.native )
return new Uint8Array([
... $mol_base64_safe_decode( x! ),
... $mol_base64_safe_decode( y! ),
... $mol_base64_url_decode( x! ),
... $mol_base64_url_decode( y! ),
])
}

Expand Down Expand Up @@ -110,9 +110,9 @@ namespace $ {

static async from( serial: string | Uint8Array ) {
if( typeof serial !== 'string' ) {
serial = $mol_base64_encode_safe( serial.subarray( 0, 32 ) )
+ $mol_base64_encode_safe( serial.subarray( 32, 64 ) )
+ $mol_base64_encode_safe( serial.subarray( 64 ) )
serial = $mol_base64_url_encode( serial.subarray( 0, 32 ) )
+ $mol_base64_url_encode( serial.subarray( 32, 64 ) )
+ $mol_base64_url_encode( serial.subarray( 64 ) )
}
return new this(
await $mol_crypto_native.subtle.importKey(
Expand Down Expand Up @@ -143,9 +143,9 @@ namespace $ {
async toArray(): Promise< Uint8Array > {
const { x, y, d } = await $mol_crypto_native.subtle.exportKey( 'jwk', this.native )
return new Uint8Array([
... $mol_base64_safe_decode( x! ),
... $mol_base64_safe_decode( y! ),
... $mol_base64_safe_decode( d! ),
... $mol_base64_url_decode( x! ),
... $mol_base64_url_decode( y! ),
... $mol_base64_url_decode( d! ),
])
}

Expand Down
12 changes: 6 additions & 6 deletions crypto/key/key.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ namespace $ {

if( typeof serial === 'string' ) {
serial = new Uint8Array([
... $mol_base64_safe_decode( serial.slice( 0, 43 ) ),
... $mol_base64_safe_decode( serial.slice( 43, 86 ) ),
... $mol_base64_safe_decode( serial.slice( 86, 129 ) ),
... $mol_base64_url_decode( serial.slice( 0, 43 ) ),
... $mol_base64_url_decode( serial.slice( 43, 86 ) ),
... $mol_base64_url_decode( serial.slice( 86, 129 ) ),
])
}

Expand All @@ -28,9 +28,9 @@ namespace $ {
@ $mol_memo.method
toString() {
const arr = this.asArray()
return $mol_base64_encode_safe( arr.subarray( 0, 32 ) )
+ $mol_base64_encode_safe( arr.subarray( 32, 64 ) )
+ $mol_base64_encode_safe( arr.subarray( 64 ) )
return $mol_base64_url_encode( arr.subarray( 0, 32 ) )
+ $mol_base64_url_encode( arr.subarray( 32, 64 ) )
+ $mol_base64_url_encode( arr.subarray( 64 ) )
}

}
Expand Down

0 comments on commit 62ad2cb

Please sign in to comment.