Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecate 'length' parameter in Hash java action #149

Merged
merged 1 commit into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version=10.0.8-SNAPSHOT
version=10.1.0-SNAPSHOT
1 change: 1 addition & 0 deletions marketplace/release-notes/10.1.0.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
We deprecated the 'length' parameter of the StringUtils Hash java action. The length of the hash will now always be 32 bytes, represented as 64 hex characters.
Binary file modified src/CommunityCommons/CommunityCommons.mpr
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ public class StringUtils {

public static final String HASH_ALGORITHM = "SHA-256";

public static String hash(String value) throws NoSuchAlgorithmException, DigestException {
int LENGTH = 32;
return hash(value, LENGTH);
}

@Deprecated
public static String hash(String value, int length) throws NoSuchAlgorithmException, DigestException {
byte[] inBytes = value.getBytes(StandardCharsets.UTF_8);
byte[] outBytes = new byte[length];
Expand Down
112 changes: 56 additions & 56 deletions src/CommunityCommons/javasource/communitycommons/actions/Hash.java
Original file line number Diff line number Diff line change
@@ -1,56 +1,56 @@
// This file was generated by Mendix Studio Pro.
//
// WARNING: Only the following code will be retained when actions are regenerated:
// - the import list
// - the code between BEGIN USER CODE and END USER CODE
// - the code between BEGIN EXTRA CODE and END EXTRA CODE
// Other code you write will be lost the next time you deploy the project.
// Special characters, e.g., é, ö, à, etc. are supported in comments.

package communitycommons.actions;

import communitycommons.StringUtils;
import com.mendix.systemwideinterfaces.core.IContext;
import com.mendix.webui.CustomJavaAction;

/**
* Hashes a value using the SHA-256 hash algorithm.
*
* - value : the value to hash
* - length : the desired length of the hash.
*
* Returns a SHA-256 hash of 'value', with length 'length'
*/
public class Hash extends CustomJavaAction<java.lang.String>
{
private java.lang.String value;
private java.lang.Long length;

public Hash(IContext context, java.lang.String value, java.lang.Long length)
{
super(context);
this.value = value;
this.length = length;
}

@java.lang.Override
public java.lang.String executeAction() throws Exception
{
// BEGIN USER CODE
return StringUtils.hash(value, length.intValue());
// END USER CODE
}

/**
* Returns a string representation of this action
* @return a string representation of this action
*/
@java.lang.Override
public java.lang.String toString()
{
return "Hash";
}

// BEGIN EXTRA CODE
// END EXTRA CODE
}
// This file was generated by Mendix Studio Pro.
//
// WARNING: Only the following code will be retained when actions are regenerated:
// - the import list
// - the code between BEGIN USER CODE and END USER CODE
// - the code between BEGIN EXTRA CODE and END EXTRA CODE
// Other code you write will be lost the next time you deploy the project.
// Special characters, e.g., é, ö, à, etc. are supported in comments.
package communitycommons.actions;
import communitycommons.StringUtils;
import com.mendix.systemwideinterfaces.core.IContext;
import com.mendix.webui.CustomJavaAction;
/**
* Hashes a value using the SHA-256 hash algorithm.
*
* - value : the value to hash
* - length : the desired length of the hash.
*
* Returns a SHA-256 hash of 'value', with length 'length'
*/
public class Hash extends CustomJavaAction<java.lang.String>
{
private java.lang.String value;
private java.lang.Long length;
public Hash(IContext context, java.lang.String value, java.lang.Long length)
{
super(context);
this.value = value;
this.length = length;
}
@java.lang.Override
public java.lang.String executeAction() throws Exception
{
// BEGIN USER CODE
return StringUtils.hash(value);
// END USER CODE
}
/**
* Returns a string representation of this action
* @return a string representation of this action
*/
@java.lang.Override
public java.lang.String toString()
{
return "Hash";
}
// BEGIN EXTRA CODE
// END EXTRA CODE
}
Expand Down
13 changes: 13 additions & 0 deletions src/test/communitycommons/StringUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
import org.owasp.html.PolicyFactory;
import org.owasp.html.Sanitizers;

import java.security.DigestException;
import java.security.NoSuchAlgorithmException;

/**
*
* @author res
Expand Down Expand Up @@ -157,4 +160,14 @@ public void testRandomStrongPassword_WithNumberOfSpecifiedCharactersGreaterThanL
assertThrows(IllegalArgumentException.class, () ->
StringUtils.randomStrongPassword(length, length, upperCount, digitCount, specialCount));
}

@Test
public void testHash() throws DigestException, NoSuchAlgorithmException {
final int length = 32;
final String originalString = "original string";
final String hashedString = "18760223948747fb081582fdef27e3d216d0e6bc67734eb080bb1c0c4b22d01b";

assertEquals(StringUtils.hash(originalString), StringUtils.hash(originalString, length));
assertEquals(StringUtils.hash(originalString), hashedString);
}
}