March 2014 update

This commit is contained in:
Ingy döt Net 2014-04-02 16:56:35 +00:00
parent 09687c4926
commit a25938f123
1846 changed files with 21876 additions and 5203 deletions

View file

@ -1,6 +1,6 @@
'''SHA-1''' or '''SHA1''' is a one-way hash function; it computes a 160-bit message digest. SHA-1 often appears in security protocols; for example, many HTTPS websites use RSA with SHA-1 to secure their connections. BitTorrent uses SHA-1 to verify downloads. Git and Mercurial use SHA-1 digests to identify commits.
A US government standard, [http://www.itl.nist.gov/fipspubs/fip180-1.htm FIPS 180-1], defines SHA-1.
A US government standard, [[SHA-1/FIPS-180-1|FIPS 180-1]], defines SHA-1.
Find the SHA-1 message digest for a string of [[octet]]s. You may either call a SHA-1 library, or implement SHA-1 in your language. Both approaches interest Rosetta Code.

35
Task/SHA-1/J/sha-1-1.j Normal file
View file

@ -0,0 +1,35 @@
pad=: ,1,(0#~512 | [: - 65 + #),(64#2)#:#
f=:4 :0
'B C D'=: _32 ]\ y
if. x < 20 do.
(B*C)+.D>B
elseif. x < 40 do.
B~:C~:D
elseif. x < 60 do.
(B*C)+.(B*D)+.C*D
elseif. x < 80 do.
B~:C~:D
end.
)
K=: ((32#2) #: 16b5a827999 16b6ed9eba1 16b8f1bbcdc 16bca62c1d6) {~ <.@%&20
plus=:+&.((32#2)&#.)
process=:3 :0
H=. #: 16b67452301 16befcdab89 16b98badcfe 16b10325476 16bc3d2e1f0
W=. (, [: , 1 |."#. _3 _8 _14 _16 ~:/@:{ ])^:64 y ]\~ _32
'A B C D E'=. H
for_t. i.80 do.
TEMP=. (5|.A) plus (t f B,C,D) plus E plus (W{~t) plus K t
E=. D
D=. C
C=. 30 |. B
B=. A
A=. TEMP
end.
,H plus A,B,C,D,:E
)
sha1=: _512 process\ pad

5
Task/SHA-1/J/sha-1-2.j Normal file
View file

@ -0,0 +1,5 @@
text2bits=: (8#2) ,@:#: a. i. ]
bits2hex=: '0123456789abcdef' {~ _4 #.\ ,
bits2hex sha1 text2bits 'Rosetta Code'
48c98f7e5a6e736d790ab740dfc3f51a61abe2b5

42
Task/SHA-1/J/sha-1-3.j Normal file
View file

@ -0,0 +1,42 @@
text2bits=: (8#2) ,@:#: a. i. ]
words2bits=: ,@:((32#2)&#:)
pad=: _32 #.\ (, 1,(0#~512 | [: - 65 + #),(64#2)#:#)
lim32=: 2^32
rot32=: (] lim32&|@* 2 ^ [) + ] <.@% 2 ^ 32- [
and=: 17 b.
notimplies=: 18 b.
xor=: 22 b.
or=: 23 b.
f=:4 :0
'B C D'=: y
if. x < 20 do.
(B and C) or D notimplies B
elseif. x < 40 do.
B xor C xor D
elseif. x < 60 do.
(B and C) or (B and D) or C and D
elseif. x < 80 do.
B xor C xor D
end.
)
K=: 16b5a827999 16b6ed9eba1 16b8f1bbcdc 16bca62c1d6 {~ <.@%&20
process=:3 :0
H=. 16b67452301 16befcdab89 16b98badcfe 16b10325476 16bc3d2e1f0
W=. (, [: , 1 rot32 _3 _8 _14 _16 xor/@:{ ])^:64 y
'A B C D E'=. H
for_t. i.80 do.
TEMP=. lim32|(5 rot32 A) + (t f B,C,D) + E + (W{~t) + K t
E=. D
D=. C
C=. 30 rot32 B
B=. A
A=. TEMP
end.
,lim32|H + A,B,C,D,E
)
sha1=: _16 process\ pad

8
Task/SHA-1/J/sha-1-4.j Normal file
View file

@ -0,0 +1,8 @@
words2hex=: '0123456789abcdef' {~ (8#16)&#:
words2hex sha1 text2bits 'Rosetta Code'
48c98f7e
5a6e736d
790ab740
dfc3f51a
61abe2b5

7
Task/SHA-1/Lua/sha-1.lua Normal file
View file

@ -0,0 +1,7 @@
#!/usr/bin/lua
local sha1 = require "sha1"
for i, str in ipairs{"Rosetta code", "Rosetta Code"} do
print(string.format("SHA-1(%q) = %s", str, sha1(str)))
end

View file

@ -0,0 +1 @@
sprintf("%02x", SHA1(+"Rosetta Code"(:)))

View file

@ -0,0 +1,11 @@
Function Calculate-SHA1( $String ){
$Enc = [system.Text.Encoding]::UTF8
$Data = $enc.GetBytes($String)
# Create a New SHA1 Crypto Provider
$Sha = New-Object System.Security.Cryptography.SHA1CryptoServiceProvider
# Now hash and display results
$Result = $sha.ComputeHash($Data)
[System.Convert]::ToBase64String($Result)
}