mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

Matthew Swift
28.17.2015 2e056544b859bdd14504fb8357e4298e86b23e2a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at legal-notices/CDDLv1_0.txt
 * or http://forgerock.org/license/CDDLv1.0.html.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at legal-notices/CDDLv1_0.txt.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information:
 *      Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 *
 *      Portions Copyright 2012 Dariusz Janny <dariusz.janny@gmail.com>
 *      Copyright 2013 ForgeRock AS
 *
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.opends.server.extensions;
 
import java.security.MessageDigest;
import java.util.Arrays;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
/**
 * SHA2-based Unix crypt implementation.
 *
 * <p>
 * Based on the C implementation released into the Public Domain by Ulrich
 * Drepper &lt;drepper@redhat.com&gt;
 * http://www.akkadia.org/drepper/SHA-crypt.txt
 * </p>
 *
 * <p>
 * Conversion to Kotlin and from there to Java in 2012 by Christian Hammers
 * &lt;ch@lathspell.de&gt; and likewise put into the Public Domain.
 * </p>
 *
 * <p>
 * This class is immutable and thread-safe.
 * </p>
 *
 * <p>
 * Note this class was originally in the
 * <code>org.apache.commons.codec.digest</code> package, but was moved into
 * <code>org.opends.server.extensions</code> for convenience.
 * </p>
 *
 * @version $Id$
 * @since 1.7
 */
final class Sha2Crypt {
 
  /**
   * Base64 like method to convert binary bytes into ASCII chars.
   *
   * <p>This class is immutable and thread-safe.</p>
   *
   * <p>
   * Note this class was originally in the
   * <code>org.apache.commons.codec.digest</code> package, but was moved into an
   * inner class here for convenience. It is <b>not</b> compatible with Base64.
   * </p>
   *
   * @version $Id$
   * @since 1.7
   */
  static class B64 {
 
      /**
       * Table with characters for Base64 transformation.
       */
      static final String B64T =
          "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
 
      /**
       * Base64 like conversion of bytes to ASCII chars.
       *
       * @param b2
       *            A byte from the result.
       * @param b1
       *            A byte from the result.
       * @param b0
       *            A byte from the result.
       * @param outLen
       *            The number of expected output chars.
       * @param buffer
       *            Where the output chars is appended to.
       */
      static void b64from24bit(byte b2, byte b1, byte b0, int outLen,
          StringBuilder buffer) {
          // The bit masking is necessary because the JVM byte type is signed!
          int w =
              ((b2 << 16) & 0x00ffffff) | ((b1 << 8) & 0x00ffff) | (b0 & 0xff);
          // It's effectively a "for" loop but kept to resemble
          // the original C code.
          int n = outLen;
          while (n-- > 0) {
              buffer.append(B64T.charAt(w & 0x3f));
              w >>= 6;
          }
      }
 
      /**
       * Generates a string of random chars from the B64T set.
       *
       * @param num
       *            Number of chars to generate.
       * @return
       *            a string of random chars from the B64T set
       */
      static String getRandomSalt(int num) {
          StringBuilder saltString = new StringBuilder();
          for (int i = 1; i <= num; i++) {
              saltString.append(B64T.charAt(new Random().
                  nextInt(B64T.length())));
          }
          return saltString.toString();
      }
  }
 
  /**
   * Default number of rounds if not explicitly specified.
   */
  private static final int ROUNDS_DEFAULT = 5000;
 
  /**
   * Maximum number of rounds.
   */
  private static final int ROUNDS_MAX = 999999999;
 
  /**
   * Minimum number of rounds.
   */
  private static final int ROUNDS_MIN = 1000;
 
  /**
   * Prefix for optional rounds specification.
   */
  private static final String ROUNDS_PREFIX = "rounds=";
 
  /**
   * The MessageDigest algorithms.
   */
  private static final String SHA256_ALGORITHM = "SHA-256";
 
  private static final String SHA512_ALGORITHM = "SHA-512";
 
  /**
   * The number of bytes the final hash value will have.
   */
  private static final int SHA256_BLOCKSIZE = 32;
 
  private static final int SHA512_BLOCKSIZE = 64;
 
  /**
   * The prefixes that can be used to identify this crypt() variant.
   */
  static final String SHA256_PREFIX = "$5$";
 
