blob: d6b2fe28ddcfcd0990b65caeafa6a8cef3e8b564 [file] [log] [blame]
Eric Andersen27f64e12002-06-23 04:24:25 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Utility routine.
4 *
Eric Andersenc7bda1c2004-03-15 08:29:22 +00005 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
Eric Andersen27f64e12002-06-23 04:24:25 +00006 *
"Robert P. J. Day"5d8843e2006-07-10 11:41:19 +00007 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Eric Andersen27f64e12002-06-23 04:24:25 +00008 */
9
Rob Landleyea224be2006-06-18 20:20:07 +000010#include "libbb.h"
Eric Andersen27f64e12002-06-23 04:24:25 +000011#include <string.h>
12#include <crypt.h>
Eric Andersen27f64e12002-06-23 04:24:25 +000013
14
Rob Landleydfba7412006-03-06 20:47:33 +000015char *pw_encrypt(const char *clear, const char *salt)
Eric Andersen27f64e12002-06-23 04:24:25 +000016{
17 static char cipher[128];
18 char *cp;
19
20#ifdef CONFIG_FEATURE_SHA1_PASSWORDS
21 if (strncmp(salt, "$2$", 3) == 0) {
22 return sha1_crypt(clear);
23 }
24#endif
25 cp = (char *) crypt(clear, salt);
26 /* if crypt (a nonstandard crypt) returns a string too large,
27 truncate it so we don't overrun buffers and hope there is
28 enough security in what's left */
Glenn L McGrath393183d2003-05-26 14:07:50 +000029 safe_strncpy(cipher, cp, sizeof(cipher));
Eric Andersen27f64e12002-06-23 04:24:25 +000030 return cipher;
31}
32