SHA-1: Difference between revisions
Content added Content deleted
Line 374: | Line 374: | ||
Using Windows API. See [https://msdn.microsoft.com/en-us/library/aa379886.aspx CryptAcquireContext], [https://msdn.microsoft.com/en-us/library/aa379908.aspx CryptCreateHash], [https://msdn.microsoft.com/en-us/library/aa380202.aspx CryptHashData] and [https://msdn.microsoft.com/en-us/library/aa379947.aspx CryptGetHashParam] on MSDN. |
Using Windows API. See [https://msdn.microsoft.com/en-us/library/aa379886.aspx CryptAcquireContext], [https://msdn.microsoft.com/en-us/library/aa379908.aspx CryptCreateHash], [https://msdn.microsoft.com/en-us/library/aa380202.aspx CryptHashData] and [https://msdn.microsoft.com/en-us/library/aa379947.aspx CryptGetHashParam] on MSDN. |
||
<lang fortran>module |
<lang fortran>module sha1_mod |
||
use kernel32 |
use kernel32 |
||
use advapi32 |
use advapi32 |
||
Line 390: | Line 390: | ||
integer(BYTE) :: hash(SHA1LEN) |
integer(BYTE) :: hash(SHA1LEN) |
||
integer(UINT64) :: filesize |
integer(UINT64) :: filesize |
||
dwStatus = 0 |
dwStatus = 0 |
||
filesize = 0 |
filesize = 0 |
||
hFile = CreateFile(trim(name) // char(0), GENERIC_READ, FILE_SHARE_READ, NULL, & |
hFile = CreateFile(trim(name) // char(0), GENERIC_READ, FILE_SHARE_READ, NULL, & |
||
OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, NULL) |
OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, NULL) |
||
if (hFile == INVALID_HANDLE_VALUE) then |
if (hFile == INVALID_HANDLE_VALUE) then |
||
dwStatus = GetLastError() |
dwStatus = GetLastError() |
||
Line 401: | Line 401: | ||
return |
return |
||
end if |
end if |
||
if (CryptAcquireContext(hProv, NULL, NULL, PROV_RSA_FULL, & |
if (CryptAcquireContext(hProv, NULL, NULL, PROV_RSA_FULL, & |
||
CRYPT_VERIFYCONTEXT) == FALSE) then |
CRYPT_VERIFYCONTEXT) == FALSE) then |
||
dwStatus = GetLastError() |
dwStatus = GetLastError() |
||
print *, "CryptAcquireContext failed." |
print *, "CryptAcquireContext failed." |
||
goto 3 |
goto 3 |
||
end if |
end if |
||
if (CryptCreateHash(hProv, CALG_SHA1, 0_ULONG_PTR, 0_DWORD, hHash) == FALSE) then |
if (CryptCreateHash(hProv, CALG_SHA1, 0_ULONG_PTR, 0_DWORD, hHash) == FALSE) then |
||
dwStatus = GetLastError() |
dwStatus = GetLastError() |
||
print *, "CryptCreateHash failed." |
print *, "CryptCreateHash failed." |
||
go to 2 |
go to 2 |
||
end if |
end if |
||
do |
do |
||
status = ReadFile(hFile, loc(buffer), BUFLEN, |
status = ReadFile(hFile, loc(buffer), BUFLEN, nRead, NULL) |
||
if (status == FALSE .or. nRead == 0) exit |
if (status == FALSE .or. nRead == 0) exit |
||
filesize = filesize + nRead |
filesize = filesize + nRead |
||
Line 427: | Line 427: | ||
end if |
end if |
||
end do |
end do |
||
if (status == FALSE) then |
if (status == FALSE) then |
||
dwStatus = GetLastError() |
dwStatus = GetLastError() |
||
Line 433: | Line 433: | ||
go to 1 |
go to 1 |
||
end if |
end if |
||
nRead = SHA1LEN |
nRead = SHA1LEN |
||
if (CryptGetHashParam(hHash, HP_HASHVAL, hash, nRead, 0) == FALSE) then |
if (CryptGetHashParam(hHash, HP_HASHVAL, hash, nRead, 0) == FALSE) then |
||
Line 439: | Line 439: | ||
print *, "CryptGetHashParam failed.", status, nRead, dwStatus |
print *, "CryptGetHashParam failed.", status, nRead, dwStatus |
||
end if |
end if |
||
1 status = CryptDestroyHash(hHash) |
1 status = CryptDestroyHash(hHash) |
||
2 status = CryptReleaseContext(hProv, 0) |
2 status = CryptReleaseContext(hProv, 0) |
||
Line 445: | Line 445: | ||
end subroutine |
end subroutine |
||
end module |
end module |
||
program sha1 |
program sha1 |
||
use |
use sha1_mod |
||
implicit none |
implicit none |
||
integer :: n, m, i, j |
integer :: n, m, i, j |
||
Line 454: | Line 454: | ||
integer(BYTE) :: hash(SHA1LEN) |
integer(BYTE) :: hash(SHA1LEN) |
||
integer(UINT64) :: filesize |
integer(UINT64) :: filesize |
||
n = command_argument_count() |
n = command_argument_count() |
||
do i = 1, n |
do i = 1, n |