()
| 88 | } |
| 89 | |
| 90 | function runBasicTest() { |
| 91 | console.log('Running basic JSEncrypt functionality test...\n'); |
| 92 | |
| 93 | try { |
| 94 | // Test 1: Basic encryption/decryption |
| 95 | console.log('Testing basic encryption/decryption...'); |
| 96 | const crypt = new JSEncrypt(); |
| 97 | crypt.setPrivateKey(testKeys.privateKey); |
| 98 | crypt.setPublicKey(testKeys.publicKey); |
| 99 | |
| 100 | const message = "This is a secret message for testing"; |
| 101 | const encrypted = crypt.encrypt(message); |
| 102 | assert(encrypted !== null, "Encryption should succeed"); |
| 103 | assert(typeof encrypted === 'string', "Encrypted data should be a string"); |
| 104 | console.log('✓ Encryption successful'); |
| 105 | |
| 106 | const decrypted = crypt.decrypt(encrypted); |
| 107 | assert(decrypted === message, "Decrypted message should match original"); |
| 108 | console.log('✓ Decryption successful'); |
| 109 | console.log('✓ Basic encryption/decryption test passed\n'); |
| 110 | |
| 111 | // Test 2: Credentials encryption example |
| 112 | console.log('Testing credentials encryption example...'); |
| 113 | |
| 114 | function encryptCredentials(username, password, publicKey) { |
| 115 | const crypt = new JSEncrypt(); |
| 116 | crypt.setPublicKey(publicKey); |
| 117 | |
| 118 | const credentials = JSON.stringify({ |
| 119 | username: username, |
| 120 | password: password, |
| 121 | timestamp: Date.now() |
| 122 | }); |
| 123 | |
| 124 | return crypt.encrypt(credentials); |
| 125 | } |
| 126 | |
| 127 | const encryptedCreds = encryptCredentials('john.doe', 'myPassword123', testKeys.publicKey); |
| 128 | assert(encryptedCreds !== null, "Credentials encryption should succeed"); |
| 129 | console.log('✓ Credentials encryption successful'); |
| 130 | |
| 131 | // Verify we can decrypt credentials |
| 132 | const decryptedCreds = crypt.decrypt(encryptedCreds); |
| 133 | const credentials = JSON.parse(decryptedCreds); |
| 134 | |
| 135 | assert(credentials.username === 'john.doe', "Username should match"); |
| 136 | assert(credentials.password === 'myPassword123', "Password should match"); |
| 137 | console.log('✓ Credentials decryption and verification successful'); |
| 138 | console.log('✓ Credentials encryption example test passed\n'); |
| 139 | |
| 140 | // Test 3: API Communication example |
| 141 | console.log('Testing API communication example...'); |
| 142 | |
| 143 | class SecureAPIClient { |
| 144 | constructor(serverPublicKey, clientPrivateKey) { |
| 145 | this.encryptor = new JSEncrypt(); |
| 146 | this.decryptor = new JSEncrypt(); |
| 147 | this.encryptor.setPublicKey(serverPublicKey); |
no test coverage detected
searching dependent graphs…