-
Notifications
You must be signed in to change notification settings - Fork 29.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
crypto: add cipher update/final methods encoding validation
Refs #45189 PR-URL: #45990 Refs: #45189 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Filip Skokan <[email protected]>
- Loading branch information
1 parent
5294371
commit a558774
Showing
2 changed files
with
60 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
if (!common.hasCrypto) | ||
common.skip('missing crypto'); | ||
|
||
// This test checks if error is thrown in case of wrong encoding provided into cipher. | ||
|
||
const assert = require('assert'); | ||
const { createCipheriv, randomBytes } = require('crypto'); | ||
|
||
const createCipher = () => { | ||
return createCipheriv('aes-256-cbc', randomBytes(32), randomBytes(16)); | ||
}; | ||
|
||
{ | ||
const cipher = createCipher(); | ||
cipher.update('test', 'utf-8', 'utf-8'); | ||
|
||
assert.throws( | ||
() => cipher.update('666f6f', 'hex', 'hex'), | ||
{ message: /Cannot change encoding/ } | ||
); | ||
} | ||
|
||
{ | ||
const cipher = createCipher(); | ||
cipher.update('test', 'utf-8', 'utf-8'); | ||
|
||
assert.throws( | ||
() => cipher.final('hex'), | ||
{ message: /Cannot change encoding/ } | ||
); | ||
} | ||
|
||
{ | ||
const cipher = createCipher(); | ||
cipher.update('test', 'utf-8', 'utf-8'); | ||
|
||
assert.throws( | ||
() => cipher.final('bad2'), | ||
{ message: /^Unknown encoding: bad2$/, code: 'ERR_UNKNOWN_ENCODING' } | ||
); | ||
} | ||
|
||
{ | ||
const cipher = createCipher(); | ||
|
||
assert.throws( | ||
() => cipher.update('test', 'utf-8', 'bad3'), | ||
{ message: /^Unknown encoding: bad3$/, code: 'ERR_UNKNOWN_ENCODING' } | ||
); | ||
} |