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