2016 Update

This commit is contained in:
Tina Müller 2016-12-05 22:15:40 +01:00
parent 948b86eafa
commit dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions

View file

@ -1,3 +1,3 @@
'''[[wp:SHA-256|SHA-256]]''' is the recommended stronger alternative to [[SHA-1]].
'''[[wp:SHA-256|SHA-256]]''' is the recommended stronger alternative to [[SHA-1]]. See [http://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.180-4.pdf FIPS PUB 180-4] for implementation details.
Either by using a dedicated library or implementing the algorithm in your language, show that the SHA-256 digest of the string "Rosetta code" is: 764faf5c61ac315f1497f9dfa542713965b785e5cc2f707d6468d7d1124cdfcf

View file

@ -0,0 +1 @@
PrintLn( HashSHA256.HashData('Rosetta code') );

View file

@ -0,0 +1,98 @@
module sha256_m
use kernel32
use advapi32
implicit none
integer, parameter :: SHA256LEN = 32
integer(DWORD), parameter :: CALG_SHA_256 = 32780
character(*), parameter :: MS_ENH_RSA_AES_PROV = "Microsoft Enhanced RSA and AES Cryptographic Provider"C
contains
subroutine sha256hash(name, hash, dwStatus, filesize)
implicit none
character(*) :: name
integer, parameter :: BUFLEN = 32768
integer(HANDLE) :: hFile, hProv, hHash
integer(DWORD) :: dwStatus, nRead
integer(BOOL) :: status
integer(BYTE) :: buffer(BUFLEN)
integer(BYTE) :: hash(SHA256LEN)
integer(UINT64) :: filesize
dwStatus = 0
filesize = 0
hFile = CreateFile(trim(name) // char(0), GENERIC_READ, FILE_SHARE_READ, NULL, &
OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, NULL)
if (hFile == INVALID_HANDLE_VALUE) then
dwStatus = GetLastError()
print *, "CreateFile failed."
return
end if
if (CryptAcquireContext(hProv, NULL, MS_ENH_RSA_AES_PROV, PROV_RSA_AES, &
CRYPT_VERIFYCONTEXT) == FALSE) then
dwStatus = GetLastError()
print *, "CryptAcquireContext failed.", dwStatus
goto 3
end if
if (CryptCreateHash(hProv, CALG_SHA_256, 0_ULONG_PTR, 0_DWORD, hHash) == FALSE) then
dwStatus = GetLastError()
print *, "CryptCreateHash failed."
go to 2
end if
do
status = ReadFile(hFile, loc(buffer), BUFLEN, loc(nRead), NULL)
if (status == FALSE .or. nRead == 0) exit
filesize = filesize + nRead
if (CryptHashData(hHash, buffer, nRead, 0) == FALSE) then
dwStatus = GetLastError()
print *, "CryptHashData failed."
go to 1
end if
end do
if (status == FALSE) then
dwStatus = GetLastError()
print *, "ReadFile failed."
go to 1
end if
nRead = SHA256LEN
if (CryptGetHashParam(hHash, HP_HASHVAL, hash, nRead, 0) == FALSE) then
dwStatus = GetLastError()
print *, "CryptGetHashParam failed."
end if
1 status = CryptDestroyHash(hHash)
2 status = CryptReleaseContext(hProv, 0)
3 status = CloseHandle(hFile)
end subroutine
end module
program sha256
use sha256_m
implicit none
integer :: n, m, i, j
character(:), allocatable :: name
integer(DWORD) :: dwStatus
integer(BYTE) :: hash(SHA256LEN)
integer(UINT64) :: filesize
n = command_argument_count()
do i = 1, n
call get_command_argument(i, length=m)
allocate(character(m) :: name)
call get_command_argument(i, name)
call sha256hash(name, hash, dwStatus, filesize)
if (dwStatus*0 == 0) then
do j = 1, SHA256LEN
write(*, "(Z2.2)", advance="NO") hash(j)
end do
write(*, "(' ',A,' (',G0,' bytes)')") name, filesize
end if
deallocate(name)
end do
end program

View file

@ -0,0 +1,2 @@
require '~addons/ide/qt/qt.ijs'
getsha256=: 'sha256'&gethash_jqtide_

View file

@ -0,0 +1,2 @@
getsha256 'Rosetta code'
764faf5c61ac315f1497f9dfa542713965b785e5cc2f707d6468d7d1124cdfcf

View file

@ -16,7 +16,7 @@ multi sha256(Blob $data) {
constant K = init(* **(1/3))[^64];
my @b = flat $data.list, 0x80;
push @b, 0 until (8 * @b - 448) %% 512;
push @b, reverse (8 * $data).polymod(256 xx 7);
push @b, slip reverse (8 * $data).polymod(256 xx 7);
my @word = :256[@b.shift xx 4] xx @b/4;
my @H = init(&sqrt)[^8];
@ -40,5 +40,5 @@ multi sha256(Blob $data) {
}
@H [Z[m+]]= @h;
}
return Blob.new: map { reverse .polymod(256 xx 3) }, @H;
return Blob.new: map { |reverse .polymod(256 xx 3) }, @H;
}

View file

@ -0,0 +1,8 @@
a$="Rosetta code"
bit.i= 256
UseSHA2Fingerprint() : b$=StringFingerprint(a$, #PB_Cipher_SHA2, bit)
OpenConsole()
Print("[SHA2 "+Str(bit)+" bit] Text: "+a$+" ==> "+b$)
Input()