1- /*
2- The MIT License (MIT)
3-
4- Copyright (c) 2021 Emanuele Bellocchia
1+ // The MIT License (MIT)
52
6- Permission is hereby granted, free of charge, to any person obtaining a copy
7- of this software and associated documentation files (the "Software"), to deal
8- in the Software without restriction, including without limitation the rights
9- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
10- of the Software, and to permit persons to whom the Software is furnished to do so,
11- subject to the following conditions:
3+ // Copyright (c) 2021 Emanuele Bellocchia
4+ // Copyright (c) 2023 Mohsen Haydari (MRTNETWORK)
125
13- The above copyright notice and this permission notice shall be included in all
14- copies or substantial portions of the Software.
6+ // Permission is hereby granted, free of charge, to any person obtaining a copy
7+ // of this software and associated documentation files (the "Software"), to deal
8+ // in the Software without restriction, including without limitation the rights
9+ // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
10+ // of the Software, and to permit persons to whom the Software is furnished to do so,
11+ // subject to the following conditions:
1512
16- THE SOFTWARE IS PROVIDED "AS IS," WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
17- INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
18- PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
19- FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20- ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21-
22- Note: This code has been adapted from its original Python version to Dart.
23- */
13+ // The above copyright notice and this permission notice shall be included in all
14+ // copies or substantial portions of the Software.
2415
25- /*
26- The 3-Clause BSD License
27-
28- Copyright (c) 2023 Mohsen Haydari (MRTNETWORK)
29- All rights reserved.
30-
31- Redistribution and use in source and binary forms, with or without
32- modification, are permitted provided that the following conditions are met:
33-
34- 1. Redistributions of source code must retain the above copyright notice, this
35- list of conditions, and the following disclaimer.
36- 2. Redistributions in binary form must reproduce the above copyright notice, this
37- list of conditions, and the following disclaimer in the documentation and/or
38- other materials provided with the distribution.
39- 3. Neither the name of the [organization] nor the names of its contributors may be
40- used to endorse or promote products derived from this software without
41- specific prior written permission.
42-
43- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
44- ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
45- WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
46- IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
47- INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
48- BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
49- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
50- LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
51- OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
52- OF THE POSSIBILITY OF SUCH DAMAGE.
53- */
16+ // THE SOFTWARE IS PROVIDED "AS IS," WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
17+ // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
18+ // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
19+ // FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20+ // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21+ // Note: This code has been adapted from its original Python version to Dart.
5422
5523import 'package:blockchain_utils/helper/helper.dart' ;
56- import 'package:blockchain_utils/utils/utils.dart' ;
24+
5725import 'package:blockchain_utils/exception/exceptions.dart' ;
26+ import 'package:blockchain_utils/utils/binary/binary_operation.dart' ;
27+ import 'package:blockchain_utils/utils/string/string.dart' ;
5828
5929/// Constants and data structures used for Base32 encoding and decoding.
6030class _Base32Const {
@@ -63,12 +33,6 @@ class _Base32Const {
6333
6434 /// Padding character used for Base32 encoding.
6535 static const String paddingChar = '=' ;
66-
67- /// Internal data structures used for Base32 encoding and decoding.
68- // static final Map<String, dynamic> _b32tab2 = {};
69-
70- /// Reverse mapping for Base32 decoding.
71- static final Map <String , Map <String , int >> _b32rev = {}; // (private)
7236}
7337
7438class _Base32Utils {
@@ -84,7 +48,10 @@ class _Base32Utils {
8448
8549 /// Translate the standard Base32 alphabet to a custom one.
8650 static String translateAlphabet (
87- String data, String fromAlphabet, String toAlphabet) {
51+ String data,
52+ String fromAlphabet,
53+ String toAlphabet,
54+ ) {
8855 final translationMap = Map <String , String >.fromIterable (
8956 fromAlphabet.codeUnits,
9057 key: (unit) => String .fromCharCode (unit),
@@ -94,22 +61,20 @@ class _Base32Utils {
9461 },
9562 );
9663
97- final translatedData = data.split ('' ).map ((char) {
98- return translationMap[char] ?? char;
99- }).join ('' );
64+ final translatedData = data
65+ .split ('' )
66+ .map ((char) {
67+ return translationMap[char] ?? char;
68+ })
69+ .join ('' );
10070
10171 return translatedData;
10272 }
10373
104- static List <int > _b32decode (
105- String alphabet,
106- String base32,
107- ) {
108- if (! _Base32Const ._b32rev.containsKey (alphabet)) {
109- _Base32Const ._b32rev[alphabet] = {};
110- for (var i = 0 ; i < alphabet.length; i++ ) {
111- _Base32Const ._b32rev[alphabet]! [alphabet[i]] = i;
112- }
74+ static List <int > _b32decode (String alphabet, String base32) {
75+ Map <String , int > rev = {};
76+ for (var i = 0 ; i < alphabet.length; i++ ) {
77+ rev[alphabet[i]] = i;
11378 }
11479 int shift = 8 ;
11580 int carry = 0 ;
@@ -118,14 +83,14 @@ class _Base32Utils {
11883 if (char == '=' ) {
11984 return ;
12085 }
121- final symbol = (_Base32Const ._b32rev[alphabet] ! [ char] ?? 0 ) & mask8;
86+ final symbol = (rev[ char] ?? 0 ) & BinaryOps . mask8;
12287 shift -= 5 ;
12388 if (shift > 0 ) {
124- carry | = (symbol << shift) & mask8;
89+ carry | = (symbol << shift) & BinaryOps . mask8;
12590 } else if (shift < 0 ) {
12691 decoded.add (carry | (symbol >> - shift));
12792 shift += 8 ;
128- carry = (symbol << shift) & mask8;
93+ carry = (symbol << shift) & BinaryOps . mask8;
12994 } else {
13095 decoded.add (carry | symbol);
13196 shift = 8 ;
@@ -144,8 +109,7 @@ class _Base32Utils {
144109 static List <int > _b32encode (String alphabet, List <int > s) {
145110 final leftover = s.length % 5 ;
146111 if (leftover != 0 ) {
147- final padding = List .filled (5 - leftover, 0 );
148- s = List <int >.from ([...s, ...padding]);
112+ s = [...s, ...List .filled (5 - leftover, 0 )];
149113 }
150114 int shift = 3 ;
151115 int carry = 0 ;
@@ -176,7 +140,7 @@ class _Base32Utils {
176140 } else if (leftover == 4 ) {
177141 encoded.setAll (encoded.length - 1 , [0x3d ]);
178142 }
179- return List < int >. from ( encoded) ;
143+ return encoded;
180144 }
181145}
182146
@@ -192,17 +156,21 @@ class Base32Decoder {
192156 /// If a custom alphabet is specified, translate the input data to the standard Base32 alphabet.
193157 if (customAlphabet != null ) {
194158 data = _Base32Utils .translateAlphabet (
195- data, customAlphabet, _Base32Const .alphabet);
159+ data,
160+ customAlphabet,
161+ _Base32Const .alphabet,
162+ );
196163 }
197164
198165 /// Decode the Base32 string and obtain the decoded bytes.
199- final decodedBytes = _Base32Utils ._b32decode (_Base32Const .alphabet, data);
200-
201- /// Return the decoded bytes as a List.
202- return List <int >.from (decodedBytes);
203- } catch (ex) {
166+ return _Base32Utils ._b32decode (_Base32Const .alphabet, data);
167+ } catch (_) {
204168 /// Handle exceptions by throwing an error for invalid Base32 strings.
205- throw const ArgumentException ('Invalid Base32 string' );
169+ throw ArgumentException .invalidOperationArguments (
170+ "decode" ,
171+ name: "data" ,
172+ reason: 'Invalid Base32 string' ,
173+ );
206174 }
207175 }
208176}
@@ -213,13 +181,17 @@ class Base32Encoder {
213181 /// Optionally, you can specify a custom alphabet for encoding.
214182 static String encode (String data, [String ? customAlphabet]) {
215183 /// Convert the input string to UTF-8 encoded bytes and then encode it in Base32.
216- String encoded = StringUtils .decode (_Base32Utils ._b32encode (
217- _Base32Const .alphabet, StringUtils .encode (data)));
184+ String encoded = StringUtils .decode (
185+ _Base32Utils ._b32encode (_Base32Const .alphabet, StringUtils .encode (data)),
186+ );
218187
219188 /// If a custom alphabet is specified, translate the encoded string to the custom alphabet.
220189 if (customAlphabet != null ) {
221190 encoded = _Base32Utils .translateAlphabet (
222- encoded, _Base32Const .alphabet, customAlphabet);
191+ encoded,
192+ _Base32Const .alphabet,
193+ customAlphabet,
194+ );
223195 }
224196
225197 /// Return the Base32 encoded string.
@@ -233,12 +205,16 @@ class Base32Encoder {
233205
234206 /// Encode the input bytes in Base32.
235207 String encoded = StringUtils .decode (
236- _Base32Utils ._b32encode (_Base32Const .alphabet, data));
208+ _Base32Utils ._b32encode (_Base32Const .alphabet, data),
209+ );
237210
238211 /// If a custom alphabet is specified, translate the encoded string to the custom alphabet.
239212 if (customAlphabet != null ) {
240213 encoded = _Base32Utils .translateAlphabet (
241- encoded, _Base32Const .alphabet, customAlphabet);
214+ encoded,
215+ _Base32Const .alphabet,
216+ customAlphabet,
217+ );
242218 }
243219
244220 /// Return the Base32 encoded string.
@@ -249,15 +225,19 @@ class Base32Encoder {
249225 /// Optionally, you can specify a custom alphabet for encoding.
250226 static String encodeNoPadding (String data, [String ? customAlphabet]) {
251227 // Encode the input data and then remove any padding characters.
252- return encode (data, customAlphabet)
253- .replaceAll (_Base32Const .paddingChar, '' );
228+ return encode (
229+ data,
230+ customAlphabet,
231+ ).replaceAll (_Base32Const .paddingChar, '' );
254232 }
255233
256234 /// Encode the provided List of bytes into a Base32 encoded string without padding characters.
257235 /// Optionally, you can specify a custom alphabet for encoding.
258236 static String encodeNoPaddingBytes (List <int > data, [String ? customAlphabet]) {
259237 /// Encode the input bytes and then remove any padding characters.
260- return encodeBytes (data, customAlphabet)
261- .replaceAll (_Base32Const .paddingChar, '' );
238+ return encodeBytes (
239+ data,
240+ customAlphabet,
241+ ).replaceAll (_Base32Const .paddingChar, '' );
262242 }
263243}
0 commit comments