(target, key)
| 6 | ) |
| 7 | |
| 8 | const encrypt = (target, key) => { |
| 9 | const data = Buffer.from(target).toString('binary'); |
| 10 | |
| 11 | // We use a random iv generation, but you'll probably want to use some |
| 12 | // deterministic method |
| 13 | const iv = crypto.randomBytes(16) |
| 14 | |
| 15 | const cipher = crypto.createCipheriv('aes-256-cbc', key, iv); |
| 16 | |
| 17 | let encrypted = Buffer.from( |
| 18 | cipher.update(data, 'utf8', 'binary') + cipher.final('binary'), |
| 19 | 'binary', |
| 20 | ); |
| 21 | |
| 22 | return Buffer.concat([iv, encrypted]).toString('base64').replace(/=/g, '').replace(/\+/g, '-').replace(/\//g, '_') |
| 23 | } |
| 24 | |
| 25 | const url = 'http://img.example.com/pretty/image.jpg' |
| 26 | const encrypted_url = encrypt(url, KEY) |
no outgoing calls
no test coverage detected