  static final String SHA512_PREFIX = "$6$";
 
 
  /**
   * Returns the magic string denoting the SHA-256 scheme is being used.
   *
   * @return the magic string
   */
  public static String getMagicSHA256Prefix()
  {
    return SHA256_PREFIX;
  }
 
 
 
  /**
   * Returns the magic string denoting the SHA-512 scheme is being used.
   *
   * @return the magic string
   */
  public static String getMagicSHA512Prefix()
  {
    return SHA512_PREFIX;
  }
 
 
 
  /**
   * Generates a libc crypt() compatible "$5$" hash value with random salt.
   *
   * See {@link Crypt#crypt(String, String)} for details.
   *
   * @param keyBytes
   *          The plaintext that should be hashed.
   * @throws Exception exception
   * @return compatible "$5$" hash value with random sal
   */
  public static String sha256Crypt(byte[] keyBytes) throws Exception {
    return sha256Crypt(keyBytes, null);
  }
 
  /**
   * Generates a libc6 crypt() compatible "$5$" hash value.
   *
   * See {@link Crypt#crypt(String, String)} for details.
   *
   * @param keyBytes
   *          The plaintext that should be hashed.
   * @param salt
   *          The real salt value without prefix or "rounds=".
   * @throws Exception exception
   * @return compatible "$5$" hash value
   */
  public static String sha256Crypt(byte[] keyBytes, String salt)
      throws Exception {
    if (salt == null) {
      salt = SHA256_PREFIX + B64.getRandomSalt(8);
    }
    return sha2Crypt(keyBytes, salt, SHA256_PREFIX, SHA256_BLOCKSIZE,
        SHA256_ALGORITHM);
  }
 
