You cannot select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
	
	
		
			42 lines
		
	
	
		
			1.3 KiB
		
	
	
	
		
			TypeScript
		
	
		
		
			
		
	
	
			42 lines
		
	
	
		
			1.3 KiB
		
	
	
	
		
			TypeScript
		
	
| 
								 
											5 years ago
										 
									 | 
							
								import * as crypto from 'crypto';
							 | 
						||
| 
								 | 
							
								import { LocalizerType } from '../types/Util';
							 | 
						||
| 
								 
											7 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											7 years ago
										 
									 | 
							
								const ERRORS = {
							 | 
						||
| 
								 | 
							
								  TYPE: 'Password must be a string',
							 | 
						||
| 
								 | 
							
								  LENGTH: 'Password must be between 6 and 50 characters long',
							 | 
						||
| 
								 | 
							
								  CHARACTER: 'Password must only contain letters, numbers and symbols',
							 | 
						||
| 
								 | 
							
								};
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								const sha512 = (text: string) => {
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								  const hash = crypto.createHash('sha512');
							 | 
						||
| 
								 | 
							
								  hash.update(text.trim());
							 | 
						||
| 
								 | 
							
								  return hash.digest('hex');
							 | 
						||
| 
								 | 
							
								};
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								export const generateHash = (phrase: string) => phrase && sha512(phrase.trim());
							 | 
						||
| 
								 | 
							
								export const matchesHash = (phrase: string | null, hash: string) =>
							 | 
						||
| 
								 
											7 years ago
										 
									 | 
							
								  phrase && sha512(phrase.trim()) === hash.trim();
							 | 
						||
| 
								 
											7 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								export const validatePassword = (phrase: string, i18n: LocalizerType) => {
							 | 
						||
| 
								 
											7 years ago
										 
									 | 
							
								  if (typeof phrase !== 'string') {
							 | 
						||
| 
								 
											7 years ago
										 
									 | 
							
								    return i18n ? i18n('passwordTypeError') : ERRORS.TYPE;
							 | 
						||
| 
								 | 
							
								  }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								  const trimmed = phrase.trim();
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								  if (trimmed.length === 0) {
							 | 
						||
| 
								 | 
							
								    return i18n ? i18n('noGivenPassword') : ERRORS.LENGTH;
							 | 
						||
| 
								 | 
							
								  }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											7 years ago
										 
									 | 
							
								  if (trimmed.length < 6 || trimmed.length > 50) {
							 | 
						||
| 
								 | 
							
								    return i18n ? i18n('passwordLengthError') : ERRORS.LENGTH;
							 | 
						||
| 
								 
											7 years ago
										 
									 | 
							
								  }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											7 years ago
										 
									 | 
							
								  // Restrict characters to letters, numbers and symbols
							 | 
						||
| 
								 
											7 years ago
										 
									 | 
							
								  const characterRegex = /^[a-zA-Z0-9-!()._`~@#$%^&*+=[\]{}|<>,;: ]+$/;
							 | 
						||
| 
								 
											7 years ago
										 
									 | 
							
								  if (!characterRegex.test(trimmed)) {
							 | 
						||
| 
								 | 
							
								    return i18n ? i18n('passwordCharacterError') : ERRORS.CHARACTER;
							 | 
						||
| 
								 
											7 years ago
										 
									 | 
							
								  }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								  return null;
							 | 
						||
| 
								 
											7 years ago
										 
									 | 
							
								};
							 |