Browse Source

* added new unit unixcrypt.pas. With this unit you can generate and validate unix password hashes

git-svn-id: trunk@5452 -
ivost 18 years ago
parent
commit
9b8a570db0
4 changed files with 70 additions and 8 deletions
  1. 1 0
      .gitattributes
  2. 8 8
      packages/base/hash/Makefile
  3. 1 0
      packages/base/hash/Makefile.fpc
  4. 60 0
      packages/base/hash/unixcrypt.pas

+ 1 - 0
.gitattributes

@@ -1397,6 +1397,7 @@ packages/base/hash/md5.pp svneol=native#text/plain
 packages/base/hash/md5.ref -text
 packages/base/hash/md5test.pp svneol=native#text/plain
 packages/base/hash/ntlm.pas svneol=native#text/plain
+packages/base/hash/unixcrypt.pas -text
 packages/base/httpd/Makefile svneol=native#text/plain
 packages/base/httpd/Makefile.fpc svneol=native#text/plain
 packages/base/httpd/examples/Makefile svneol=native#text/plain

+ 8 - 8
packages/base/hash/Makefile

@@ -1,5 +1,5 @@
 #
-# Don't edit, this file is generated by FPCMake Version 2.0.0 [2006/10/13]
+# Don't edit, this file is generated by FPCMake Version 2.0.0 [2006/11/20]
 #
 default: all
 MAKEFILETARGETS=i386-linux i386-go32v2 i386-win32 i386-os2 i386-freebsd i386-beos i386-netbsd i386-solaris i386-qnx i386-netware i386-openbsd i386-wdosx i386-darwin i386-emx i386-watcom i386-netwlibc i386-wince m68k-linux m68k-freebsd m68k-netbsd m68k-amiga m68k-atari m68k-openbsd m68k-palmos powerpc-linux powerpc-netbsd powerpc-amiga powerpc-macos powerpc-darwin powerpc-morphos sparc-linux sparc-netbsd sparc-solaris x86_64-linux x86_64-freebsd x86_64-win64 arm-linux arm-palmos arm-wince arm-gba powerpc64-linux