  /**
   * Generates a libc6 crypt() compatible "$5$" or "$6$" SHA2 based hash value.
   *
   * This is a nearly line by line conversion of the original C function. The
   * numbered comments are from the algorithm description, the short C-style
   * ones from the original C code and the ones with "Remark" from me.
   *
   * See {@link Crypt#crypt(String, String)} for details.
   *
   * @param keyBytes
   *          The plaintext that should be hashed.
   * @param salt_string
   *          The real salt value without prefix or "rounds=".
   * @param saltPrefix
   *          Either $5$ or $6$.
   * @param blocksize
   *          A value that differs between $5$ and $6$.
   * @param algorithm
   *          The MessageDigest algorithm identifier string.
   * @throws Exception exception
   * @return The complete hash value including prefix and salt.
   */
  private static String sha2Crypt(byte[] keyBytes, String salt,
      String saltPrefix, int blocksize, String algorithm) throws Exception {
    int keyLen = keyBytes.length;
 
    // Extracts effective salt and the number of rounds from the given salt.
    int rounds = ROUNDS_DEFAULT;
    boolean roundsCustom = false;
    if (salt == null) {
      throw new IllegalArgumentException("Invalid salt value: null");
    }
    Pattern p = Pattern
        .compile("^\\$([56])\\$(rounds=(\\d+)\\$)?([\\.\\/a-zA-Z0-9]{1,16}).*");
    Matcher m = p.matcher(salt);
    if (m == null || !m.find()) {
      throw new IllegalArgumentException("Invalid salt value: " + salt);
    }
    if (m.group(3) != null) {
      rounds = Integer.parseInt(m.group(3));
      rounds = Math.max(ROUNDS_MIN, Math.min(ROUNDS_MAX, rounds));
      roundsCustom = true;
    }
    String saltString = m.group(4);
    byte[] saltBytes = saltString.getBytes("UTF-8");
    int saltLen = saltBytes.length;
 
    // 1. start digest A
    // Prepare for the real work.
    MessageDigest ctx = MessageDigest.getInstance(algorithm);
 
    // 2. the password string is added to digest A
    /*
     * Add the key string.
     */
    ctx.update(keyBytes);
 
    // 3. the salt string is added to digest A. This is just the salt string
    // itself without the enclosing '$', without the magic salt_prefix $5$ and
    // $6$ respectively and without the rounds=<N> specification.
    //
    // NB: the MD5 algorithm did add the $1$ salt_prefix. This is not deemed
    // necessary since it is a constant string and does not add security
    // and /possibly/ allows a plain text attack. Since the rounds=<N>
    // specification should never be added this would also create an
    // inconsistency.
    /*
     * The last part is the salt string. This must be at most 16 characters and
     * it ends at the first `$' character (for compatibility with existing
     * implementations).
     */
    ctx.update(saltBytes);
 
    // 4. start digest B
    /*
     * Compute alternate sha512 sum with input KEY, SALT, and KEY. The final
     * result will be added to the first context.
     */
    MessageDigest altCtx = MessageDigest.getInstance(algorithm);
 
    // 5. add the password to digest B
    /*
     * Add key.
     */
    altCtx.update(keyBytes);
 
    // 6. add the salt string to digest B
    /*
     * Add salt.
     */
    altCtx.update(saltBytes);
 
    // 7. add the password again to digest B
    /*
     * Add key again.
     */
    altCtx.update(keyBytes);
 
    // 8. finish digest B
    /*
     * Now get result of this (32 bytes) and add it to the other context.
     */
    byte[] altResult = altCtx.digest();
 
    // 9. For each block of 32 or 64 bytes in the password string (excluding
    // the terminating NUL in the C representation), add digest B to digest A
    /*
     * Add for any character in the key one byte of the alternate sum.
     */
    /*
     * (Remark: the C code comment seems wrong for key length > 32!)
     */
    int cnt = keyBytes.length;
    while (cnt > blocksize) {
      ctx.update(altResult, 0, blocksize);
      cnt -= blocksize;
    }
 
    // 10. For the remaining N bytes of the password string add the first
    // N bytes of digest B to digest A
    ctx.update(altResult, 0, cnt);
 
    // 11. For each bit of the binary representation of the length of the
    // password string up to and including the highest 1-digit, starting
    // from to lowest bit position (numeric value 1):
    //
    // a) for a 1-digit add digest B to digest A
    //
    // b) for a 0-digit add the password string
    //
    // NB: this step differs significantly from the MD5 algorithm. It
    // adds more randomness.
    /*
     * Take the binary representation of the length of the key and for every 1
     * add the alternate sum, for every 0 the key.
     */
    cnt = keyBytes.length;
    while (cnt > 0) {
      if ((cnt & 1) != 0) {
        ctx.update(altResult, 0, blocksize);
      } else {
        ctx.update(keyBytes);
      }
      cnt >>= 1;
    }
 
    // 12. finish digest A
    /*
     * Create intermediate result.
     */
    altResult = ctx.digest();
 
    // 13. start digest DP
    /*
     * Start computation of P byte sequence.
     */
    altCtx = MessageDigest.getInstance(algorithm);
 
    // 14. for every byte in the password (excluding the terminating NUL byte
    // in the C representation of the string)
    //
    // add the password to digest DP
    /*
     * For every character in the password add the entire password.
     */
    for (int i = 1; i <= keyLen; i++) {
      altCtx.update(keyBytes);
    }
 
    // 15. finish digest DP
    /*
     * Finish the digest.
     */
    byte[] tempResult = altCtx.digest();
 
    // 16. produce byte sequence P of the same length as the password where
    //
    // a) for each block of 32 or 64 bytes of length of the password string
    // the entire digest DP is used
    //
    // b) for the remaining N (up to 31 or 63) bytes use the first N
    // bytes of digest DP
    /*
     * Create byte sequence P.
     */
    byte[] pBytes = new byte[keyLen];
    int cp = 0;
    while (cp < keyLen - blocksize) {
      System.arraycopy(tempResult, 0, pBytes, cp, blocksize);
      cp += blocksize;
    }
    System.arraycopy(tempResult, 0, pBytes, cp, keyLen - cp);
 
    // 17. start digest DS
    /*
     * Start computation of S byte sequence.
     */
    altCtx = MessageDigest.getInstance(algorithm);
 
    // 18. repeast the following 16+A[0] times, where A[0] represents the first
    // byte in digest A interpreted as an 8-bit unsigned value
    //
    // add the salt to digest DS
    /*
     * For every character in the password add the entire password.
     */
    for (int i = 1; i <= 16 + (altResult[0] & 0xff); i++) {
      altCtx.update(saltBytes);
    }
 
    // 19. finish digest DS
    /*
     * Finish the digest.
     */
    tempResult = altCtx.digest();
 
    // 20. produce byte sequence S of the same length as the salt string where
    //
    // a) for each block of 32 or 64 bytes of length of the salt string
    // the entire digest DS is used
    //
    // b) for the remaining N (up to 31 or 63) bytes use the first N
    // bytes of digest DS
    /*
     * Create byte sequence S.
     */
    // Remark: The salt is limited to 16 chars, how does this make sense?
    byte[] sBytes = new byte[saltLen];
    cp = 0;
    while (cp < saltLen - blocksize) {
      System.arraycopy(tempResult, 0, sBytes, cp, blocksize);
      cp += blocksize;
    }
    System.arraycopy(tempResult, 0, sBytes, cp, saltLen - cp);
 
    // 21. repeat a loop according to the number specified in the rounds=<N>
    // specification in the salt (or the default value if none is
    // present). Each round is numbered, starting with 0 and up to N-1.
    //
    // The loop uses a digest as input. In the first round it is the
    // digest produced in step 12. In the latter steps it is the digest
    // produced in step 21.h. The following text uses the notation
    // "digest A/C" to describe this behavior.
    /*
     * Repeatedly run the collected hash value through sha512 to burn CPU
     * cycles.
     */
    for (int i = 0; i <= rounds - 1; i++) {
      // a) start digest C
      /*
       * New context.
       */
      ctx = MessageDigest.getInstance(algorithm);
 
      // b) for odd round numbers add the byte sequence P to digest C
      // c) for even round numbers add digest A/C
      /*
       * Add key or last result.
       */
      if ((i & 1) != 0) {
        ctx.update(pBytes, 0, keyLen);
      } else {
        ctx.update(altResult, 0, blocksize);
      }
 
      // d) for all round numbers not divisible by 3 add the byte sequence S
      /*
       * Add salt for numbers not divisible by 3.
       */
      if (i % 3 != 0) {
        ctx.update(sBytes, 0, saltLen);
      }
 
      // e) for all round numbers not divisible by 7 add the byte sequence P
      /*
       * Add key for numbers not divisible by 7.
       */
      if (i % 7 != 0) {
        ctx.update(pBytes, 0, keyLen);
      }
 
      // f) for odd round numbers add digest A/C
      // g) for even round numbers add the byte sequence P
      /*
       * Add key or last result.
       */
      if ((i & 1) != 0) {
        ctx.update(altResult, 0, blocksize);
      } else {
        ctx.update(pBytes, 0, keyLen);
      }
 
      // h) finish digest C.
      /*
       * Create intermediate result.
       */
      altResult = ctx.digest();
    }
 
    // 22. Produce the output string. This is an ASCII string of the maximum
    // size specified above, consisting of multiple pieces:
    //
    // a) the salt salt_prefix, $5$ or $6$ respectively
    //
    // b) the rounds=<N> specification, if one was present in the input
    // salt string. A trailing '$' is added in this case to separate
    // the rounds specification from the following text.
    //
    // c) the salt string truncated to 16 characters
    //
    // d) a '$' character
    /*
     * Now we can construct the result string. It consists of three parts.
     */
    StringBuilder buffer = new StringBuilder(saltPrefix
        + (roundsCustom ? ROUNDS_PREFIX + rounds + "$" : "")
        + saltString + "$");
 
    // e) the base-64 encoded final C digest. The encoding used is as
    // follows:
    // [...]
    //
    // Each group of three bytes from the digest produces four
    // characters as output:
    //
    // 1. character: the six low bits of the first byte
    // 2. character: the two high bits of the first byte and the
    // four low bytes from the second byte
    // 3. character: the four high bytes from the second byte and
    // the two low bits from the third byte
    // 4. character: the six high bits from the third byte
    //
    // The groups of three bytes are as follows (in this sequence).
    // These are the indices into the byte array containing the
    // digest, starting with index 0. For the last group there are
    // not enough bytes left in the digest and the value zero is used
    // in its place. This group also produces only three or two
    // characters as output for SHA-512 and SHA-512 respectively.
 
    // This was just a safeguard in the C implementation:
    // int buflen = salt_prefix.length() - 1 + ROUNDS_PREFIX.length() + 9 + 1 +
    // salt_string.length() + 1 + 86 + 1;
 
    if (blocksize == 32) {
      B64.b64from24bit(altResult[0], altResult[10], altResult[20], 4, buffer);
      B64.b64from24bit(altResult[21], altResult[1], altResult[11], 4, buffer);
      B64.b64from24bit(altResult[12], altResult[22], altResult[2], 4, buffer);
      B64.b64from24bit(altResult[3], altResult[13], altResult[23], 4, buffer);
      B64.b64from24bit(altResult[24], altResult[4], altResult[14], 4, buffer);
      B64.b64from24bit(altResult[15], altResult[25], altResult[5], 4, buffer);
      B64.b64from24bit(altResult[6], altResult[16], altResult[26], 4, buffer);
      B64.b64from24bit(altResult[27], altResult[7], altResult[17], 4, buffer);
      B64.b64from24bit(altResult[18], altResult[28], altResult[8], 4, buffer);
      B64.b64from24bit(altResult[9], altResult[19], altResult[29], 4, buffer);
      B64.b64from24bit((byte) 0, altResult[31], altResult[30], 3, buffer);
    } else {
      B64.b64from24bit(altResult[0], altResult[21], altResult[42], 4, buffer);
      B64.b64from24bit(altResult[22], altResult[43], altResult[1], 4, buffer);
      B64.b64from24bit(altResult[44], altResult[2], altResult[23], 4, buffer);
      B64.b64from24bit(altResult[3], altResult[24], altResult[45], 4, buffer);
      B64.b64from24bit(altResult[25], altResult[46], altResult[4], 4, buffer);
      B64.b64from24bit(altResult[47], altResult[5], altResult[26], 4, buffer);
      B64.b64from24bit(altResult[6], altResult[27], altResult[48], 4, buffer);
      B64.b64from24bit(altResult[28], altResult[49], altResult[7], 4, buffer);
      B64.b64from24bit(altResult[50], altResult[8], altResult[29], 4, buffer);
      B64.b64from24bit(altResult[9], altResult[30], altResult[51], 4, buffer);
      B64.b64from24bit(altResult[31], altResult[52], altResult[10], 4, buffer);
      B64.b64from24bit(altResult[53], altResult[11], altResult[32], 4, buffer);
      B64.b64from24bit(altResult[12], altResult[33], altResult[54], 4, buffer);
      B64.b64from24bit(altResult[34], altResult[55], altResult[13], 4, buffer);
      B64.b64from24bit(altResult[56], altResult[14], altResult[35], 4, buffer);
      B64.b64from24bit(altResult[15], altResult[36], altResult[57], 4, buffer);
      B64.b64from24bit(altResult[37], altResult[58], altResult[16], 4, buffer);
      B64.b64from24bit(altResult[59], altResult[17], altResult[38], 4, buffer);
      B64.b64from24bit(altResult[18], altResult[39], altResult[60], 4, buffer);
      B64.b64from24bit(altResult[40], altResult[61], altResult[19], 4, buffer);
      B64.b64from24bit(altResult[62], altResult[20], altResult[41], 4, buffer);
      B64.b64from24bit((byte) 0, (byte) 0, altResult[63], 2, buffer);
    }
 
    /*
     * Clear the buffer for the intermediate result so that people attaching to
     * processes or reading core dumps cannot get any information.
     */
    // Is there a better way to do this with the JVM?
    Arrays.fill(tempResult, (byte) 0);
    Arrays.fill(pBytes, (byte) 0);
    Arrays.fill(sBytes, (byte) 0);
    ctx.reset();
    altCtx.reset();
    Arrays.fill(keyBytes, (byte) 0);
    Arrays.fill(saltBytes, (byte) 0);
 
    return buffer.toString();
  }
 
  /**
   * Generates a libc crypt() compatible "$6$" hash value with random salt.
   *
   * See {@link Crypt#crypt(String, String)} for details.
   *
   * @param keyBytes
   *          The plaintext that should be hashed.
   * @throws Exception exception
   * @return compatible "$6$" hash value with random salt
   */
  public static String sha512Crypt(byte[] keyBytes) throws Exception {
    return sha512Crypt(keyBytes, null);
  }
 
  /**
   * Generates a libc6 crypt() compatible "$6$" hash value.
   *
   * See {@link Crypt#crypt(String, String)} for details.
   *
   * @param keyBytes
   *          The plaintext that should be hashed.
   * @param salt
   *          The real salt value without prefix or "rounds=".
   * @throws Exception exception
   * @return compatible "$6$" hash value
   */
  public static String sha512Crypt(byte[] keyBytes, String salt)
      throws Exception {
    if (salt == null) {
      salt = SHA512_PREFIX + B64.getRandomSalt(8);
    }
    return sha2Crypt(keyBytes, salt, SHA512_PREFIX, SHA512_BLOCKSIZE,
        SHA512_ALGORITHM);
  }
}