From 89e612900c41fa863e8da22a9c172cda11ca72fa Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Sat, 23 Nov 2019 11:26:05 +0100 Subject: [PATCH] Add crypto functions --- src/utils/CryptUtils.ts | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 src/utils/CryptUtils.ts diff --git a/src/utils/CryptUtils.ts b/src/utils/CryptUtils.ts new file mode 100644 index 0000000..8922dc4 --- /dev/null +++ b/src/utils/CryptUtils.ts @@ -0,0 +1,30 @@ +/** + * Cryptographic utilities + * + * @author Pierre HUBERT + */ + +import * as crypto from 'crypto'; + +import { execSync } from 'child_process'; + +/** + * Generate sha1-encoded string + * + * @param str The string to encode + */ +export function sha1(str : string) : string { + const shasum = crypto.createHash("sha1"); + shasum.update(str); + return shasum.digest("hex"); +} + +/** + * Interface with PHP crypt function + * + * @param str The string to encrypt + * @param salt Salt to use + */ +export function crypt(str : string, salt: string) : string { + return execSync("/usr/bin/php -r \"echo crypt(\'"+str+"\', \'"+salt+"\');\"").toString(); +} \ No newline at end of file