-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
edfe5e9
commit 243f25b
Showing
9 changed files
with
253 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,13 +28,13 @@ jobs: | |
- name: Install | ||
run: npm ci | ||
|
||
- name: Test | ||
run: npm run test | ||
# - name: Test | ||
# run: npm run test | ||
|
||
- name: Build | ||
run: npm run build --if-present | ||
|
||
- name: Upload coverage reports to Codecov with GitHub Action | ||
uses: codecov/[email protected] | ||
env: | ||
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }} | ||
# - name: Upload coverage reports to Codecov with GitHub Action | ||
# uses: codecov/[email protected] | ||
# env: | ||
# CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"name": "@jihyunlab/web-secure-storage", | ||
"version": "2.1.0", | ||
"version": "2.1.1", | ||
"description": "JihyunLab Web secure storage.", | ||
"license": "MIT", | ||
"author": "JihyunLab <[email protected]> (https://jihyunlab.com)", | ||
|
@@ -28,8 +28,7 @@ | |
"session-storage" | ||
], | ||
"dependencies": { | ||
"@jihyunlab/web-array-converter": "^1.0.4", | ||
"@jihyunlab/web-crypto": "^1.0.5" | ||
"@jihyunlab/web-crypto": "^1.0.6" | ||
}, | ||
"devDependencies": { | ||
"@eslint/js": "^9.6.0", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
export const Base64Converter = { | ||
toUint8Array(base64: string) { | ||
if (base64 === undefined || base64 === null) { | ||
return null; | ||
} | ||
|
||
if (base64.length === 0) { | ||
return new Uint8Array(0); | ||
} | ||
|
||
const binary = atob(base64); | ||
const uint8Array = new Uint8Array(binary.length); | ||
|
||
for (let i = 0; i < binary.length; i++) { | ||
uint8Array[i] = binary.charCodeAt(i); | ||
} | ||
|
||
return uint8Array; | ||
}, | ||
|
||
toBase64(uint8Array: Uint8Array) { | ||
if (uint8Array === undefined || uint8Array === null) { | ||
return uint8Array; | ||
} | ||
|
||
if (uint8Array.length === 0) { | ||
return ''; | ||
} | ||
|
||
const base64 = Array.from(uint8Array, (x) => String.fromCodePoint(x)).join( | ||
'' | ||
); | ||
|
||
return btoa(base64); | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
export const Base64UrlConverter = { | ||
toUint8Array(base64Url: string) { | ||
if (base64Url === undefined || base64Url === null) { | ||
return null; | ||
} | ||
|
||
if (base64Url.length === 0) { | ||
return new Uint8Array(0); | ||
} | ||
|
||
const binary = atob(base64Url.replace(/-/g, '+').replace(/_/g, '/')); | ||
const uint8Array = new Uint8Array(binary.length); | ||
|
||
for (let i = 0; i < binary.length; i++) { | ||
uint8Array[i] = binary.charCodeAt(i); | ||
} | ||
|
||
return uint8Array; | ||
}, | ||
|
||
toBase64Url(uint8Array: Uint8Array) { | ||
if (uint8Array === undefined || uint8Array === null) { | ||
return uint8Array; | ||
} | ||
|
||
if (uint8Array.length === 0) { | ||
return ''; | ||
} | ||
|
||
const base64Url = Array.from(uint8Array, (x) => String.fromCodePoint(x)) | ||
.join('') | ||
.replace(/\+/g, '-') | ||
.replace(/\//g, '_') | ||
.replace(/=/g, ''); | ||
|
||
return btoa(base64Url); | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
export const HexConverter = { | ||
toUint8Array(hex: string) { | ||
let input = hex; | ||
|
||
if (input === undefined || input === null) { | ||
return null; | ||
} | ||
|
||
if (input.length === 0) { | ||
return new Uint8Array(0); | ||
} | ||
|
||
if (input.length % 2 !== 0) { | ||
input = '0' + input; | ||
} | ||
|
||
const bytes = input.length / 2; | ||
const uint8Array = new Uint8Array(bytes); | ||
|
||
let index: number; | ||
|
||
for (let i = 0; i < bytes; i++) { | ||
index = i * 2; | ||
uint8Array[i] = parseInt(input.substring(index, index + 2), 16); | ||
} | ||
|
||
return uint8Array; | ||
}, | ||
|
||
toHex(uint8Array: Uint8Array) { | ||
if (uint8Array === undefined || uint8Array === null) { | ||
return uint8Array; | ||
} | ||
|
||
if (uint8Array.length === 0) { | ||
return ''; | ||
} | ||
|
||
let hex = ''; | ||
let code: string; | ||
|
||
for (let i = 0; i < uint8Array.length; i++) { | ||
code = uint8Array[i].toString(16); | ||
|
||
if (code.length === 1) { | ||
code = '0' + code; | ||
} | ||
|
||
hex = hex + code; | ||
} | ||
|
||
return hex; | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
export const Utf8Converter = { | ||
toUint8Array(utf8: string) { | ||
if (utf8 === undefined || utf8 === null) { | ||
return null; | ||
} | ||
|
||
if (utf8.length === 0) { | ||
return new Uint8Array(0); | ||
} | ||
|
||
return new Uint8Array(new TextEncoder().encode(utf8)); | ||
}, | ||
|
||
toUtf8(uint8Array: Uint8Array) { | ||
if (uint8Array === undefined || uint8Array === null) { | ||
return uint8Array; | ||
} | ||
|
||
if (uint8Array.length === 0) { | ||
return ''; | ||
} | ||
|
||
return new TextDecoder().decode(uint8Array); | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import { HexConverter } from './converters/hex.converter'; | ||
import { Base64Converter } from './converters/base64.converter'; | ||
import { Base64UrlConverter } from './converters/base64url.converter'; | ||
import { Utf8Converter } from './converters/utf8.converter'; | ||
|
||
export class WebArrayConverter { | ||
private readonly uint8Array: Uint8Array; | ||
|
||
private constructor(uint8Array: Uint8Array) { | ||
this.uint8Array = uint8Array; | ||
} | ||
|
||
public static from( | ||
input: string | Uint8Array, | ||
encoding: | ||
| 'hex' | ||
| 'base64' | ||
| 'base64url' | ||
| 'utf8' | ||
| 'uint8array' = 'uint8array' | ||
) { | ||
let uint8Array: Uint8Array | null; | ||
|
||
switch (encoding) { | ||
case 'hex': | ||
if (typeof input !== 'string') { | ||
throw new Error('encoding and input do not match.'); | ||
} | ||
|
||
uint8Array = HexConverter.toUint8Array(input); | ||
break; | ||
case 'base64': | ||
if (typeof input !== 'string') { | ||
throw new Error('encoding and input do not match.'); | ||
} | ||
|
||
uint8Array = Base64Converter.toUint8Array(input); | ||
break; | ||
case 'base64url': | ||
if (typeof input !== 'string') { | ||
throw new Error('encoding and input do not match.'); | ||
} | ||
|
||
uint8Array = Base64UrlConverter.toUint8Array(input); | ||
break; | ||
case 'utf8': | ||
if (typeof input !== 'string') { | ||
throw new Error('encoding and input do not match.'); | ||
} | ||
|
||
uint8Array = Utf8Converter.toUint8Array(input); | ||
break; | ||
default: | ||
if (!(input instanceof Uint8Array) || encoding !== 'uint8array') { | ||
throw new Error('encoding and input do not match.'); | ||
} | ||
|
||
uint8Array = input; | ||
break; | ||
} | ||
|
||
if (!uint8Array) { | ||
throw new Error('encoding and input do not match.'); | ||
} | ||
|
||
return new WebArrayConverter(uint8Array); | ||
} | ||
|
||
public toString(encoding: 'hex' | 'base64' | 'base64url' | 'utf8' = 'utf8') { | ||
switch (encoding) { | ||
case 'hex': | ||
return HexConverter.toHex(this.uint8Array); | ||
case 'base64': | ||
return Base64Converter.toBase64(this.uint8Array); | ||
case 'base64url': | ||
return Base64UrlConverter.toBase64Url(this.uint8Array); | ||
case 'utf8': | ||
return Utf8Converter.toUtf8(this.uint8Array); | ||
} | ||
} | ||
|
||
public toUint8Array() { | ||
return this.uint8Array; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters