Index: core/org/apache/jmeter/functions/AbstractFunction.java =================================================================== --- core/org/apache/jmeter/functions/AbstractFunction.java (revision 1813248) +++ core/org/apache/jmeter/functions/AbstractFunction.java (working copy) @@ -20,6 +20,7 @@ import java.util.Collection; +import org.apache.commons.lang3.StringUtils; import org.apache.jmeter.engine.util.CompoundVariable; import org.apache.jmeter.samplers.SampleResult; import org.apache.jmeter.samplers.Sampler; @@ -31,6 +32,8 @@ * Provides common methods for all functions */ public abstract class AbstractFunction implements Function { + + private static final String SHOULD_DO_UPPERCASE = "true"; /** *

@@ -140,4 +143,40 @@ ); } } + + /** + * Utility method to add variable value by variable name + * + * @param variableName variable name to put + * @param value variable value to put + */ + protected void addVariableValue(String value, CompoundVariable[] values, int index) { + if (values.length > index) { + String variableName = values[index].execute(); + if (StringUtils.isNotEmpty(variableName)) { + JMeterVariables vars = getVariables(); + if (vars != null) { + vars.put(variableName, value); + } + } + } + } + + + /** + * Upper case value if optional parameter value is true + * @param encodedString + * @param index + * @return + */ + protected String upperCase(String encodedString, CompoundVariable[] values, int index) { + if (values.length > index) { + String shouldUpperCase = values[index].execute(); + boolean shouldDoUpperCase = SHOULD_DO_UPPERCASE.equalsIgnoreCase(shouldUpperCase); + if (shouldDoUpperCase) { + encodedString = encodedString.toUpperCase(); + } + } + return encodedString; + } } Index: core/org/apache/jmeter/resources/messages.properties =================================================================== --- core/org/apache/jmeter/resources/messages.properties (revision 1813248) +++ core/org/apache/jmeter/resources/messages.properties (working copy) @@ -95,6 +95,7 @@ aggregate_report_stddev=Std. Dev. aggregate_report_total_label=TOTAL ajp_sampler_title=AJP/1.3 Sampler +algorithm_string=Digest algorithm als_message=Note\: The Access Log Parser is generic in design and allows you to plugin als_message2=your own parser. To do so, implement the LogParser, add the jar to the als_message3=/lib directory and enter the class in the sampler. @@ -933,6 +934,7 @@ running_test=Running test runtime_controller_title=Runtime Controller runtime_seconds=Runtime (seconds) +salt_string=Salt to be used for encoding sample_result_save_configuration=Sample Result Save Configuration sample_scope=Apply to: sample_scope_all=Main sample and sub-samples @@ -1023,6 +1025,7 @@ servername=Servername \: session_argument_name=Session Argument Name setup_thread_group_title=setUp Thread Group +sha_string=String to be encoded should_save=You should save your test plan before running it. \nIf you are using supporting data files (ie, for CSV Data Set or __StringFromFile), \nthen it is particularly important to first save your test script. \nDo you want to save your test plan first? shutdown=Shutdown simple_config_element=Simple Config Element @@ -1224,6 +1227,7 @@ update_per_iter=Update Once Per Iteration upload=File Upload upper_bound=Upper Bound +upper_case=Upper case result - default false (optional) url=URL url_config_get=GET url_config_http=HTTP Index: functions/org/apache/jmeter/functions/DigestEncode.java =================================================================== --- functions/org/apache/jmeter/functions/DigestEncode.java (revision 0) +++ functions/org/apache/jmeter/functions/DigestEncode.java (working copy) @@ -0,0 +1,114 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +package org.apache.jmeter.functions; + +import org.apache.commons.lang3.StringUtils; +import org.apache.jmeter.engine.util.CompoundVariable; +import org.apache.jmeter.functions.AbstractFunction; +import org.apache.jmeter.functions.InvalidVariableException; +import org.apache.jmeter.samplers.SampleResult; +import org.apache.jmeter.samplers.Sampler; +import org.apache.jmeter.util.JMeterUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.UnsupportedEncodingException; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; + +import java.util.Collection; +import java.util.LinkedList; +import java.util.List; + +/** + * + * Sha512 Encode Function Optional Upper case value and save to variable name + * + * @author orim + * + */ +public class DigestEncode extends AbstractFunction { + + private static final Logger log = LoggerFactory.getLogger(DigestEncode.class); + private static final String UTF_8 = "UTF-8"; + + /* + * The algorithm names in this section can be specified when generating an + * instance of MessageDigest: SHA-1 SHA-256 SHA-384 SHA-512 + */ + private static final List desc = new LinkedList(); + private static final String KEY = "__digest"; + + // Number of parameters expected - used to reject invalid calls + private static final int MIN_PARAMETER_COUNT = 2; + private static final int MAX_PARAMETER_COUNT = 5; + + static { + desc.add(JMeterUtils.getResString("algorithm_string")); + desc.add(JMeterUtils.getResString("sha_string")); + desc.add(JMeterUtils.getResString("salt_string")); + desc.add(JMeterUtils.getResString("upper_case")); + desc.add(JMeterUtils.getResString("function_name_paropt")); + } + + private CompoundVariable[] values; + + @Override + public synchronized String execute(SampleResult previousResult, Sampler currentSampler) throws InvalidVariableException { + String encodedString = null; + String digestAlgorithm = values[0].execute(); + String stringToEncode = values[1].execute(); + String salt = null; + if (values.length > 2) { + salt = values[2].execute(); + } + try { + MessageDigest md = MessageDigest.getInstance(digestAlgorithm); + if (StringUtils.isNotEmpty(salt)) { + md.update(salt.getBytes(UTF_8)); + } + byte[] bytes = md.digest(stringToEncode.getBytes(UTF_8)); + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < bytes.length; i++) { + sb.append(Integer.toString((bytes[i] & 0xff) + 0x100, 16).substring(1)); + } + encodedString = upperCase(sb.toString(), values, 3); + addVariableValue(encodedString, values, 4); + + } catch (NoSuchAlgorithmException | UnsupportedEncodingException e) { + log.warn("Error executing SHA encryption", e); + } + return encodedString; + } + + @Override + public synchronized void setParameters(Collection parameters) throws InvalidVariableException { + checkParameterCount(parameters, MIN_PARAMETER_COUNT, MAX_PARAMETER_COUNT); + values = parameters.toArray(new CompoundVariable[parameters.size()]); + } + + @Override + public String getReferenceKey() { + return KEY; + } + + @Override + public List getArgumentDesc() { + return desc; + } +} \ No newline at end of file