1CRYPT(P) POSIX Programmer's Manual CRYPT(P)
2
3
4
6 crypt - string encoding function (CRYPT)
7
9 #include <unistd.h>
10
11 char *crypt(const char *key, const char *salt);
12
13
15 The crypt() function is a string encoding function. The algorithm is
16 implementation-defined.
17
18 The key argument points to a string to be encoded. The salt argument is
19 a string chosen from the set:
20
21
22 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
23 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
24 0 1 2 3 4 5 6 7 8 9 . /
25
26 The first two characters of this string may be used to perturb the
27 encoding algorithm.
28
29 The return value of crypt() points to static data that is overwritten
30 by each call.
31
32 The crypt() function need not be reentrant. A function that is not
33 required to be reentrant is not required to be thread-safe.
34
36 Upon successful completion, crypt() shall return a pointer to the
37 encoded string. The first two characters of the returned value shall be
38 those of the salt argument. Otherwise, it shall return a null pointer
39 and set errno to indicate the error.
40
42 The crypt() function shall fail if:
43
44 ENOSYS The functionality is not supported on this implementation.
45
46
47 The following sections are informative.
48
50 Encoding Passwords
51 The following example finds a user database entry matching a particular
52 user name and changes the current password to a new password. The
53 crypt() function generates an encoded version of each password. The
54 first call to crypt() produces an encoded version of the old password;
55 that encoded password is then compared to the password stored in the
56 user database. The second call to crypt() encodes the new password
57 before it is stored.
58
59 The putpwent() function, used in the following example, is not part of
60 IEEE Std 1003.1-2001.
61
62
63 #include <unistd.h>
64 #include <pwd.h>
65 #include <string.h>
66 #include <stdio.h>
67 ...
68 int valid_change;
69 int pfd; /* Integer for file descriptor returned by open(). */
70 FILE *fpfd; /* File pointer for use in putpwent(). */
71 struct passwd *p;
72 char user[100];
73 char oldpasswd[100];
74 char newpasswd[100];
75 char savepasswd[100];
76 ...
77 valid_change = 0;
78 while ((p = getpwent()) != NULL) {
79 /* Change entry if found. */
80 if (strcmp(p->pw_name, user) == 0) {
81 if (strcmp(p->pw_passwd, crypt(oldpasswd, p->pw_passwd)) == 0) {
82 strcpy(savepasswd, crypt(newpasswd, user));
83 p->pw_passwd = savepasswd;
84 valid_change = 1;
85 }
86 else {
87 fprintf(stderr, "Old password is not valid\n");
88 }
89 }
90 /* Put passwd entry into ptmp. */
91 putpwent(p, fpfd);
92 }
93
95 The values returned by this function need not be portable among XSI-
96 conformant systems.
97
99 None.
100
102 None.
103
105 encrypt() , setkey() , the Base Definitions volume of
106 IEEE Std 1003.1-2001, <unistd.h>
107
109 Portions of this text are reprinted and reproduced in electronic form
110 from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
111 -- Portable Operating System Interface (POSIX), The Open Group Base
112 Specifications Issue 6, Copyright (C) 2001-2003 by the Institute of
113 Electrical and Electronics Engineers, Inc and The Open Group. In the
114 event of any discrepancy between this version and the original IEEE and
115 The Open Group Standard, the original IEEE and The Open Group Standard
116 is the referee document. The original Standard can be obtained online
117 at http://www.opengroup.org/unix/online.html .
118
119
120
121IEEE/The Open Group 2003 CRYPT(P)