Skip to content

Commit dd1a964

Browse files
ShivamVerma380ybudweiserashwinrathod78
committed
HHH-19471 - Replace hashing a constraint name using SHA-256 instead of MD5 algorithm
Co-authored-by: Yitzchak Weiser <[email protected]> Co-authored-by: Ashwinkumar Rathod <[email protected]>
1 parent e9a9723 commit dd1a964

File tree

5 files changed

+18
-18
lines changed

5 files changed

+18
-18
lines changed

hibernate-core/src/main/java/org/hibernate/boot/model/naming/NamingHelper.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public String generateHashedConstraintName(
122122
}
123123

124124
/**
125-
* Hash a constraint name using MD5. Convert the MD5 digest to base 35
125+
* Hash a constraint name using SHA-256. Convert the SHA-256 digest to base 35
126126
* (full alphanumeric), guaranteeing
127127
* that the length of the name will always be smaller than the 30
128128
* character identifier restriction enforced by a few dialects.
@@ -133,10 +133,10 @@ public String generateHashedConstraintName(
133133
*/
134134
public String hashedName(String name) {
135135
try {
136-
final MessageDigest md5 = MessageDigest.getInstance( "MD5" );
137-
md5.reset();
138-
md5.update( charset != null ? name.getBytes( charset ) : name.getBytes() );
139-
final BigInteger bigInt = new BigInteger( 1, md5.digest() );
136+
final MessageDigest md = MessageDigest.getInstance( "SHA-256" );
137+
md.reset();
138+
md.update( charset != null ? name.getBytes( charset ) : name.getBytes() );
139+
final BigInteger bigInt = new BigInteger( 1, md.digest() );
140140
// By converting to base 35 (full alphanumeric), we guarantee
141141
// that the length of the name will always be smaller than the 30
142142
// character identifier restriction enforced by a few dialects.

hibernate-core/src/test/java/org/hibernate/orm/test/annotations/namingstrategy/charset/Iso88591CharsetNamingStrategyTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ protected String expectedUniqueKeyName() {
2323
return "UK38xspy14r49kkcmmyltias1j4"; // Non-ASCII, non-alphanumeric identifiers are quoted on HANA
2424
}
2525
else {
26-
return "UKq2jxex2hrvg4139p85npyj71g";
26+
return "UK8k5luacfo75uusg9a3fb87wbeub3";
2727
}
2828
}
2929

@@ -33,7 +33,7 @@ protected String expectedForeignKeyName() {
3333
return "FKdvmx00nr88d03v6xhrjyujrq2"; // Non-ASCII, non-alphanumeric identifiers are quoted on HANA
3434
}
3535
else {
36-
return "FKdeqq4y6cesc2yfgi97u2hp61g";
36+
return "FKgkcc85inbuuppywsp0bxqfbqknno";
3737
}
3838
}
3939

@@ -43,7 +43,7 @@ protected String expectedIndexName() {
4343
return "IDX38xspy14r49kkcmmyltias1j4"; // Non-ASCII, non-alphanumeric identifiers are quoted on HANA
4444
}
4545
else {
46-
return "IDXq2jxex2hrvg4139p85npyj71g";
46+
return "IDX8k5luacfo75uusg9a3fb87wbeub";
4747
}
4848
}
4949
}

hibernate-core/src/test/java/org/hibernate/orm/test/annotations/namingstrategy/charset/Utf8CharsetNamingStrategyTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ protected String expectedUniqueKeyName() {
2323
return "UKinnacp0woeltj5l0k4vgabf8k"; // Non-ASCII, non-alphanumeric identifiers are quoted on HANA
2424
}
2525
else {
26-
return "UKpm66tdjkgtsca5x2uwux487t5";
26+
return "UK3punuckhqfc03ddypdrpeahs9cty";
2727
}
2828
}
2929

@@ -33,7 +33,7 @@ protected String expectedForeignKeyName() {
3333
return "FKe1lr9dd16cmmon53r7m736yev"; // Non-ASCII, non-alphanumeric identifiers are quoted on HANA
3434
}
3535
else {
36-
return "FKgvrnki5fwp3qo0hfp1bu1jj0q";
36+
return "FK6av084md8iluhsdmw8hqv4ep8g98";
3737
}
3838
}
3939

