From 4d2b81a6203ce009836611074425d6c98268baed Mon Sep 17 00:00:00 2001 From: delchev Date: Tue, 28 Jun 2016 17:12:03 +0300 Subject: [PATCH] nashorn compatibility - base64, digest --- core_api/ScriptingServices/utils/base64.js | 18 +++++- core_api/ScriptingServices/utils/digest.js | 67 +++++++++++++++++----- core_api/TestCases/utils/digest_test.js | 9 ++- 3 files changed, 75 insertions(+), 19 deletions(-) diff --git a/core_api/ScriptingServices/utils/base64.js b/core_api/ScriptingServices/utils/base64.js index 0c0aade..e8ac0fc 100644 --- a/core_api/ScriptingServices/utils/base64.js +++ b/core_api/ScriptingServices/utils/base64.js @@ -8,18 +8,30 @@ * SAP - initial API and implementation *******************************************************************************/ -/* globals $ java */ +/* globals $ java engine */ /* eslint-env node, dirigible */ exports.decode = function(input) { var inputBytes = getBytes(input); - var outputBytes = $.getBase64Utils().decodeBase64(inputBytes); + + var outputBytes; + if (engine === "nashorn") { + outputBytes = $.getBase64Utils().class.static.decodeBase64(inputBytes); + } else { + outputBytes = $.getBase64Utils().decodeBase64(inputBytes); + } + return toString(outputBytes); }; exports.encode = function(input) { var inputBytes = getBytes(input); - var outputBytes = $.getBase64Utils().encodeBase64(inputBytes); + var outputBytes; + if (engine === "nashorn") { + outputBytes = $.getBase64Utils().class.static.encodeBase64(inputBytes); + } else { + outputBytes = $.getBase64Utils().encodeBase64(inputBytes); + } return toString(outputBytes); }; diff --git a/core_api/ScriptingServices/utils/digest.js b/core_api/ScriptingServices/utils/digest.js index cd1b36f..6a8f91f 100644 --- a/core_api/ScriptingServices/utils/digest.js +++ b/core_api/ScriptingServices/utils/digest.js @@ -8,41 +8,82 @@ * SAP - initial API and implementation *******************************************************************************/ -/* globals $ java */ +/* globals $ java engine */ /* eslint-env node, dirigible */ +var streams = require("io/streams"); + exports.md5 = function(input) { - var output = $.getDigestUtils().md5(input); - return toString(output); + var output; + if (engine === "nashorn") { + output = $.getDigestUtils().class.static.md5(input); + } else { + output = $.getDigestUtils().md5(input); + } + return streams.toJavaScriptBytes(output); }; exports.md5Hex = function(input) { - var output = $.getDigestUtils().md5Hex(input); + var output; + if (engine === "nashorn") { + output = $.getDigestUtils().class.static.md5Hex(input); + } else { + output = $.getDigestUtils().md5Hex(input); + } return toString(output); }; +exports.sha1 = function(input) { + var output; + if (engine === "nashorn") { + output = $.getDigestUtils().class.static.sha(input); + } else { + output = $.getDigestUtils().sha(input); + } + return streams.toJavaScriptBytes(output); +}; + exports.sha = function(input) { - var output = $.getDigestUtils().sha(input); - return toString(output); + return exports.sha1(input); }; exports.sha256 = function(input) { - var output = $.getDigestUtils().sha256(input); - return toString(output); + var output; + if (engine === "nashorn") { + output = $.getDigestUtils().class.static.sha256(input); + } else { + output = $.getDigestUtils().sha256(input); + } + return streams.toJavaScriptBytes(output); }; exports.sha384 = function(input) { - var output = $.getDigestUtils().sha384(input); - return toString(output); + var output; + if (engine === "nashorn") { + output = $.getDigestUtils().class.static.sha384(input); + } else { + output = $.getDigestUtils().sha384(input); + } + return streams.toJavaScriptBytes(output); }; exports.sha512 = function(input) { - var output = $.getDigestUtils().sha512(input); - return toString(output); + var output; + if (engine === "nashorn") { + output = $.getDigestUtils().class.static.sha512(input); + } else { + output = $.getDigestUtils().sha512(input); + } + return streams.toJavaScriptBytes(output); }; exports.shaHex = function(input) { - var output = $.getDigestUtils().shaHex(input); + var output; + if (engine === "nashorn") { + output = $.getDigestUtils().class.static.shaHex(input); + } else { + output = $.getDigestUtils().shaHex(input); + } return toString(output); }; diff --git a/core_api/TestCases/utils/digest_test.js b/core_api/TestCases/utils/digest_test.js index c8d1b83..7af9877 100644 --- a/core_api/TestCases/utils/digest_test.js +++ b/core_api/TestCases/utils/digest_test.js @@ -2,10 +2,13 @@ /* eslint-env node, dirigible */ var digest = require('utils/digest'); -var response = require('http/response'); +var response = require('net/http/response'); -response.println("" + digest.sha256('admin:admin')); -response.println("" + digest.sha512('YWRtaW46YWRtaW4=')); +console.log("256: " + digest.sha256('admin:admin')); +console.log("512: " + digest.sha512('admin:admin')); + +response.println("256: " + digest.sha256('admin:admin')); +response.println("512: " + digest.sha512('admin:admin')); response.flush(); response.close();