fa0499
diff --git a/configure.in b/configure.in
fa0499
index de6a8ad..4ca489d 100644
fa0499
--- a/configure.in
fa0499
+++ b/configure.in
fa0499
@@ -465,6 +465,28 @@ LIBS=""
fa0499
 AC_SEARCH_LIBS(crypt, crypt)
fa0499
 CRYPT_LIBS="$LIBS"
fa0499
 APACHE_SUBST(CRYPT_LIBS)
fa0499
+
fa0499
+if test "$ac_cv_search_crypt" != "no"; then
fa0499
+   # Test crypt() with the SHA-512 test vector from https://akkadia.org/drepper/SHA-crypt.txt
fa0499
+   AC_CACHE_CHECK([whether crypt() supports SHA-2], [ap_cv_crypt_sha2], [
fa0499
+    AC_RUN_IFELSE([AC_LANG_PROGRAM([[
fa0499
+#include <crypt.h>
fa0499
+#include <stdlib.h>
fa0499
+#include <string.h>
fa0499
+
fa0499
+#define PASSWD_0 "Hello world!"
fa0499
+#define SALT_0 "\$6\$saltstring"
fa0499
+#define EXPECT_0 "\$6\$saltstring\$svn8UoSVapNtMuq1ukKS4tPQd8iKwSMHWjl/O817G3uBnIFNjnQJu" \
fa0499
+               "esI68u4OTLiBFdcbYEdFCoEOfaS35inz1"
fa0499
+]], [char *result = crypt(PASSWD_0, SALT_0);
fa0499
+     if (!result) return 1;
fa0499
+     if (strcmp(result, EXPECT_0)) return 2;
fa0499
+])], [ap_cv_crypt_sha2=yes], [ap_cv_crypt_sha2=no])])
fa0499
+   if test "$ap_cv_crypt_sha2" = yes; then
fa0499
+     AC_DEFINE([HAVE_CRYPT_SHA2], 1, [Define if crypt() supports SHA-2 hashes])
fa0499
+   fi
fa0499
+fi
fa0499
+
fa0499
 LIBS="$saved_LIBS"
fa0499
 
fa0499
 dnl See Comment #Spoon
fa0499
diff --git a/support/htpasswd.c b/support/htpasswd.c
fa0499
index 660a27c..136f62a 100644
fa0499
--- a/support/htpasswd.c
fa0499
+++ b/support/htpasswd.c
fa0499
@@ -98,28 +98,32 @@ static int mkrecord(struct passwd_ctx *ctx, char *user)
fa0499
 static void usage(void)
