CreateHash
Creating a hash of a given input, support sha1, sha256, sha512, md5 and blake. Wrapper for https://docs.python.org/3/library/hashlib.html.
- Type
- python
- Pack
- CommonScripts
Source
import hashlib
from hashlib import blake2b
import demistomock as demisto # noqa: F401
from CommonServerPython import * # noqa: F401
def create_hash(text, hashtype):
"""Create a hash from a given input and return it as a context outputs
Args:
text (str): input to hash
hashtype (str): hash type
Returns:
Dict[str,str]: Dictionary representing the command results context
"""
if hashtype == "sha512":
h = hashlib.sha512()
h.update(text.encode("utf-8"))
elif hashtype == "sha256":
h = hashlib.sha256()
h.update(text.encode("utf-8"))
elif hashtype == "sha1":
h = hashlib.sha1() # nosec
h.update(text.encode("utf-8"))
elif hashtype == "md5":
h = hashlib.md5() # nosec
h.update(text.encode("utf-8"))
else:
h = blake2b() # type: ignore[assignment]
h.update(text.encode("utf-8"))
context = {"CreateHash": str(h.hexdigest())}
return context
def main(): # pragma: no cover
args = demisto.args()
text = args.get("text")
hashtype = args.get("type")
context = create_hash(text, hashtype)
return_results(CommandResults(outputs=context))
if __name__ in ("__builtin__", "builtins", "__main__"):
main()
README
Creating a hash of a given input, support sha1, sha256, sha512, md5 and blake. Wrapper for https://docs.python.org/3/library/hashlib.html
Script Data
| Name |
Description |
| Script Type |
python3 |
| Tags |
transformer, Utilities, hash |
Inputs
| Argument Name |
Description |
| text |
Text or string for which a hash should be created. |
| type |
Hash method to be used. |
Outputs
| Path |
Description |
Type |
| CreateHash |
|
string |