1CRYPT(3P) POSIX Programmer's Manual CRYPT(3P)
2
3
4
6 This manual page is part of the POSIX Programmer's Manual. The Linux
7 implementation of this interface may differ (consult the corresponding
8 Linux manual page for details of Linux behavior), or the interface may
9 not be implemented on Linux.
10
11
13 crypt — string encoding function (CRYPT)
14
16 #include <unistd.h>
17
18 char *crypt(const char *key, const char *salt);
19
21 The crypt() function is a string encoding function. The algorithm is
22 implementation-defined.
23
24 The key argument points to a string to be encoded. The salt argument
25 shall be a string of at least two bytes in length not including the
26 null character chosen from the set:
27
28 a b c d e f g h i j k l m n o p q r s t u v w x y z
29 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
30 0 1 2 3 4 5 6 7 8 9 . /
31
32 The first two bytes of this string may be used to perturb the encoding
33 algorithm.
34
35 The return value of crypt() points to static data that is overwritten
36 by each call.
37
38 The crypt() function need not be thread-safe.
39
41 Upon successful completion, crypt() shall return a pointer to the
42 encoded string. The first two bytes of the returned value shall be
43 those of the salt argument. Otherwise, it shall return a null pointer
44 and set errno to indicate the error.
45
47 The crypt() function shall fail if:
48
49 ENOSYS The functionality is not supported on this implementation.
50
51 The following sections are informative.
52
54 Encoding Passwords
55 The following example finds a user database entry matching a particular
56 user name and changes the current password to a new password. The
57 crypt() function generates an encoded version of each password. The
58 first call to crypt() produces an encoded version of the old password;
59 that encoded password is then compared to the password stored in the
60 user database. The second call to crypt() encodes the new password
61 before it is stored.
62
63 The putpwent() function, used in the following example, is not part of
64 POSIX.1‐2008.
65
66 #include <unistd.h>
67 #include <pwd.h>
68 #include <string.h>
69 #include <stdio.h>
70 ...
71 int valid_change;
72 int pfd; /* Integer for file descriptor returned by open(). */
73 FILE *fpfd; /* File pointer for use in putpwent(). */
74 struct passwd *p;
75 char user[100];
76 char oldpasswd[100];
77 char newpasswd[100];
78 char savepasswd[100];
79 ...
80 valid_change = 0;
81 while ((p = getpwent()) != NULL) {
82 /* Change entry if found. */
83 if (strcmp(p->pw_name, user) == 0) {
84 if (strcmp(p->pw_passwd, crypt(oldpasswd, p->pw_passwd)) == 0) {
85 strcpy(savepasswd, crypt(newpasswd, user));
86 p->pw_passwd = savepasswd;
87 valid_change = 1;
88 }
89 else {
90 fprintf(stderr, "Old password is not valid\n");
91 }
92 }
93 /* Put passwd entry into ptmp. */
94 putpwent(p, fpfd);
95 }
96
98 The values returned by this function need not be portable among XSI-
99 conformant systems.
100
102 None.
103
105 None.
106
108 encrypt(), setkey()
109
110 The Base Definitions volume of POSIX.1‐2008, <unistd.h>
111
113 Portions of this text are reprinted and reproduced in electronic form
114 from IEEE Std 1003.1, 2013 Edition, Standard for Information Technology
115 -- Portable Operating System Interface (POSIX), The Open Group Base
116 Specifications Issue 7, Copyright (C) 2013 by the Institute of Electri‐
117 cal and Electronics Engineers, Inc and The Open Group. (This is
118 POSIX.1-2008 with the 2013 Technical Corrigendum 1 applied.) In the
119 event of any discrepancy between this version and the original IEEE and
120 The Open Group Standard, the original IEEE and The Open Group Standard
121 is the referee document. The original Standard can be obtained online
122 at http://www.unix.org/online.html .
123
124 Any typographical or formatting errors that appear in this page are
125 most likely to have been introduced during the conversion of the source
126 files to man page format. To report such errors, see https://www.ker‐
127 nel.org/doc/man-pages/reporting_bugs.html .
128
129
130
131IEEE/The Open Group 2013 CRYPT(3P)