mirror of
				https://github.com/TeamPiped/Piped.git
				synced 2024-08-14 23:57:27 +00:00 
			
		
		
		
	Add utilities to encrypt text.
This commit is contained in:
		
							parent
							
								
									e79ebc2429
								
							
						
					
					
						commit
						bd7de9f94e
					
				
					 1 changed files with 19 additions and 0 deletions
				
			
		
							
								
								
									
										19
									
								
								src/utils/encryptionUtils.js
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										19
									
								
								src/utils/encryptionUtils.js
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,19 @@
 | 
			
		|||
// These functions accept and return Uint8Arrays
 | 
			
		||||
 | 
			
		||||
export async function encryptAESGCM(plaintext, key) {
 | 
			
		||||
    const iv = crypto.getRandomValues(new Uint8Array(12));
 | 
			
		||||
    const algorithm = { name: "AES-GCM", iv: iv };
 | 
			
		||||
    const keyMaterial = await crypto.subtle.importKey("raw", key, algorithm, false, ["encrypt"]);
 | 
			
		||||
    const ciphertext = await crypto.subtle.encrypt(algorithm, keyMaterial, plaintext);
 | 
			
		||||
 | 
			
		||||
    return new Uint8Array([...iv, ...new Uint8Array(ciphertext)]);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export async function decryptAESGCM(ciphertextArray, key) {
 | 
			
		||||
    const iv = new Uint8Array(ciphertextArray.slice(0, 12));
 | 
			
		||||
    const algorithm = { name: "AES-GCM", iv: iv };
 | 
			
		||||
    const keyMaterial = await crypto.subtle.importKey("raw", key, algorithm, false, ["decrypt"]);
 | 
			
		||||
    const decrypted = await crypto.subtle.decrypt(algorithm, keyMaterial, new Uint8Array(ciphertextArray.slice(12)));
 | 
			
		||||
 | 
			
		||||
    return decrypted;
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue