├── LICENSE ├── README.md ├── base64.js └── package.json /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | If you find this package useful hit the star with <3 2 | 3 | # react-native-base64 4 | Base64 encoding and decoding helping util (does not support some Unicode characters). 5 | Created for React Native but can be used anywhere. 6 | 7 | Install 8 | ``` 9 | npm i react-native-base64 10 | ``` 11 | You can find it on npmjs.com here: [https://www.npmjs.com/package/react-native-base64](https://www.npmjs.com/package/react-native-base64) 12 | 13 | How to use: 14 | Add import statement: 15 | ``` 16 | import base64 from 'react-native-base64' 17 | ``` 18 | To Encode: 19 | ``` 20 | base64.encode('Some string to encode to base64'); 21 | ``` 22 | To Decode: 23 | ``` 24 | base64.decode('SW4gbXkgZXllcywgaW5kaXNwb3NlZA0KSW4gZGlzZ3Vpc2VzIG5vIG9uZSBrbm93cw0KUklQIEND=='); 25 | ``` 26 | To Decode from byte arary: 27 | ``` 28 | base64.encodeFromByteArray(byteArray: Uint8Array); 29 | ``` 30 | 31 | Do you like this package? Do you find it useful? 32 | Please rank it with a star and leave a comment. 33 | Thanks :) 34 | -------------------------------------------------------------------------------- /base64.js: -------------------------------------------------------------------------------- 1 | let keyStr = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='; 2 | export default { 3 | encode: function (input) { 4 | var output = []; 5 | var chr1, chr2, chr3 = ""; 6 | var enc1, enc2, enc3, enc4 = ""; 7 | var i = 0; 8 | 9 | do { 10 | chr1 = input.charCodeAt(i++); 11 | chr2 = input.charCodeAt(i++); 12 | chr3 = input.charCodeAt(i++); 13 | 14 | enc1 = chr1 >> 2; 15 | enc2 = ((chr1 & 3) << 4) | (chr2 >> 4); 16 | enc3 = ((chr2 & 15) << 2) | (chr3 >> 6); 17 | enc4 = chr3 & 63; 18 | 19 | if (isNaN(chr2)) { 20 | enc3 = enc4 = 64; 21 | } else if (isNaN(chr3)) { 22 | enc4 = 64; 23 | } 24 | 25 | output.push( 26 | keyStr.charAt(enc1) + 27 | keyStr.charAt(enc2) + 28 | keyStr.charAt(enc3) + 29 | keyStr.charAt(enc4)) 30 | chr1 = chr2 = chr3 = ""; 31 | enc1 = enc2 = enc3 = enc4 = ""; 32 | } while (i < input.length); 33 | 34 | return output.join(''); 35 | }, 36 | 37 | encodeFromByteArray: function (input) { 38 | var output = []; 39 | var chr1, chr2, chr3 = ""; 40 | var enc1, enc2, enc3, enc4 = ""; 41 | var i = 0; 42 | 43 | do { 44 | chr1 = input[i++]; 45 | chr2 = input[i++]; 46 | chr3 = input[i++]; 47 | 48 | enc1 = chr1 >> 2; 49 | enc2 = ((chr1 & 3) << 4) | (chr2 >> 4); 50 | enc3 = ((chr2 & 15) << 2) | (chr3 >> 6); 51 | enc4 = chr3 & 63; 52 | 53 | if (isNaN(chr2)) { 54 | enc3 = enc4 = 64; 55 | } else if (isNaN(chr3)) { 56 | enc4 = 64; 57 | } 58 | 59 | output.push( 60 | keyStr.charAt(enc1) + 61 | keyStr.charAt(enc2) + 62 | keyStr.charAt(enc3) + 63 | keyStr.charAt(enc4)) 64 | chr1 = chr2 = chr3 = ""; 65 | enc1 = enc2 = enc3 = enc4 = ""; 66 | } while (i < input.length); 67 | 68 | return output.join(''); 69 | }, 70 | 71 | decode: function (input) { 72 | var output = ""; 73 | var chr1, chr2, chr3 = ""; 74 | var enc1, enc2, enc3, enc4 = ""; 75 | var i = 0; 76 | 77 | // remove all characters that are not A-Z, a-z, 0-9, +, /, or = 78 | var base64test = /[^A-Za-z0-9\+\/\=]/g; 79 | if (base64test.exec(input)) { 80 | throw new Error("There were invalid base64 characters in the input text.\n" + 81 | "Valid base64 characters are A-Z, a-z, 0-9, '+', '/',and '='\n" + 82 | "Expect errors in decoding."); 83 | } 84 | input = input.replace(/[^A-Za-z0-9\+\/\=]/g, ""); 85 | 86 | do { 87 | enc1 = keyStr.indexOf(input.charAt(i++)); 88 | enc2 = keyStr.indexOf(input.charAt(i++)); 89 | enc3 = keyStr.indexOf(input.charAt(i++)); 90 | enc4 = keyStr.indexOf(input.charAt(i++)); 91 | 92 | chr1 = (enc1 << 2) | (enc2 >> 4); 93 | chr2 = ((enc2 & 15) << 4) | (enc3 >> 2); 94 | chr3 = ((enc3 & 3) << 6) | enc4; 95 | 96 | output = output + String.fromCharCode(chr1); 97 | 98 | if (enc3 != 64) { 99 | output = output + String.fromCharCode(chr2); 100 | } 101 | if (enc4 != 64) { 102 | output = output + String.fromCharCode(chr3); 103 | } 104 | 105 | chr1 = chr2 = chr3 = ""; 106 | enc1 = enc2 = enc3 = enc4 = ""; 107 | 108 | } while (i < input.length); 109 | 110 | return output; 111 | } 112 | } 113 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "react-native-base64", 3 | "version": "0.2.1", 4 | "description": "Base64 encoding and decoding helping util. Created for React Native but can be used anywhere", 5 | "main": "base64.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git+https://github.com/eranbo/react-native-base64.git" 12 | }, 13 | "keywords": [ 14 | "base64", 15 | "encode", 16 | "decode", 17 | "react", 18 | "native" 19 | ], 20 | "author": "eranbo", 21 | "license": "MIT", 22 | "bugs": { 23 | "url": "https://github.com/eranbo/react-native-base64/issues" 24 | }, 25 | "homepage": "https://github.com/eranbo/react-native-base64#readme" 26 | } 27 | --------------------------------------------------------------------------------