12
12
import java .util .List ;
13
13
14
14
import org .hibernate .HibernateException ;
15
+ import org .jboss .logging .Logger ;
15
16
16
17
import static java .util .Comparator .comparing ;
17
18
@@ -30,6 +31,8 @@ public static NamingHelper withCharset(String charset) {
30
31
31
32
private final String charset ;
32
33
34
+ private static final Logger log = Logger .getLogger (NamingHelper .class );
35
+
33
36
public NamingHelper () {
34
37
this (null );
35
38
}
@@ -124,8 +127,8 @@ public String generateHashedConstraintName(
124
127
}
125
128
126
129
/**
127
- * Hash a constraint name using MD5. Convert the MD5 digest to base 35
128
- * (full alphanumeric), guaranteeing
130
+ * Hash a constraint name using MD5. If MD5 is not available, fall back to SHA-256.
131
+ * Convert the digest to base 35 (full alphanumeric), guaranteeing
129
132
* that the length of the name will always be smaller than the 30
130
133
* character identifier restriction enforced by a few dialects.
131
134
*
@@ -135,17 +138,36 @@ public String generateHashedConstraintName(
135
138
*/
136
139
public String hashedName (String name ) {
137
140
try {
138
- final MessageDigest md5 = MessageDigest .getInstance ( "MD5" );
139
- md5 .reset ();
140
- md5 .update ( charset != null ? name .getBytes ( charset ) : name .getBytes () );
141
- final BigInteger bigInt = new BigInteger ( 1 , md5 .digest () );
142
- // By converting to base 35 (full alphanumeric), we guarantee
143
- // that the length of the name will always be smaller than the 30
144
- // character identifier restriction enforced by a few dialects.
145
- return bigInt .toString ( 35 );
141
+ return hashWithAlgorithm (name , "MD5" );
146
142
}
147
- catch ( NoSuchAlgorithmException | UnsupportedEncodingException e ) {
148
- throw new HibernateException ( "Unable to generate a hashed name" , e );
143
+ catch (NoSuchAlgorithmException | UnsupportedEncodingException e ) {
144
+ log .infof ("MD5 algorithm failed for hashedName, falling back to SHA-256: %s" , e .getMessage ());
145
+ try {
146
+ return hashWithAlgorithm (name , "SHA-256" );
147
+ }
148
+ catch (NoSuchAlgorithmException | UnsupportedEncodingException ex ) {
149
+ throw new HibernateException ("Unable to generate a hashed name" , ex );
150
+ }
149
151
}
150
152
}
153
+
154
+ /**
155
+ * Helper to hash a name with the given algorithm and convert to base 35.
156
+ *
157
+ * @param name The name to be hashed.
158
+ * @param algorithm The hashing algorithm to use.
159
+ *
160
+ * @return String The hashed name.
161
+ */
162
+ public String hashWithAlgorithm (String name , String algorithm )
163
+ throws NoSuchAlgorithmException , UnsupportedEncodingException {
164
+ final MessageDigest md = MessageDigest .getInstance (algorithm );
165
+ md .reset ();
166
+ md .update ( charset != null ? name .getBytes ( charset ) : name .getBytes () );
167
+ final BigInteger bigInt = new BigInteger ( 1 , md .digest () );
168
+ // By converting to base 35 (full alphanumeric), we guarantee
169
+ // that the length of the name will always be smaller than the 30
170
+ // character identifier restriction enforced by a few dialects.
171
+ return bigInt .toString ( 35 );
172
+ }
151
173
}
0 commit comments