Skip to content

Commit 55e1eda

Browse files
authored
Merge pull request #149 from mendix/4016-deprecate-hash-length-parameter
Deprecate 'length' parameter in Hash java action
2 parents ff02630 + 8631939 commit 55e1eda

File tree

6 files changed

+77
-57
lines changed

6 files changed

+77
-57
lines changed

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
version=10.0.8-SNAPSHOT
1+
version=10.1.0-SNAPSHOT
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
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.
0 Bytes
Binary file not shown.

src/CommunityCommons/javasource/communitycommons/StringUtils.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,12 @@ public class StringUtils {
6464

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

67+
public static String hash(String value) throws NoSuchAlgorithmException, DigestException {
68+
int LENGTH = 32;
69+
return hash(value, LENGTH);
70+
}
71+
72+
@Deprecated
6773
public static String hash(String value, int length) throws NoSuchAlgorithmException, DigestException {
6874
byte[] inBytes = value.getBytes(StandardCharsets.UTF_8);
6975
byte[] outBytes = new byte[length];

src/CommunityCommons/javasource/communitycommons/actions/Hash.java

Lines changed: 56 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,56 @@
1-
// This file was generated by Mendix Studio Pro.
2-
//
3-
// WARNING: Only the following code will be retained when actions are regenerated:
4-
// - the import list
5-
// - the code between BEGIN USER CODE and END USER CODE
6-
// - the code between BEGIN EXTRA CODE and END EXTRA CODE
7-
// Other code you write will be lost the next time you deploy the project.
8-
// Special characters, e.g., é, ö, à, etc. are supported in comments.
9-
10-
package communitycommons.actions;
11-
12-
import communitycommons.StringUtils;
13-
import com.mendix.systemwideinterfaces.core.IContext;
14-
import com.mendix.webui.CustomJavaAction;
15-
16-
/**
17-
* Hashes a value using the SHA-256 hash algorithm.
18-
*
19-
* - value : the value to hash
20-
* - length : the desired length of the hash.
21-
*
22-
* Returns a SHA-256 hash of 'value', with length 'length'
23-
*/
24-
public class Hash extends CustomJavaAction<java.lang.String>
25-
{
26-
private java.lang.String value;
27-
private java.lang.Long length;
28-
29-
public Hash(IContext context, java.lang.String value, java.lang.Long length)
30-
{
31-
super(context);
32-
this.value = value;
33-
this.length = length;
34-
}
35-
36-
@java.lang.Override
37-
public java.lang.String executeAction() throws Exception
38-
{
39-
// BEGIN USER CODE
40-
return StringUtils.hash(value, length.intValue());
41-
// END USER CODE
42-
}
43-
44-
/**
45-
* Returns a string representation of this action
46-
* @return a string representation of this action
47-
*/
48-
@java.lang.Override
49-
public java.lang.String toString()
50-
{
51-
return "Hash";
52-
}
53-
54-
// BEGIN EXTRA CODE
55-
// END EXTRA CODE
56-
}
1+
// This file was generated by Mendix Studio Pro.
2+
//
3+
// WARNING: Only the following code will be retained when actions are regenerated:
4+
// - the import list
5+
// - the code between BEGIN USER CODE and END USER CODE
6+
// - the code between BEGIN EXTRA CODE and END EXTRA CODE
7+
// Other code you write will be lost the next time you deploy the project.
8+
// Special characters, e.g., é, ö, à, etc. are supported in comments.
9+
10+
package communitycommons.actions;
11+
12+
import communitycommons.StringUtils;
13+
import com.mendix.systemwideinterfaces.core.IContext;
14+
import com.mendix.webui.CustomJavaAction;
15+
16+
/**
17+
* Hashes a value using the SHA-256 hash algorithm.
18+
*
19+
* - value : the value to hash
20+
* - length : the desired length of the hash.
21+
*
22+
* Returns a SHA-256 hash of 'value', with length 'length'
23+
*/
24+
public class Hash extends CustomJavaAction<java.lang.String>
25+
{
26+
private java.lang.String value;
27+
private java.lang.Long length;
28+
29+
public Hash(IContext context, java.lang.String value, java.lang.Long length)
30+
{
31+
super(context);
32+
this.value = value;
33+
this.length = length;
34+
}
35+
36+
@java.lang.Override
37+
public java.lang.String executeAction() throws Exception
38+
{
39+
// BEGIN USER CODE
40+
return StringUtils.hash(value);
41+
// END USER CODE
42+
}
43+
44+
/**
45+
* Returns a string representation of this action
46+
* @return a string representation of this action
47+
*/
48+
@java.lang.Override
49+
public java.lang.String toString()
50+
{
51+
return "Hash";
52+
}
53+
54+
// BEGIN EXTRA CODE
55+
// END EXTRA CODE
56+
}

src/test/communitycommons/StringUtilsTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
import org.owasp.html.PolicyFactory;
1515
import org.owasp.html.Sanitizers;
1616

17+
import java.security.DigestException;
18+
import java.security.NoSuchAlgorithmException;
19+
1720
/**
1821
*
1922
* @author res
@@ -157,4 +160,14 @@ public void testRandomStrongPassword_WithNumberOfSpecifiedCharactersGreaterThanL
157160
assertThrows(IllegalArgumentException.class, () ->
158161
StringUtils.randomStrongPassword(length, length, upperCount, digitCount, specialCount));
159162
}
163+
164+
@Test
165+
public void testHash() throws DigestException, NoSuchAlgorithmException {
166+
final int length = 32;
167+
final String originalString = "original string";
168+
final String hashedString = "18760223948747fb081582fdef27e3d216d0e6bc67734eb080bb1c0c4b22d01b";
169+
170+
assertEquals(StringUtils.hash(originalString), StringUtils.hash(originalString, length));
171+
assertEquals(StringUtils.hash(originalString), hashedString);
172+
}
160173
}

0 commit comments

Comments
 (0)