-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #501 from kassi/fernet
Add Fernet encryption/decryption operation
- Loading branch information
Showing
7 changed files
with
216 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,63 @@ | ||
/** | ||
* @author Karsten Silkenbäumer [github.com/kassi] | ||
* @copyright Karsten Silkenbäumer 2019 | ||
* @license Apache-2.0 | ||
*/ | ||
|
||
import Operation from "../Operation.mjs"; | ||
import OperationError from "../errors/OperationError.mjs"; | ||
import fernet from "fernet"; | ||
|
||
/** | ||
* FernetDecrypt operation | ||
*/ | ||
class FernetDecrypt extends Operation { | ||
/** | ||
* FernetDecrypt constructor | ||
*/ | ||
constructor() { | ||
super(); | ||
|
||
this.name = "Fernet Decrypt"; | ||
this.module = "Default"; | ||
this.description = "Fernet is a symmetric encryption method which makes sure that the message encrypted cannot be manipulated/read without the key. It uses URL safe encoding for the keys. Fernet uses 128-bit AES in CBC mode and PKCS7 padding, with HMAC using SHA256 for authentication. The IV is created from os.random().<br><br><b>Key:</b> The key must be 32 bytes (256 bits) encoded with Base64."; | ||
this.infoURL = "https://asecuritysite.com/encryption/fer"; | ||
this.inputType = "string"; | ||
this.outputType = "string"; | ||
this.args = [ | ||
{ | ||
"name": "Key", | ||
"type": "string", | ||
"value": "" | ||
}, | ||
]; | ||
this.patterns = [ | ||
{ | ||
match: "^[A-Z\\d\\-_=]{20,}$", | ||
flags: "i", | ||
args: [] | ||
}, | ||
]; | ||
} | ||
/** | ||
* @param {String} input | ||
* @param {Object[]} args | ||
* @returns {String} | ||
*/ | ||
run(input, args) { | ||
const [secretInput] = args; | ||
try { | ||
const secret = new fernet.Secret(secretInput); | ||
const token = new fernet.Token({ | ||
secret: secret, | ||
token: input, | ||
ttl: 0 | ||
}); | ||
return token.decode(); | ||
} catch (err) { | ||
throw new OperationError(err); | ||
} | ||
} | ||
} | ||
|
||
export default FernetDecrypt; |
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,54 @@ | ||
/** | ||
* @author Karsten Silkenbäumer [github.com/kassi] | ||
* @copyright Karsten Silkenbäumer 2019 | ||
* @license Apache-2.0 | ||
*/ | ||
|
||
import Operation from "../Operation.mjs"; | ||
import OperationError from "../errors/OperationError.mjs"; | ||
import fernet from "fernet"; | ||
|
||
/** | ||
* FernetEncrypt operation | ||
*/ | ||
class FernetEncrypt extends Operation { | ||
/** | ||
* FernetEncrypt constructor | ||
*/ | ||
constructor() { | ||
super(); | ||
|
||
this.name = "Fernet Encrypt"; | ||
this.module = "Default"; | ||
this.description = "Fernet is a symmetric encryption method which makes sure that the message encrypted cannot be manipulated/read without the key. It uses URL safe encoding for the keys. Fernet uses 128-bit AES in CBC mode and PKCS7 padding, with HMAC using SHA256 for authentication. The IV is created from os.random().<br><br><b>Key:</b> The key must be 32 bytes (256 bits) encoded with Base64."; | ||
this.infoURL = "https://asecuritysite.com/encryption/fer"; | ||
this.inputType = "string"; | ||
this.outputType = "string"; | ||
this.args = [ | ||
{ | ||
"name": "Key", | ||
"type": "string", | ||
"value": "" | ||
}, | ||
]; | ||
} | ||
/** | ||
* @param {String} input | ||
* @param {Object[]} args | ||
* @returns {String} | ||
*/ | ||
run(input, args) { | ||
const [secretInput] = args; | ||
try { | ||
const secret = new fernet.Secret(secretInput); | ||
const token = new fernet.Token({ | ||
secret: secret, | ||
}); | ||
return token.encode(input); | ||
} catch (err) { | ||
throw new OperationError(err); | ||
} | ||
} | ||
} | ||
|
||
export default FernetEncrypt; |
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,80 @@ | ||
/** | ||
* Fernet tests. | ||
* | ||
* @author Karsten Silkenbäumer [github.com/kassi] | ||
* @copyright Karsten Silkenbäumer 2019 | ||
* @license Apache-2.0 | ||
*/ | ||
import TestRegister from "../TestRegister"; | ||
|
||
TestRegister.addTests([ | ||
{ | ||
name: "Fernet Decrypt: no input", | ||
input: "", | ||
expectedOutput: "Error: Invalid version", | ||
recipeConfig: [ | ||
{ | ||
op: "Fernet Decrypt", | ||
args: ["MTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTI="] | ||
} | ||
], | ||
}, | ||
{ | ||
name: "Fernet Decrypt: no secret", | ||
input: "gAAAAABce-Tycae8klRxhDX2uenJ-uwV8-A1XZ2HRnfOXlNzkKKfRxviNLlgtemhT_fd1Fw5P_zFUAjd69zaJBQyWppAxVV00SExe77ql8c5n62HYJOnoIU=", | ||
expectedOutput: "Error: Secret must be 32 url-safe base64-encoded bytes.", | ||
recipeConfig: [ | ||
{ | ||
op: "Fernet Decrypt", | ||
args: [""] | ||
} | ||
], | ||
}, | ||
{ | ||
name: "Fernet Decrypt: valid arguments", | ||
input: "gAAAAABce-Tycae8klRxhDX2uenJ-uwV8-A1XZ2HRnfOXlNzkKKfRxviNLlgtemhT_fd1Fw5P_zFUAjd69zaJBQyWppAxVV00SExe77ql8c5n62HYJOnoIU=", | ||
expectedOutput: "This is a secret message.\n", | ||
recipeConfig: [ | ||
{ | ||
op: "Fernet Decrypt", | ||
args: ["VGhpc0lzVGhpcnR5VHdvQ2hhcmFjdGVyc0xvbmdLZXk="] | ||
} | ||
], | ||
} | ||
]); | ||
|
||
TestRegister.addTests([ | ||
{ | ||
name: "Fernet Encrypt: no input", | ||
input: "", | ||
expectedMatch: /^gAAAAABce-[\w-]+={0,2}$/, | ||
recipeConfig: [ | ||
{ | ||
op: "Fernet Encrypt", | ||
args: ["MTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTI="] | ||
} | ||
], | ||
}, | ||
{ | ||
name: "Fernet Encrypt: no secret", | ||
input: "This is a secret message.\n", | ||
expectedOutput: "Error: Secret must be 32 url-safe base64-encoded bytes.", | ||
recipeConfig: [ | ||
{ | ||
op: "Fernet Encrypt", | ||
args: [""] | ||
} | ||
], | ||
}, | ||
{ | ||
name: "Fernet Encrypt: valid arguments", | ||
input: "This is a secret message.\n", | ||
expectedMatch: /^gAAAAABce-[\w-]+={0,2}$/, | ||
recipeConfig: [ | ||
{ | ||
op: "Fernet Encrypt", | ||
args: ["MTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTI="] | ||
} | ||
], | ||
} | ||
]); |