fa0499
 {
fa0499
     apr_file_printf(errfile, "Usage:" NL
fa0499
-        "\thtpasswd [-cimBdpsDv] [-C cost] passwordfile username" NL
fa0499
-        "\thtpasswd -b[cmBdpsDv] [-C cost] passwordfile username password" NL
fa0499
+        "\thtpasswd [-cimB25dpsDv] [-C cost] [-r rounds] passwordfile username" NL
fa0499
+        "\thtpasswd -b[cmB25dpsDv] [-C cost] [-r rounds] passwordfile username password" NL
fa0499
         NL
fa0499
-        "\thtpasswd -n[imBdps] [-C cost] username" NL
fa0499
-        "\thtpasswd -nb[mBdps] [-C cost] username password" NL
fa0499
+        "\thtpasswd -n[imB25dps] [-C cost] [-r rounds] username" NL
fa0499
+        "\thtpasswd -nb[mB25dps] [-C cost] [-r rounds] username password" NL
fa0499
         " -c  Create a new file." NL
fa0499
         " -n  Don't update file; display results on stdout." NL
fa0499
         " -b  Use the password from the command line rather than prompting "
fa0499
             "for it." NL
fa0499
         " -i  Read password from stdin without verification (for script usage)." NL
fa0499
         " -m  Force MD5 encryption of the password (default)." NL
fa0499
-        " -B  Force bcrypt encryption of the password (very secure)." NL
fa0499
+        " -2  Force SHA-256 crypt() hash of the password (secure)." NL
fa0499
+        " -5  Force SHA-512 crypt() hash of the password (secure)." NL
ca8514
+        " -B  Force bcrypt encryption of the password (very secure)." NL
fa0499
         " -C  Set the computing time used for the bcrypt algorithm" NL
fa0499
         "     (higher is more secure but slower, default: %d, valid: 4 to 31)." NL
fa0499
+        " -r  Set the number of rounds used for the SHA-256, SHA-512 algorithms" NL
fa0499
+        "     (higher is more secure but slower, default: 5000)." NL
fa0499
         " -d  Force CRYPT encryption of the password (8 chars max, insecure)." NL
fa0499
-        " -s  Force SHA encryption of the password (insecure)." NL
fa0499
+        " -s  Force SHA-1 encryption of the password (insecure)." NL
fa0499
         " -p  Do not encrypt the password (plaintext, insecure)." NL
fa0499
         " -D  Delete the specified user." NL
fa0499
         " -v  Verify password for the specified user." NL
fa0499
         "On other systems than Windows and NetWare the '-p' flag will "
fa0499
             "probably not work." NL
fa0499
-        "The SHA algorithm does not use a salt and is less secure than the "
fa0499
+        "The SHA-1 algorithm does not use a salt and is less secure than the "
fa0499
             "MD5 algorithm." NL,
fa0499
         BCRYPT_DEFAULT_COST
fa0499
     );
fa0499
@@ -178,7 +182,7 @@ static void check_args(int argc, const char *const argv[],
fa0499
     if (rv != APR_SUCCESS)
fa0499
         exit(ERR_SYNTAX);
fa0499
 
fa0499
-    while ((rv = apr_getopt(state, "cnmspdBbDiC:v", &opt, &opt_arg)) == APR_SUCCESS) {
fa0499
+    while ((rv = apr_getopt(state, "cnmspdBbDi25C:r:v", &opt, &opt_arg)) == APR_SUCCESS) {
fa0499
         switch (opt) {
fa0499
         case 'c':
fa0499
             *mask |= APHTP_NEWFILE;
fa0499
diff --git a/support/passwd_common.c b/support/passwd_common.c
fa0499
index 664e509..d45657c 100644
fa0499
--- a/support/passwd_common.c
fa0499
+++ b/support/passwd_common.c
fa0499
@@ -185,10 +185,15 @@ int mkhash(struct passwd_ctx *ctx)
fa0499
 #if CRYPT_ALGO_SUPPORTED
fa0499
     char *cbuf;
fa0499
 #endif
fa0499
+#ifdef HAVE_CRYPT_SHA2
fa0499
+    const char *setting;
fa0499
+    char method;
fa0499
+#endif
fa0499
 
fa0499
-    if (ctx->cost != 0 && ctx->alg != ALG_BCRYPT) {
fa0499
+    if (ctx->cost != 0 && ctx->alg != ALG_BCRYPT
fa0499
+        && ctx->alg != ALG_CRYPT_SHA256 && ctx->alg != ALG_CRYPT_SHA512 ) {
fa0499
         apr_file_printf(errfile,
fa0499
-                        "Warning: Ignoring -C argument for this algorithm." NL);
fa0499
+                        "Warning: Ignoring -C/-r argument for this algorithm." NL);
fa0499
     }
fa0499
 
fa0499
     if (ctx->passwd == NULL) {
fa0499
@@ -246,6 +251,34 @@ int mkhash(struct passwd_ctx *ctx)
fa0499
         break;
fa0499
 #endif /* CRYPT_ALGO_SUPPORTED */
fa0499
 
fa0499
+#ifdef HAVE_CRYPT_SHA2
fa0499
+    case ALG_CRYPT_SHA256:
fa0499
+    case ALG_CRYPT_SHA512:
fa0499
+        ret = generate_salt(salt, 16, &ctx->errstr, ctx->pool);
fa0499
+        if (ret != 0)
fa0499
+            break;
fa0499
+
fa0499
+        method = ctx->alg == ALG_CRYPT_SHA256 ? '5': '6';
fa0499
+
fa0499
+        if (ctx->cost) 
fa0499
+            setting = apr_psprintf(ctx->pool, "$%c$rounds=%d$%s",
fa0499
+                                   method, ctx->cost, salt);
fa0499
+        else
fa0499
+            setting = apr_psprintf(ctx->pool, "$%c$%s",
fa0499
+                                   method, salt);
fa0499
+
fa0499
+        cbuf = crypt(pw, setting);
fa0499
+        if (cbuf == NULL) {
fa0499
+            rv = APR_FROM_OS_ERROR(errno);
fa0499
+            ctx->errstr = apr_psprintf(ctx->pool, "crypt() failed: %pm", &rv;;
fa0499
+            ret = ERR_PWMISMATCH;
fa0499
+            break;
fa0499
+        }
fa0499
+
fa0499
+        apr_cpystrn(ctx->out, cbuf, ctx->out_len - 1);
fa0499
+        break;
fa0499
+#endif /* HAVE_CRYPT_SHA2 */
fa0499
+
fa0499
 #if BCRYPT_ALGO_SUPPORTED
fa0499
     case ALG_BCRYPT:
fa0499
         rv = apr_generate_random_bytes((unsigned char*)salt, 16);
fa0499
@@ -294,6 +327,19 @@ int parse_common_options(struct passwd_ctx *ctx, char opt,
fa0499
     case 's':
fa0499
         ctx->alg = ALG_APSHA;
fa0499
         break;
fa0499
+#ifdef HAVE_CRYPT_SHA2
fa0499
+    case '2':
fa0499
+        ctx->alg = ALG_CRYPT_SHA256;
fa0499
+        break;
fa0499
+    case '5':
fa0499
+        ctx->alg = ALG_CRYPT_SHA512;
fa0499
+        break;
fa0499
+#else
fa0499
+    case '2':
fa0499
+    case '5':
fa0499
+        ctx->errstr = "SHA-2 crypt() algorithms are not supported on this platform.";
fa0499
+        return ERR_ALG_NOT_SUPP;
fa0499
+#endif
fa0499
     case 'p':
fa0499
         ctx->alg = ALG_PLAIN;
fa0499
 #if !PLAIN_ALGO_SUPPORTED
fa0499
@@ -324,11 +370,12 @@ int parse_common_options(struct passwd_ctx *ctx, char opt,
fa0499
         return ERR_ALG_NOT_SUPP;
fa0499
 #endif
fa0499
         break;
fa0499
-    case 'C': {
fa0499
+    case 'C':
fa0499
+    case 'r': {
fa0499
             char *endptr;
fa0499
             long num = strtol(opt_arg, &endptr, 10);
fa0499
             if (*endptr != '\0' || num <= 0) {
fa0499
-                ctx->errstr = "argument to -C must be a positive integer";
fa0499
+                ctx->errstr = "argument to -C/-r must be a positive integer";
fa0499
                 return ERR_SYNTAX;
fa0499
             }
fa0499
             ctx->cost = num;
fa0499
diff --git a/support/passwd_common.h b/support/passwd_common.h
fa0499
index 660081e..f1b3cd7 100644
fa0499
--- a/support/passwd_common.h
fa0499
+++ b/support/passwd_common.h
fa0499
@@ -28,6 +28,8 @@
fa0499
 #include "apu_version.h"
fa0499
 #endif
fa0499
 
fa0499
+#include "ap_config_auto.h"
fa0499
+
fa0499
 #define MAX_STRING_LEN 256
fa0499
 
fa0499
 #define ALG_PLAIN 0
fa0499
@@ -35,6 +37,8 @@
fa0499
 #define ALG_APMD5 2
fa0499
 #define ALG_APSHA 3
fa0499
 #define ALG_BCRYPT 4
fa0499
+#define ALG_CRYPT_SHA256 5
fa0499
+#define ALG_CRYPT_SHA512 6
fa0499
 
fa0499
 #define BCRYPT_DEFAULT_COST 5
fa0499
 
fa0499
@@ -84,7 +88,7 @@ struct passwd_ctx {
fa0499
     apr_size_t      out_len;
fa0499
     char            *passwd;
fa0499
     int             alg;
fa0499
-    int             cost;
fa0499
+    int             cost; /* cost for bcrypt, rounds for SHA-2 */
fa0499
     enum {
fa0499
         PW_PROMPT = 0,
fa0499
         PW_ARG,