@@ -233,7 +233,7 @@ PACKAGESDIR:=$(wildcard $(FPCDIR) $(FPCDIR)/packages/base $(FPCDIR)/packages/ext
 override PACKAGE_NAME=hash
 override PACKAGE_VERSION=2.0.0
 ifeq ($(FULL_TARGET),i386-linux)
-override TARGET_UNITS+=md5 crc ntlm
+override TARGET_UNITS+=md5 crc ntlm  unixcrypt
 endif
 ifeq ($(FULL_TARGET),i386-go32v2)
 override TARGET_UNITS+=md5 crc ntlm
@@ -284,7 +284,7 @@ ifeq ($(FULL_TARGET),i386-wince)
 override TARGET_UNITS+=md5 crc ntlm
 endif
 ifeq ($(FULL_TARGET),m68k-linux)
-override TARGET_UNITS+=md5 crc ntlm
+override TARGET_UNITS+=md5 crc ntlm  unixcrypt
 endif
 ifeq ($(FULL_TARGET),m68k-freebsd)
 override TARGET_UNITS+=md5 crc ntlm
@@ -305,7 +305,7 @@ ifeq ($(FULL_TARGET),m68k-palmos)
 override TARGET_UNITS+=md5 crc ntlm
 endif
 ifeq ($(FULL_TARGET),powerpc-linux)
-override TARGET_UNITS+=md5 crc ntlm
+override TARGET_UNITS+=md5 crc ntlm  unixcrypt
 endif
 ifeq ($(FULL_TARGET),powerpc-netbsd)
 override TARGET_UNITS+=md5 crc ntlm
@@ -323,7 +323,7 @@ ifeq ($(FULL_TARGET),powerpc-morphos)
 override TARGET_UNITS+=md5 crc ntlm
 endif
 ifeq ($(FULL_TARGET),sparc-linux)
-override TARGET_UNITS+=md5 crc ntlm
+override TARGET_UNITS+=md5 crc ntlm  unixcrypt
 endif
 ifeq ($(FULL_TARGET),sparc-netbsd)
 override TARGET_UNITS+=md5 crc ntlm
@@ -332,7 +332,7 @@ ifeq ($(FULL_TARGET),sparc-solaris)
 override TARGET_UNITS+=md5 crc ntlm
 endif
 ifeq ($(FULL_TARGET),x86_64-linux)
-override TARGET_UNITS+=md5 crc ntlm
+override TARGET_UNITS+=md5 crc ntlm  unixcrypt
 endif
 ifeq ($(FULL_TARGET),x86_64-freebsd)
 override TARGET_UNITS+=md5 crc ntlm
@@ -341,7 +341,7 @@ ifeq ($(FULL_TARGET),x86_64-win64)
 override TARGET_UNITS+=md5 crc ntlm
 endif
 ifeq ($(FULL_TARGET),arm-linux)
-override TARGET_UNITS+=md5 crc ntlm
+override TARGET_UNITS+=md5 crc ntlm  unixcrypt
 endif
 ifeq ($(FULL_TARGET),arm-palmos)
 override TARGET_UNITS+=md5 crc ntlm
@@ -353,7 +353,7 @@ ifeq ($(FULL_TARGET),arm-gba)
 override TARGET_UNITS+=md5 crc ntlm
 endif
 ifeq ($(FULL_TARGET),powerpc64-linux)
-override TARGET_UNITS+=md5 crc ntlm
+override TARGET_UNITS+=md5 crc ntlm  unixcrypt
 endif
 ifeq ($(FULL_TARGET),i386-linux)
 override TARGET_EXAMPLES+=md5test

+ 1 - 0
packages/base/hash/Makefile.fpc

@@ -8,6 +8,7 @@ version=2.0.0
 
 [target]
 units=md5 crc ntlm
+units_linux=unixcrypt
 examples=md5test
 
 [install]

+ 60 - 0
packages/base/hash/unixcrypt.pas

@@ -0,0 +1,60 @@
+unit unixcrypt;
+
+{$mode objfpc}
+{$linklib crypt}
+{$H+}
+
+interface
+
+uses
+  ctypes;
+
+function crypt(const key: pchar; const salt: pchar): pchar; cdecl; external;
+
+// salt helper functions
+function gen_des_salt: string;
+function gen_md5_salt: string;
+
+// crypt helper functions
+function crypt_password(const key: string; const UseMD5: boolean): string;
+function validate_password(const key: string; const hash: string): boolean;
+
+implementation
+
+const
+  salt_chars: array[0..63] of char = (
+    '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',
+    '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',
+    '0','1','2','3','4','5','6','7','8','9','.','/');
+
+function gen_des_salt: string;
+begin
+  Result := salt_chars[Random(64)] + salt_chars[Random(64)];
+end;
+
+function gen_md5_salt: string;
+var
+  i: integer;
+begin
+  Result := '$1$';
+  for i := 0 to 7 do
+    Result := Result + salt_chars[Random(64)];
+end;
+
+function crypt_password(const key: string; const UseMD5: boolean): string;
+begin
+  if UseMD5 then
+    Result := crypt(pchar(key), pchar(gen_md5_salt)) else
+    Result := crypt(pchar(key), pchar(gen_des_salt));
+end;
+
+function validate_password(const key: string; const hash: string): boolean;
+begin
+  Result :=
+  // MD5 compare
+    ((Length(hash) = 34) and (hash[1] = '$') and (hash[2] = '1') and (hash[3] = '$') and (hash[12] = '$') and (crypt(pchar(key), pchar(copy(hash, 1, 11))) = hash)) or
+  // DES compare
+    ((Length(hash) = 13) and (crypt(pchar(key), pchar(copy(hash, 1, 2))) = hash));
+end;
+
+end.