Just another update

This commit is contained in:
Ingy döt Net 2015-02-20 00:35:01 -05:00
parent a25938f123
commit 00a190b0a6
6591 changed files with 94363 additions and 23227 deletions

View file

@ -1,5 +1,5 @@
import std.stdio, std.digest.sha;
void main() {
writefln("%-(%02x%)", "Ars longa, vita brevis".sha1Of());
import std.stdio, std.digest.sha;
writefln("%-(%02x%)", "Ars longa, vita brevis".sha1Of);
}

View file

@ -0,0 +1,14 @@
module Digestor
where
import Data.Digest.Pure.SHA
import qualified Data.ByteString.Lazy as B
convertString :: String -> B.ByteString
convertString phrase = B.pack $ map ( fromIntegral . fromEnum ) phrase
convertToSHA1 :: String -> String
convertToSHA1 word = showDigest $ sha1 $ convertString word
main = do
putStr "Rosetta Code SHA1-codiert: "
putStrLn $ convertToSHA1 "Rosetta Code"

View file

@ -17,10 +17,11 @@ 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
H=: #: 16b67452301 16befcdab89 16b98badcfe 16b10325476 16bc3d2e1f0
process=:4 :0
W=. (, [: , 1 |."#. _3 _8 _14 _16 ~:/@:{ ])^:64 x ]\~ _32
'A B C D E'=. y=._32[\,y
for_t. i.80 do.
TEMP=. (5|.A) plus (t f B,C,D) plus E plus (W{~t) plus K t
E=. D
@ -29,7 +30,7 @@ process=:3 :0
B=. A
A=. TEMP
end.
,H plus A,B,C,D,:E
,y plus A,B,C,D,:E
)
sha1=: _512 process\ pad
sha1=: [:> [: process&.>/ (<H) (,~ |.) _512<\ pad

View file

@ -0,0 +1,4 @@
;; using the crypto module from http://www.newlisp.org/code/modules/crypto.lsp.html
;; (import native functions from the crypto library, provided by OpenSSL)
(module "crypto.lsp")
(crypto:sha1 "Rosetta Code")

View file

@ -0,0 +1,9 @@
program RosettaSha1;
uses
sha1;
var
d: TSHA1Digest;
begin
d:=SHA1String('Rosetta Code');
WriteLn(SHA1Print(d));
end.

View file

@ -35,7 +35,7 @@ sub sha1(Blob $msg) returns Blob
my @M = sha1-pad($msg);
my @H = 0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0;
sha1-block(@H,@M[$_..$_+15]) for 0, 16...^ +@M;
Blob.new: map -> $w is rw { reverse gather for ^4 { take $w % 256; $w div= 256 } }, @H;
Blob.new: map { reverse .polymod(256 xx 3) }, @H;
}
say sha1(.encode('ascii')), " $_"

View file

@ -1,4 +1,116 @@
(de leftRotate (X C)
(| (mod32 (>> (- C) X)) (>> (- 32 C) X)) )
(de mod32 (N)
(& N `(hex "FFFFFFFF")) )
(de not32 (N)
(x| N `(hex "FFFFFFFF")) )
(de add32 @
(mod32 (pass +)) )
(de sha1 (Str)
(let Len (length Str)
(setq Str
(conc
(need
(-
8
(* 64 (/ (+ Len 1 8 63) 64)) )
(conc
(mapcar char (chop Str))
(cons `(hex "80")) )
0 )
(flip
(make
(setq Len (* 8 Len))
(do 8
(link (& Len 255))
(setq Len (>> 8 Len )) ) ) ) ) ) )
(let
(H0 `(hex "67452301")
H1 `(hex "EFCDAB89")
H2 `(hex "98BADCFE")
H3 `(hex "10325476")
H4 `(hex "C3D2E1F0") )
(while Str
(let
(A H0 B H1 C H2 D H3 E H4
W (conc
(make
(do 16
(link
(apply
|
(mapcar >> (-24 -16 -8 0) (cut 4 'Str)) ) ) ) )
(need 64 0) ) )
(for (I 17 (>= 80 I) (inc I))
(set (nth W I)
(leftRotate
(x|
(get W (- I 3))
(get W (- I 8))
(get W (- I 14))
(get W (- I 16)) )
1 ) ) )
(use (Tmp F K)
(for I 80
(cond
((>= 20 I)
(setq
F (| (& B C) (& (not32 B) D))
K `(hex "5A827999") ) )
((>= 40 I)
(setq
F (x| B C D)
K `(hex "6ED9EBA1") ) )
((>= 60 I)
(setq
F (| (& B C) (& B D) (& C D))
K `(hex "8F1BBCDC") ) )
(T
(setq
F (x| B C D)
K `(hex "CA62C1D6") ) ) )
(setq
Tmp (add32 (leftRotate A 5) F E K (get W I) )
E D
D C
C (leftRotate B 30)
B A
A Tmp ) ) )
(setq
H0 (add32 H0 A)
H1 (add32 H1 B)
H2 (add32 H2 C)
H3 (add32 H3 D)
H4 (add32 H4 E) ) ) )
(mapcan
'((N)
(flip
(make
(do 4
(link (& 255 N))
(setq N (>> 8 N)) ) ) ) )
(list H0 H1 H2 H3 H4) ) ) )
(let Str "Rosetta Code"
(pack
(mapcar '((B) (pad 2 (hex B)))
(native "libcrypto.so" "SHA1" '(B . 20) Str (length Str) '(NIL (20))) ) ) )
(println
(pack
(mapcar
'((B) (pad 2 (hex B)))
(sha1 Str) ) ) )
(println
(pack
(mapcar
'((B) (pad 2 (hex B)))
(native
"libcrypto.so"
"SHA1"
'(B . 20)
Str
(length Str)
'(NIL (20)) ) ) ) ) )
(bye)

View file

@ -8,7 +8,7 @@ require 'stringio'
#++
def sha1(string)
# functions and constants
mask = (1 << 32) - 1 # ffffffff
mask = 0xffffffff
s = proc{|n, x| ((x << n) & mask) | (x >> (32 - n))}
f = [
proc {|b, c, d| (b & c) | (b.^(mask) & d)},
@ -21,41 +21,22 @@ def sha1(string)
# initial hash
h = [0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0]
bit_len = string.size << 3
string += "\x80"
while (string.size % 64) != 56
string += "\0"
end
string = string.force_encoding('ascii-8bit') + [bit_len >> 32, bit_len & mask].pack("N2")
if string.size % 64 != 0
fail "failed to pad to correct length"
end
io = StringIO.new(string)
block = ""
term = false # appended "\x80" in second-last block?
last = false # last block?
until last
# Read next block of 16 words (64 bytes, 512 bits).
io.read(64, block) or (
# Work around a bug in Rubinius 1.2.4. At eof,
# MRI and JRuby already replace block with "".
block.clear
)
# Unpack block into 32-bit words "N".
case len = block.length
when 64
# Unpack 16 words.
w = block.unpack("N16")
when 56..63
# Second-last block: append padding, unpack 16 words.
block.concat("\x80"); term = true
block.concat("\0" * (63 - len))
w = block.unpack("N16")
when 0..55
# Last block: append padding, unpack 14 words.
block.concat(term ? "\0" : "\x80")
block.concat("\0" * (55 - len))
w = block.unpack("N14")
# Append bit length, 2 words.
bit_len = string.length << 3
w.push(bit_len >> 32, bit_len & mask)
last = true
else
fail "impossible"
end
while io.read(64, block)
w = block.unpack("N16")
# Process block.
(16..79).each {|t| w[t] = s[1, w[t-3] ^ w[t-8] ^ w[t-14] ^ w[t-16]]}