diff --git a/package-lock.json b/package-lock.json index 88c9b48b5..ba8813a49 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1260,6 +1260,11 @@ "randomfill": "^1.0.3" } }, + "crypto-js": { + "version": "3.1.9-1", + "resolved": "https://registry.npmjs.org/crypto-js/-/crypto-js-3.1.9-1.tgz", + "integrity": "sha1-/aGedh/Ad+Af+/3G6f38WeiAbNg=" + }, "crypto-random-string": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/crypto-random-string/-/crypto-random-string-1.0.0.tgz", @@ -7460,7 +7465,7 @@ "source-map": { "version": "0.6.1", "resolved": "https://registry.npmjs.org/source-map/-/source-map-0.6.1.tgz", - "integrity": "sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==" + "integrity": "sha1-dHIq8y6WFOnCh6jQu95IteLxomM=" }, "source-map-resolve": { "version": "0.5.2", @@ -8490,7 +8495,7 @@ "which": { "version": "1.3.1", "resolved": "https://registry.npmjs.org/which/-/which-1.3.1.tgz", - "integrity": "sha1-pFBD1U9YBTFtqNYvn1CRjT2nCwo=", + "integrity": "sha512-HxJdYWq1MTIQbJ3nw0cqssHoTNU267KlrDuGZ1WYlxDStUtKUhOaJmh112/TZmHxxUfuJqPXSOm7tDyas0OSIQ==", "requires": { "isexe": "^2.0.0" } diff --git a/package.json b/package.json index cc845e6a4..d863cd015 100644 --- a/package.json +++ b/package.json @@ -51,6 +51,7 @@ "dependencies": { "camelcase": "^4.1.0", "capitalize": "^1.0.0", + "crypto-js": "^3.1.9-1", "esrever": "^0.2.0", "fs-extra": "^5.0.0", "htmlspecialchars": "^1.0.5", diff --git a/src/lib/cache/filesystem.ts b/src/lib/cache/filesystem.ts index 44e6cdca9..3bb67b324 100644 --- a/src/lib/cache/filesystem.ts +++ b/src/lib/cache/filesystem.ts @@ -5,7 +5,9 @@ import {TwingEnvironment} from "../environment"; let fs = require('fs-extra'); let path = require('path'); let tmp = require('tmp'); -let crypto = require('crypto'); + +const sha256 = require('crypto-js/sha256'); +const hex = require('crypto-js/enc-hex'); /** * Implements a cache on the filesystem. @@ -29,7 +31,7 @@ export class TwingCacheFilesystem implements TwingCacheInterface { } generateKey(name: string, className: string) { - let hash: string = crypto.createHash('sha256').update(className).digest('hex'); + let hash: string = hex.stringify(sha256(className)); return path.join( this.directory, diff --git a/src/lib/compiler.ts b/src/lib/compiler.ts index 085ad1272..060ccc790 100644 --- a/src/lib/compiler.ts +++ b/src/lib/compiler.ts @@ -5,7 +5,8 @@ import {ksort} from "./helper/ksort"; const substr_count = require('locutus/php/strings/substr_count'); const addcslashes = require('locutus/php/strings/addcslashes'); -const crypto = require('crypto'); +const sha256 = require('crypto-js/sha256'); +const hex = require('crypto-js/enc-hex'); export class TwingCompiler { private lastLine: number; @@ -302,6 +303,6 @@ export class TwingCompiler { } getVarName(prefix: string = '__internal_'): string { - return `${prefix}${crypto.createHash('sha256').update('TwingCompiler::getVarName' + this.varNameSalt++).digest('hex')}`; + return `${prefix}${hex.stringify(sha256('TwingCompiler::getVarName' + this.varNameSalt++))}`; } } diff --git a/src/lib/environment.ts b/src/lib/environment.ts index 66521ffdf..b56ebe2a5 100644 --- a/src/lib/environment.ts +++ b/src/lib/environment.ts @@ -58,7 +58,8 @@ import {TwingSandboxSecurityNotAllowedTagError} from "./sandbox/security-not-all import {TwingSourceMapNode, TwingSourceMapNodeConstructor} from "./source-map/node"; const path = require('path'); -const crypto = require('crypto'); +const sha256 = require('crypto-js/sha256'); +const hex = require('crypto-js/enc-hex'); /** * * Available options: @@ -363,7 +364,7 @@ export abstract class TwingEnvironment extends EventEmitter { getTemplateClass(name: string, index: number = null, from: TwingSource = null) { let key = this.getLoader().getCacheKey(name, from) + this.optionsHash; - return this.templateClassPrefix + crypto.createHash('sha256').update(key).digest('hex') + (index === null ? '' : '_' + index); + return this.templateClassPrefix + hex.stringify(sha256(key)) + (index === null ? '' : '_' + index); } /** @@ -535,7 +536,7 @@ return module.exports; */ createTemplate(template: string) { let result: TwingTemplate; - let name = `__string_template__${crypto.createHash('sha256').update(template).digest('hex')}`; + let name = `__string_template__${hex.stringify(sha256(template))}`; let current = this.getLoader(); let loader = new TwingLoaderChain([ diff --git a/src/lib/parser.ts b/src/lib/parser.ts index 539688647..0462b63c0 100644 --- a/src/lib/parser.ts +++ b/src/lib/parser.ts @@ -20,7 +20,8 @@ import {push} from "./helper/push"; const ctype_space = require('locutus/php/ctype/ctype_space'); const mt_rand = require('locutus/php/math/mt_rand'); -const crypto = require('crypto'); +const sha256 = require('crypto-js/sha256'); +const hex = require('crypto-js/enc-hex'); class TwingParserStackEntry { stream: TwingTokenStream; @@ -65,7 +66,7 @@ export class TwingParser { } getVarName(prefix: string = '__internal_'): string { - return `${prefix}${crypto.createHash('sha256').update('TwingParser::getVarName' + this.stream.getSourceContext().getCode() + this.varNameSalt++).digest('hex')}`; + return `${prefix}${hex.stringify(sha256('TwingParser::getVarName' + this.stream.getSourceContext().getCode() + this.varNameSalt++))}`; } parse(stream: TwingTokenStream, test: Array = null, dropNeedle: boolean = false): TwingNodeModule { diff --git a/src/lib/profiler/node-visitor/profiler.ts b/src/lib/profiler/node-visitor/profiler.ts index 775b00c84..ff8542082 100644 --- a/src/lib/profiler/node-visitor/profiler.ts +++ b/src/lib/profiler/node-visitor/profiler.ts @@ -7,7 +7,8 @@ import {TwingNodeBody} from "../../node/body"; import {TwingProfilerNodeEnterProfile} from "../node/enter-profile"; import {TwingProfilerNodeLeaveProfile} from "../node/leave-profile"; -const crypto = require('crypto'); +const sha256 = require('crypto-js/sha256'); +const hex = require('crypto-js/enc-hex'); export class TwingProfilerNodeVisitorProfiler extends TwingBaseNodeVisitor { private extensionName: string; @@ -72,7 +73,7 @@ export class TwingProfilerNodeVisitorProfiler extends TwingBaseNodeVisitor { } private getVarName(): string { - return `__internal_${crypto.createHash('sha256').update(this.extensionName).digest('hex')}`; + return `__internal_${hex.stringify(sha256(this.extensionName))}`; } getPriority() {