| 
									
										
										
										
											2018-11-22 00:39:43 -07:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var Enc = module.exports; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-01 15:33:43 -07:00
										 |  |  | Enc.base64ToBuf = function (str) { | 
					
						
							|  |  |  |   // always convert from urlsafe base64, just in case
 | 
					
						
							|  |  |  |   //return Buffer.from(Enc.urlBase64ToBase64(str)).toString('base64');
 | 
					
						
							|  |  |  |   // node handles urlBase64 automatically
 | 
					
						
							|  |  |  |   return Buffer.from(str, 'base64'); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Enc.base64ToHex = function (b64) { | 
					
						
							|  |  |  |   return Enc.bufToHex(Enc.base64ToBuf(b64)); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Enc.bufToBase64 = function (u8) { | 
					
						
							|  |  |  |   // we want to maintain api compatability with browser APIs,
 | 
					
						
							|  |  |  |   // so we assume that this could be a Uint8Array
 | 
					
						
							|  |  |  |   return Buffer.from(u8).toString('base64'); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* | 
					
						
							|  |  |  | Enc.bufToUint8 = function bufToUint8(buf) { | 
					
						
							|  |  |  |   return new Uint8Array(buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.byteLength)); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Enc.bufToUrlBase64 = function (u8) { | 
					
						
							|  |  |  |   return Enc.bufToBase64(u8) | 
					
						
							|  |  |  |     .replace(/\+/g, '-').replace(/\//g, '_').replace(/=/g, ''); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Enc.bufToHex = function (u8) { | 
					
						
							| 
									
										
										
										
											2018-11-22 00:39:43 -07:00
										 |  |  |   var hex = []; | 
					
						
							|  |  |  |   var i, h; | 
					
						
							| 
									
										
										
										
											2018-11-23 22:02:29 -07:00
										 |  |  |   var len = (u8.byteLength || u8.length); | 
					
						
							| 
									
										
										
										
											2018-11-22 00:39:43 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-23 22:02:29 -07:00
										 |  |  |   for (i = 0; i < len; i += 1) { | 
					
						
							| 
									
										
										
										
											2018-11-22 00:39:43 -07:00
										 |  |  |     h = u8[i].toString(16); | 
					
						
							|  |  |  |     if (2 !== h.length) { h = '0' + h; } | 
					
						
							|  |  |  |     hex.push(h); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return hex.join('').toLowerCase(); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-22 17:21:54 -07:00
										 |  |  | Enc.hexToBase64 = function (hex) { | 
					
						
							|  |  |  |   return Buffer.from(hex, 'hex').toString('base64'); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-22 14:31:53 -07:00
										 |  |  | Enc.hexToBuf = function (hex) { | 
					
						
							|  |  |  |   return Buffer.from(hex, 'hex'); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-01 15:33:43 -07:00
										 |  |  | Enc.numToHex = function (d) { | 
					
						
							| 
									
										
										
										
											2018-11-22 04:47:04 -07:00
										 |  |  |   d = d.toString(16); | 
					
						
							|  |  |  |   if (d.length % 2) { | 
					
						
							|  |  |  |     return '0' + d; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   return d; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-22 17:21:54 -07:00
										 |  |  | /* | 
					
						
							| 
									
										
										
										
											2018-12-01 15:33:43 -07:00
										 |  |  | Enc.strToBase64 = function (str) { | 
					
						
							|  |  |  |   // node automatically can tell the difference
 | 
					
						
							|  |  |  |   // between uc2 (utf-8) strings and binary strings
 | 
					
						
							|  |  |  |   // so we don't have to re-encode the strings
 | 
					
						
							|  |  |  |   return Buffer.from(str).toString('base64'); | 
					
						
							| 
									
										
										
										
											2018-11-22 17:21:54 -07:00
										 |  |  | }; | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-22 00:39:43 -07:00
										 |  |  | /* | 
					
						
							| 
									
										
										
										
											2018-12-01 15:33:43 -07:00
										 |  |  | Enc.strToBin = function (str) { | 
					
						
							| 
									
										
										
										
											2018-11-22 00:39:43 -07:00
										 |  |  |   var escstr = encodeURIComponent(str); | 
					
						
							|  |  |  |   // replaces any uri escape sequence, such as %0A,
 | 
					
						
							|  |  |  |   // with binary escape, such as 0x0A
 | 
					
						
							|  |  |  |   var binstr = escstr.replace(/%([0-9A-F]{2})/g, function(match, p1) { | 
					
						
							|  |  |  |     return String.fromCharCode(parseInt(p1, 16)); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return binstr; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-01 15:33:43 -07:00
										 |  |  | Enc.strToBuf = function (str) { | 
					
						
							|  |  |  |   return Buffer.from(str); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Enc.strToHex = function (str) { | 
					
						
							|  |  |  |   return Buffer.from(str).toString('hex'); | 
					
						
							| 
									
										
										
										
											2018-11-22 00:39:43 -07:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* | 
					
						
							| 
									
										
										
										
											2018-12-01 15:33:43 -07:00
										 |  |  | Enc.urlBase64ToBase64 = function (str) { | 
					
						
							| 
									
										
										
										
											2018-11-22 00:39:43 -07:00
										 |  |  |   var r = str % 4; | 
					
						
							|  |  |  |   if (2 === r) { | 
					
						
							|  |  |  |     str += '=='; | 
					
						
							|  |  |  |   } else if (3 === r) { | 
					
						
							|  |  |  |     str += '='; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   return str.replace(/-/g, '+').replace(/_/g, '/'); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | */ |