1- const encoder = new TextEncoder ( ) ;
1+ import { text_encoder } from '../../utils.js' ;
22
33/**
44 * SHA-256 hashing function adapted from https://bitwiseshiftleft.github.io/sjcl
@@ -102,7 +102,7 @@ export function sha256(data) {
102102 const bytes = new Uint8Array ( out . buffer ) ;
103103 reverse_endianness ( bytes ) ;
104104
105- return base64 ( bytes ) ;
105+ return btoa ( String . fromCharCode ( ... bytes ) ) ;
106106}
107107
108108/** The SHA-256 initialization vector */
@@ -160,7 +160,7 @@ function reverse_endianness(bytes) {
160160
161161/** @param {string } str */
162162function encode ( str ) {
163- const encoded = encoder . encode ( str ) ;
163+ const encoded = text_encoder . encode ( str ) ;
164164 const length = encoded . length * 8 ;
165165
166166 // result should be a multiple of 512 bits in length,
@@ -182,58 +182,3 @@ function encode(str) {
182182
183183 return words ;
184184}
185-
186- /*
187- Based on https://gist.github.com/enepomnyaschih/72c423f727d395eeaa09697058238727
188-
189- MIT License
190- Copyright (c) 2020 Egor Nepomnyaschih
191- Permission is hereby granted, free of charge, to any person obtaining a copy
192- of this software and associated documentation files (the "Software"), to deal
193- in the Software without restriction, including without limitation the rights
194- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
195- copies of the Software, and to permit persons to whom the Software is
196- furnished to do so, subject to the following conditions:
197- The above copyright notice and this permission notice shall be included in all
198- copies or substantial portions of the Software.
199- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
200- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
201- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
202- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
203- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
204- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
205- SOFTWARE.
206- */
207- const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/' . split ( '' ) ;
208-
209- /** @param {Uint8Array } bytes */
210- export function base64 ( bytes ) {
211- const l = bytes . length ;
212-
213- let result = '' ;
214- let i ;
215-
216- for ( i = 2 ; i < l ; i += 3 ) {
217- result += chars [ bytes [ i - 2 ] >> 2 ] ;
218- result += chars [ ( ( bytes [ i - 2 ] & 0x03 ) << 4 ) | ( bytes [ i - 1 ] >> 4 ) ] ;
219- result += chars [ ( ( bytes [ i - 1 ] & 0x0f ) << 2 ) | ( bytes [ i ] >> 6 ) ] ;
220- result += chars [ bytes [ i ] & 0x3f ] ;
221- }
222-
223- if ( i === l + 1 ) {
224- // 1 octet yet to write
225- result += chars [ bytes [ i - 2 ] >> 2 ] ;
226- result += chars [ ( bytes [ i - 2 ] & 0x03 ) << 4 ] ;
227- result += '==' ;
228- }
229-
230- if ( i === l ) {
231- // 2 octets yet to write
232- result += chars [ bytes [ i - 2 ] >> 2 ] ;
233- result += chars [ ( ( bytes [ i - 2 ] & 0x03 ) << 4 ) | ( bytes [ i - 1 ] >> 4 ) ] ;
234- result += chars [ ( bytes [ i - 1 ] & 0x0f ) << 2 ] ;
235- result += '=' ;
236- }
237-
238- return result ;
239- }
0 commit comments