1CRYPT(3P)                  POSIX Programmer's Manual                 CRYPT(3P)
2
3
4

PROLOG

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

NAME

12       crypt - string encoding function (CRYPT)
13

SYNOPSIS

15       #include <unistd.h>
16
17       char *crypt(const char *key, const char *salt);
18
19

DESCRIPTION

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 is
25       a string chosen from the set:
26
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 characters of this string may  be  used  to  perturb  the
33       encoding 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 reentrant.  A  function  that  is  not
39       required to be reentrant is not required to be thread-safe.
40

RETURN VALUE

42       Upon  successful  completion,  crypt()  shall  return  a pointer to the
43       encoded string. The first two characters of the returned value shall be
44       those  of  the salt argument. Otherwise, it shall return a null pointer
45       and set errno to indicate the error.
46

ERRORS

48       The crypt() function shall fail if:
49
50       ENOSYS The functionality is not supported on this implementation.
51
52
53       The following sections are informative.
54

EXAMPLES

56   Encoding Passwords
57       The following example finds a user database entry matching a particular
58       user  name  and  changes  the  current  password to a new password. The
59       crypt() function generates an encoded version  of  each  password.  The
60       first  call to crypt() produces an encoded version of the old password;
61       that encoded password is then compared to the password  stored  in  the
62       user  database.  The  second  call  to crypt() encodes the new password
63       before it is stored.
64
65       The putpwent() function, used in the following example, is not part  of
66       IEEE Std 1003.1-2001.
67
68
69              #include <unistd.h>
70              #include <pwd.h>
71              #include <string.h>
72              #include <stdio.h>
73              ...
74              int valid_change;
75              int pfd;  /* Integer for file descriptor returned by open(). */
76              FILE *fpfd;  /* File pointer for use in putpwent(). */
77              struct passwd *p;
78              char user[100];
79              char oldpasswd[100];
80              char newpasswd[100];
81              char savepasswd[100];
82              ...
83              valid_change = 0;
84              while ((p = getpwent()) != NULL) {
85                  /* Change entry if found. */
86                  if (strcmp(p->pw_name, user) == 0) {
87                      if (strcmp(p->pw_passwd, crypt(oldpasswd, p->pw_passwd)) == 0) {
88                          strcpy(savepasswd, crypt(newpasswd, user));
89                          p->pw_passwd = savepasswd;
90                          valid_change = 1;
91                      }
92                      else {
93                          fprintf(stderr, "Old password is not valid\n");
94                      }
95                  }
96                  /* Put passwd entry into ptmp. */
97                  putpwent(p, fpfd);
98              }
99

APPLICATION USAGE

101       The  values  returned  by this function need not be portable among XSI-
102       conformant systems.
103

RATIONALE

105       None.
106

FUTURE DIRECTIONS

108       None.
109

SEE ALSO

111       encrypt(),    setkey(),    the    Base    Definitions     volume     of
112       IEEE Std 1003.1-2001, <unistd.h>
113
115       Portions  of  this text are reprinted and reproduced in electronic form
116       from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
117       --  Portable  Operating  System  Interface (POSIX), The Open Group Base
118       Specifications Issue 6, Copyright (C) 2001-2003  by  the  Institute  of
119       Electrical  and  Electronics  Engineers, Inc and The Open Group. In the
120       event of any discrepancy between this version and the original IEEE and
121       The  Open Group Standard, the original IEEE and The Open Group Standard
122       is the referee document. The original Standard can be  obtained  online
123       at http://www.opengroup.org/unix/online.html .
124
125
126
127IEEE/The Open Group                  2003                            CRYPT(3P)
Impressum