From 2227a5969bbf83d05cc6b738670e38d0b56cd437 Mon Sep 17 00:00:00 2001
From: rofl0r <retnyg@gmx.net>
Date: Mon, 27 Aug 2012 21:34:55 +0200
Subject: [PATCH] add blowfish support to bb

---
 libbb/pw_encrypt.c |   23 ++++++++++++++++++++---
 1 files changed, 20 insertions(+), 3 deletions(-)

diff --git a/libbb/pw_encrypt.c b/libbb/pw_encrypt.c
index 39ffa08..d2fb2b9 100644
--- a/libbb/pw_encrypt.c
+++ b/libbb/pw_encrypt.c
@@ -53,9 +53,20 @@ char* FAST_FUNC crypt_make_pw_salt(char salt[MAX_PW_SALT_LEN], const char *algo)
 	int len = 2/2;
 	char *salt_ptr = salt;
 	if (algo[0] != 'd') { /* not des */
-		len = 8/2; /* so far assuming md5 */
 		*salt_ptr++ = '$';
-		*salt_ptr++ = '1';
+		if(algo[0] == 'b') {
+			// blowfish
+			*salt_ptr++ = '2';
+			*salt_ptr++ = 'a';
+			*salt_ptr++ = '$';
+			// 8 rounds
+			*salt_ptr++ = '0';
+			*salt_ptr++ = '8';
+			len = 22/2;
+		} else {
+			*salt_ptr++ = '1';
+			if (algo[0] == 'm') len = 8/2; /* so far assuming md5 */
+		}
 		*salt_ptr++ = '$';
 #if !ENABLE_USE_BB_CRYPT || ENABLE_USE_BB_CRYPT_SHA
 		if (algo[0] == 's') { /* sha */
@@ -95,6 +106,11 @@ to64(char *s, unsigned v, int n)
 /* Other advanced crypt ids (TODO?): */
 /* $2$ or $2a$: Blowfish */
 
+static char *bf_crypt(const char *key, const char *salt) {
+	// use musl-builtin crypt
+	return crypt(key, salt);
+}
+
 static struct const_des_ctx *des_cctx;
 static struct des_ctx *des_ctx;
 
@@ -109,7 +125,8 @@ static char *my_crypt(const char *key, const char *salt)
 		if (salt[1] == '5' || salt[1] == '6')
 			return sha_crypt((char*)key, (char*)salt);
 #endif
-	}
+	} else if (salt[0] == '$' && salt[1] == '2' && salt[2] == 'a' && salt[3] == '$')
+		return bf_crypt(key, salt);
 
 	if (!des_cctx)
 		des_cctx = const_des_init();
-- 
1.7.3.4

