| 
									
										
										
										
											2017-10-21 21:10:33 -04:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-28 19:55:55 -04:00
										 |  |  | const crypto = require('crypto'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | function newNoteId() { | 
					
						
							| 
									
										
										
										
											2017-10-28 12:12:20 -04:00
										 |  |  |     return randomString(12); | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-19 08:47:22 -05:00
										 |  |  | function newNoteTreeId() { | 
					
						
							|  |  |  |     return randomString(8); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function newNoteHistoryId() { | 
					
						
							|  |  |  |     return randomString(12); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-28 12:12:20 -04:00
										 |  |  | const ALPHA_NUMERIC = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-28 13:19:12 -04:00
										 |  |  | function randomString(length) { | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  |     let result = ''; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for (let i = length; i > 0; --i) { | 
					
						
							| 
									
										
										
										
											2017-10-28 12:12:20 -04:00
										 |  |  |         result += ALPHA_NUMERIC[Math.floor(Math.random() * ALPHA_NUMERIC.length)]; | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return result; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-28 19:55:55 -04:00
										 |  |  | function randomSecureToken(bytes = 32) { | 
					
						
							| 
									
										
										
										
											2017-10-29 11:22:41 -04:00
										 |  |  |     return crypto.randomBytes(bytes).toString('base64'); | 
					
						
							| 
									
										
										
										
											2017-10-28 19:55:55 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | function nowTimestamp() { | 
					
						
							| 
									
										
										
										
											2017-10-15 17:07:34 -04:00
										 |  |  |     return Math.floor(Date.now() / 1000); | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function toBase64(plainText) { | 
					
						
							|  |  |  |     return Buffer.from(plainText).toString('base64'); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function fromBase64(encodedText) { | 
					
						
							|  |  |  |     return Buffer.from(encodedText, 'base64'); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-29 14:55:48 -04:00
										 |  |  | function hmac(secret, value) { | 
					
						
							|  |  |  |     const hmac = crypto.createHmac('sha256', Buffer.from(secret.toString(), 'ASCII')); | 
					
						
							|  |  |  |     hmac.update(value.toString()); | 
					
						
							|  |  |  |     return hmac.digest('base64'); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-05 10:41:54 -05:00
										 |  |  | function browserId(req) { | 
					
						
							| 
									
										
										
										
											2017-11-06 19:48:02 -05:00
										 |  |  |     return req == null ? null : req.get('x-browser-id'); | 
					
						
							| 
									
										
										
										
											2017-11-05 10:41:54 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-05 17:58:55 -05:00
										 |  |  | function isElectron() { | 
					
						
							|  |  |  |     return !!process.versions['electron']; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-05 21:56:42 -05:00
										 |  |  | function formatDateTimeFromTS(timestamp) { | 
					
						
							|  |  |  |     const date = new Date(timestamp * 1000); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return date.toISOString(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function formatTwoTimestamps(origTS, newTS) { | 
					
						
							|  |  |  |     return "orig: " + formatDateTimeFromTS(origTS) + ", new: " + formatDateTimeFromTS(newTS); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | module.exports = { | 
					
						
							| 
									
										
										
										
											2017-10-28 19:55:55 -04:00
										 |  |  |     randomSecureToken, | 
					
						
							| 
									
										
										
										
											2017-10-28 12:12:20 -04:00
										 |  |  |     randomString, | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  |     nowTimestamp, | 
					
						
							|  |  |  |     newNoteId, | 
					
						
							| 
									
										
										
										
											2017-11-19 08:47:22 -05:00
										 |  |  |     newNoteTreeId, | 
					
						
							|  |  |  |     newNoteHistoryId, | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  |     toBase64, | 
					
						
							| 
									
										
										
										
											2017-10-29 14:55:48 -04:00
										 |  |  |     fromBase64, | 
					
						
							| 
									
										
										
										
											2017-11-05 10:41:54 -05:00
										 |  |  |     hmac, | 
					
						
							| 
									
										
										
										
											2017-11-05 17:58:55 -05:00
										 |  |  |     browserId, | 
					
						
							| 
									
										
										
										
											2017-11-05 21:56:42 -05:00
										 |  |  |     isElectron, | 
					
						
							|  |  |  |     formatTwoTimestamps | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | }; |