1212import java .util .List ;
1313
1414import org .hibernate .HibernateException ;
15+ import org .jboss .logging .Logger ;
1516
1617import static java .util .Comparator .comparing ;
1718
@@ -30,6 +31,8 @@ public static NamingHelper withCharset(String charset) {
3031
3132 private final String charset ;
3233
34+ private static final Logger log = Logger .getLogger (NamingHelper .class );
35+
3336 public NamingHelper () {
3437 this (null );
3538 }
@@ -124,8 +127,8 @@ public String generateHashedConstraintName(
124127 }
125128
126129 /**
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
129132 * that the length of the name will always be smaller than the 30
130133 * character identifier restriction enforced by a few dialects.
131134 *
@@ -135,17 +138,36 @@ public String generateHashedConstraintName(
135138 */
136139 public String hashedName (String name ) {
137140 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" );
146142 }
147- catch ( NoSuchAlgorithmException | UnsupportedEncodingException e ) {
148- throw new HibernateException ( "Unable to generate a hashed name" , e );
143+ catch (NoSuchAlgorithmException | UnsupportedEncodingException e ) {
144+ log .warnf ("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+ }
149151 }
150152 }
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+ }
151173}
0 commit comments