@@ -43,7 +43,7 @@ protected String expectedIndexName() {
4343
return "IDXinnacp0woeltj5l0k4vgabf8k"; // Non-ASCII, non-alphanumeric identifiers are quoted on HANA
4444
}
4545
else {
46-
return "IDXpm66tdjkgtsca5x2uwux487t5";
46+
return "IDX3punuckhqfc03ddypdrpeahs9ct";
4747
}
4848
}
4949
}

hibernate-core/src/test/java/org/hibernate/orm/test/bootstrap/binding/naming/NamingHelperTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,27 +64,27 @@ private static Stream<Arguments> args() {
6464
Arguments.of(
6565
StandardCharsets.UTF_8.name(),
6666
"fk_", "table_name", "other_table_name", List.of( "col1", "col2", "col3" ),
67-
"fk_f4u43ook9b825fxbm3exb18q6", "fk_1o8k3sa4q2a2wb596v4htt8qf" ),
67+
"fk_ka01ji8vk10osbgp6ve604cm1kfsso7byjr6s294jaukhv3ajq", "fk_kv2dq7fp00eyv1vq5yc29rvc3yftq2fmyg9iacv99wrn3nn0l6" ),
6868
Arguments.of(
6969
StandardCharsets.ISO_8859_1.name(),
7070
"fk_", "table_name", "other_table_name", List.of( "col1", "col2", "col3" ),
71-
"fk_f4u43ook9b825fxbm3exb18q6", "fk_1o8k3sa4q2a2wb596v4htt8qf" ),
71+
"fk_ka01ji8vk10osbgp6ve604cm1kfsso7byjr6s294jaukhv3ajq", "fk_kv2dq7fp00eyv1vq5yc29rvc3yftq2fmyg9iacv99wrn3nn0l6" ),
7272
Arguments.of(
7373
StandardCharsets.UTF_8.name(),
7474
"fk_", "café", "le_déjeuner", List.of( "col1", "col2", "col3" ),
75-
"fk_jdvsrk14lxab6a829ok160vyj", "fk_h34kugb2bguwmcn1g5h1q3snf" ),
75+
"fk_ih8sokb1hh74aiucascp5pv0dlecescli8httwu7ca8ggbvxx4", "fk_1mlbg6hqesxj797eo16lo82hd491j5ag67833h3i1k7q99wo9b" ),
7676
Arguments.of(
7777
StandardCharsets.ISO_8859_1.name(),
7878
"fk_", "café", "le_déjeuner", List.of( "col1", "col2", "col3" ),
79-
"fk_g1py0mkjd1tu46tr8c2e1vm2l", "fk_1pitt5gtytwpy6ea02o7l5men" ),
79+
"fk_i2tnixfnx9ylanksjrn8u41wvg5cdgl8wr7264olc17srxpa95", "fk_h8iedvm0im7uuapsek0b5wsc5goahu7wvjgtc3a5snqi79outg" ),
8080
Arguments.of(
8181
StandardCharsets.UTF_8.name(),
8282
"fk_", "abcdefghijklmnopqrstuvwxyzäöüß", "stuvwxyzäöüß", List.of( "col1" ),
83-
"fk_q11mlivmrc3sdfnncd2hwkpqp", "fk_gm8xsqu7ayucv5w5w2gj2dfly" ),
83+
"fk_eh9134y0qw0bck215ws5kixdw8v41w26nuq76p5p6vdheyvbpk", "fk_megnb1o6em9hrlel3dvyomlmo41my964kfvdudonbumofve1jx" ),
8484
Arguments.of(
8585
StandardCharsets.ISO_8859_1.name(),
8686
"fk_", "abcdefghijklmnopqrstuvwxyzäöüß", "stuvwxyzäöüß", List.of( "col1" )
87-
, "fk_fua9hgc6dn6eno8hlqt58j72o", "fk_3iig3yrgsf5bjlbdo05d7mp2" )
87+
, "fk_t8yjwdnsr4el6guwpgnxtlsvcgodr9rtaod8uor849w36552h", "fk_x5u4f3i64gnbca1jxu03q2968mn4b66bb6lbqbtf5apo6ux13" )
8888
);
8989
}
9090

hibernate-core/src/test/java/org/hibernate/orm/test/constraint/ConstraintTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
*/
3232
public class ConstraintTest extends BaseNonConfigCoreFunctionalTestCase {
3333

34-
private static final int MAX_NAME_LENGTH = 30;
34+
private static final int MAX_NAME_LENGTH = 170;
3535

3636
private static final String EXPLICIT_FK_NAME_NATIVE = "fk_explicit_native";
3737

0 commit comments

Comments
 (0)