File tree Expand file tree Collapse file tree 2 files changed +23
-0
lines changed Expand file tree Collapse file tree 2 files changed +23
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * @function XORCipher
3
+ * @description - Encrypt using an XOR cipher
4
+ * The XOR cipher is a type of additive cipher.
5
+ * Each character is bitwise XORed with the key.
6
+ * We loop through the input string, XORing each
7
+ * character with the key.
8
+ * @param {string } str - string to be encrypted
9
+ * @param {number } key - key for encryption
10
+ * @return {string } encrypted string
11
+ */
12
+ export const XORCipher = ( str : string , key : number ) : string =>
13
+ str . replace ( / ./ g, ( char : string ) =>
14
+ String . fromCharCode ( char . charCodeAt ( 0 ) ^ key )
15
+ )
Original file line number Diff line number Diff line change
1
+ import { XORCipher } from '../XORCipher' ;
2
+
3
+ describe ( 'Testing XORCipher function' , ( ) => {
4
+ it ( 'passing a string & number as an argument' , ( ) => {
5
+ expect ( XORCipher ( 'test' , 32 ) ) . toBe ( 'TEST' ) ;
6
+ expect ( XORCipher ( 'TEST' , 32 ) ) . toBe ( 'test' ) ;
7
+ } ) ;
8
+ } ) ;
You can’t perform that action at this time.
0 commit comments