Another update from ingydotnet^djgoku

This commit is contained in:
Ingy döt Net 2015-11-18 06:14:39 +00:00
parent 91df62d461
commit 948b86eafa
7604 changed files with 108452 additions and 22726 deletions

View file

@ -1,7 +1,14 @@
'''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.
'''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, [[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.
{{alertbox|lightgray|'''Warning:''' SHA-1 has [https://en.wikinews.org/wiki/Chinese_researchers_crack_major_U.S._government_algorithm_used_in_digital_signatures known weaknesses]. Theoretical attacks may find a collision after [http://lwn.net/Articles/337745/ 2<sup>52</sup> operations], or perhaps fewer. This is much faster than a brute force attack of 2<sup>80</sup> operations. US government [http://csrc.nist.gov/groups/ST/hash/statement.html deprecated SHA-1]. For production-grade cryptography, users may consider a stronger alternative, such as SHA-256 (from the SHA-2 family) or the upcoming SHA-3.}}
{{alertbox|lightgray|'''Warning:''' SHA-1 has [https://en.wikinews.org/wiki/Chinese_researchers_crack_major_U.S._government_algorithm_used_in_digital_signatures known weaknesses]. Theoretical attacks may find a collision after [http://lwn.net/Articles/337745/ 2<sup>52</sup> operations], or perhaps fewer.
This is much faster than a brute force attack of 2<sup>80</sup> operations. USgovernment [http://csrc.nist.gov/groups/ST/hash/statement.html deprecated SHA-1].
For production-grade cryptography, users may consider a stronger alternative, such as SHA-256 (from the SHA-2 family) or the upcoming SHA-3.}}

View file

@ -0,0 +1,39 @@
using Nettle
function sha1sum(s::String)
bytes2hex(sha1_hash(s))
end
println("Testing SHA-1 function against FIPS 180-1")
s = sha1sum("abc")
t = lowercase("A9993E364706816ABA3E25717850C26C9CD0D89D")
print(" \"abc\" should yield ", t)
if s == t
println(", and it does.")
else
println(", but it yields ", s)
end
s = sha1sum("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq")
t = lowercase("84983E441C3BD26EBAAE4AA1F95129E5E54670F1")
print(" \"abcdbc...\" should yield ", t)
if s == t
println(", and it does.")
else
println(", but it yields ", s)
end
s = sha1sum("a"^10^6)
t = lowercase("34AA973CD4C4DAA4F61EEB2BDBAD27316534016F")
print(" a million \"a\"s should yield ", t)
if s == t
println(", and it does.")
else
println(", but it yields ", s)
end
println("\nAlso")
s = "Rosetta Code"
h = sha1sum(s)
println(" ", s, " => ", h)

View file

@ -0,0 +1,70 @@
'--------------------------------------------------------------------------------
' FAST SHA1 CALCULATION BASED ON MS ADVAPI32.DLL BY CRYPTOMAN '
' BASED ON SHA256 EXAMPLE BY RICHARD T. RUSSEL AUTHOR OF LBB '
' http://lbb.conforums.com/ '
' VERIFY CORRECTNESS BY http://www.fileformat.info/tool/hash.htm '
'--------------------------------------------------------------------------------
print sha1$("Rosetta Code")
end
X$="1234567890ABCDEF"
dat$ = pack$(X$)
print "SPEED TEST"
for i=1 to 20
t1=time$("ms")
print sha1$(dat$)
t2=time$("ms")
print "calculated in ";t2-t1;" ms"
next
end
function sha1$(message$)
HP.HASHVAL = 2
CRYPT.NEWKEYSET = 48
PROV.RSA.AES = 24
buffer$ = space$(128)
PROVRSAFULL = 1
ALGCLASSHASH = 32768
ALGTYPEANY = 0
ALGSIDMD2 = 1
ALGSIDMD4 = 2
ALGSIDMD5 = 3
ALGSIDSHA1 = 4
ALGOSHA1 = ALGCLASSHASH OR ALGTYPEANY OR ALGSIDSHA1
struct temp, v as long
open "ADVAPI32.DLL" for dll as #advapi32
calldll #advapi32, "CryptAcquireContextA", temp as struct, _
0 as long, 0 as long, PROV.RSA.AES as long, _
0 as long, re as long
hprov = temp.v.struct
calldll #advapi32, "CryptCreateHash", hprov as long, _
ALGOSHA1 as long, 0 as long, 0 as long, _
temp as struct, re as long
hhash = temp.v.struct
l = len(message$)
calldll #advapi32, "CryptHashData", hhash as long, message$ as ptr, _
l as long, 0 as long, re as long
temp.v.struct = len(buffer$)
calldll #advapi32, "CryptGetHashParam", hhash as long, _
HP.HASHVAL as long, buffer$ as ptr, _
temp as struct, 0 as long, re as long
calldll #advapi32, "CryptDestroyHash", hhash as long, re as long
calldll #advapi32, "CryptReleaseContext", hprov as long, re as long
close #advapi32
for i = 1 TO temp.v.struct
sha1$ = sha1$ + right$("0" + dechex$(asc(mid$(buffer$,i))), 2)
next
end function
function pack$(x$)
for i = 1 TO len(x$) step 2
pack$ = pack$ + chr$(hexdec(mid$(x$,i,2)))
next
end function

View file

@ -0,0 +1,17 @@
MODULE SHA1;
IMPORT
Crypto:SHA1,
Crypto:Utils,
Strings,
Out;
VAR
h: SHA1.Hash;
str: ARRAY 128 OF CHAR;
BEGIN
h := SHA1.NewHash();
h.Initialize;
str := "Rosetta Code";
h.Update(str,0,Strings.Length(str));
h.GetHash(str,0);
Out.String("SHA1: ");Utils.PrintHex(str,0,h.size);Out.Ln
END SHA1.

View file

@ -0,0 +1 @@
sha1(s)=extern("echo \"Str(`echo -n '"Str(s)"'|sha1sum|cut -d' ' -f1`)\"")

View file

@ -12,19 +12,18 @@ my \K = 0x5A827999, 0x6ED9EBA1, 0x8F1BBCDC, 0xCA62C1D6;
sub sha1-pad(Blob $msg)
{
my \bits = 8 * $msg.elems;
my @padded = $msg.list, 0x80, 0x00 xx (-($msg.elems + 1 + 8) % 64);
@padded.map({ :256[$^a,$^b,$^c,$^d] }), (bits +> 32)mod2³², (bits)mod2³²;
my @padded = flat $msg.list, 0x80, 0x00 xx (-($msg.elems + 1 + 8) % 64);
flat @padded.map({ :256[$^a,$^b,$^c,$^d] }), (bits +> 32)mod2³², (bits)mod2³²;
}
sub sha1-block(@H is rw, @M)
sub sha1-block(@H is rw, @M is copy)
{
my @W = @M;
@W.push: S(1, [+^] @W[$_ «-« <3 8 14 16>] ) for 16 .. 79;
@M.push: S(1, [+^] @M[$_ «-« <3 8 14 16>] ) for 16 .. 79;
my ($A,$B,$C,$D,$E) = @H;
for 0..79 -> \t {
($A, $B, $C, $D, $E) =
S(5,$A)f[t div 20]($B,$C,$D)$E@W[t]K[t div 20],
S(5,$A)f[t div 20]($B,$C,$D)$E@M[t]K[t div 20],
$A, S(30,$B), $C, $D;
}
@H »=« ($A,$B,$C,$D,$E);
@ -35,7 +34,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 { reverse .polymod(256 xx 3) }, @H;
Blob.new: flat map { reverse .polymod(256 xx 3) }, @H;
}
say sha1(.encode('ascii')), " $_"

View file

@ -0,0 +1,89 @@
import java.nio._
case class Hash(message: List[Byte]) {
val defaultHashes = List(0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0)
val hash = {
val padded = generatePadding(message)
val chunks: List[List[Byte]] = messageToChunks(padded)
toHashForm(hashesFromChunks(chunks))
}
def generatePadding(message: List[Byte]): List[Byte] = {
val finalPadding = BigInt(message.length * 8).toByteArray match {
case x => List.fill(8 - x.length)(0.toByte) ++ x
}
val padding = (message.length + 1) % 64 match {
case l if l < 56 =>
message ::: 0x80.toByte :: List.fill(56 - l)(0.toByte)
case l =>
message ::: 0x80.toByte :: List.fill((64 - l) + 56 + 1)(0.toByte)
}
padding ::: finalPadding
}
def toBigEndian(bytes: List[Byte]) =
ByteBuffer.wrap(bytes.toArray).getInt
def messageToChunks(message: List[Byte]) =
message.grouped(64).toList
def chunkToWords(chunk: List[Byte]) =
chunk.grouped(4).map(toBigEndian).toList
def extendWords(words: List[Int]): List[Int] = words.length match {
case i if i < 80 => extendWords(words :+ Integer.rotateLeft(
(words(i - 3) ^ words(i - 8) ^ words(i - 14) ^ words(i - 16)), 1))
case _ => words
}
def generateFK(i: Int, b: Int, c: Int, d: Int) = i match {
case i if i < 20 => (b & c | ~b & d, 0x5A827999)
case i if i < 40 => (b ^ c ^ d, 0x6ED9EBA1)
case i if i < 60 => (b & c | b & d | c & d, 0x8F1BBCDC)
case i if i < 80 => (b ^ c ^ d, 0xCA62C1D6)
}
def generateHash(words: List[Int], prevHash: List[Int]): List[Int] = {
def generateHash(i: Int, currentHashes: List[Int]): List[Int] = i match {
case i if i < 80 => currentHashes match {
case a :: b :: c :: d :: e :: Nil => {
val (f, k) = generateFK(i, b, c, d)
val x = Integer.rotateLeft(a, 5) + f + e + k + words(i)
val t = Integer.rotateLeft(b, 30)
generateHash(i + 1, x :: a :: t :: c :: d :: Nil)
}
}
case _ => currentHashes
}
addHashes(prevHash, generateHash(0, prevHash))
}
def addHashes(xs: List[Int], ys: List[Int]) = (xs, ys).zipped.map(_ + _)
def hashesFromChunks(chunks: List[List[Byte]],
remainingHash: List[Int] = defaultHashes): List[Int] =
chunks match {
case Nil => remainingHash
case x :: xs => {
val words = extendWords(chunkToWords(x))
val newHash = generateHash(words, remainingHash)
hashesFromChunks(xs, newHash)
}
}
def toHashForm(hashes: List[Int]) =
hashes.map(b => ByteBuffer.allocate(4)
.order(ByteOrder.BIG_ENDIAN).putInt(b).array.toList)
.map(bytesToHex).mkString
def bytesToHex(bytes: List[Byte]) =
(for (byte <- bytes) yield (Character.forDigit((byte >> 4) & 0xF, 16) ::
Character.forDigit((byte & 0xF), 16) :: Nil).mkString).mkString
}
object Hash extends App {
def hash(message: String) = new Hash(message.getBytes.toList).hash
println(hash("Rosetta Code"))
}

View file

@ -0,0 +1,169 @@
(define-library (lib sha1)
(export
sha1:digest)
(import (r5rs base)
(owl math) (owl list) (owl string) (owl list-extra))
(begin
; band - binary AND operation
; bor - binary OR operation
; bxor - binary XOR operation
; >>, << - binary shift operations
; runes->string - convert byte list to string /(runes->string '(65 66 67 65)) => "ABCA"/
(define (sha1-padding-size n)
(let ((x (mod (- 56 (rem n 64)) 64)))
(if (= x 0) 64 x)))
(define (sha1-pad-message message)
(let*((message-len (string-length message))
(message-len-in-bits (* message-len 8))
(buffer-len (+ message-len 8 (sha1-padding-size message-len)))
(message (string-append message (runes->string '(#b10000000))))
(zeroes-len (- buffer-len message-len 1 4)) ; for ending length encoded value
(message (string-append message (make-string zeroes-len 0)))
(message (string-append message (runes->string (list
(band (>> message-len-in-bits 24) #xFF)
(band (>> message-len-in-bits 16) #xFF)
(band (>> message-len-in-bits 8) #xFF)
(band (>> message-len-in-bits 0) #xFF))))))
; (print "message-len: " message-len)
; (print "message-len-in-bits: " message-len-in-bits)
; (print "buffer-len: " buffer-len)
; (print "zeroes-len: " zeroes-len)
; (print "message: " message)
; (print "length(message): " (string-length message))
message))
(define XOR (lambda args (fold bxor 0 args))) ; bxor more than 2 arguments
(define OR (lambda args (fold bor 0 args))) ; bor more than 2 arguments
(define NOT (lambda (arg) (bxor arg #xFFFFFFFF))) ; binary not operation
; to 32-bit number
(define (->32 i)
(band i #xFFFFFFFF))
; binary cycle rotate left
(define (rol bits x)
(->32
(bor
(<< x bits)
(>> x (- 32 bits)))))
(define (word->list x)
(list
(band (>> x 24) #xFF)
(band (>> x 16) #xFF)
(band (>> x 8) #xFF)
(band (>> x 0) #xFF)))
(define (message->words message)
(let cycle ((W
(let loop ((t (iota 0 1 16)))
(if (null? t)
null
(let*((p (* (car t) 4)))
(cons (OR
(<< (string-ref message (+ p 0)) 24)
(<< (string-ref message (+ p 1)) 16)
(<< (string-ref message (+ p 2)) 8)
(<< (string-ref message (+ p 3)) 0))
(loop (cdr t)))))))
(t 16))
(if (eq? t 80)
W
(cycle (append W (list
(XOR
(rol 1 (list-ref W (- t 3)))
(rol 1 (list-ref W (- t 8)))
(rol 1 (list-ref W (- t 14)))
(rol 1 (list-ref W (- t 16))))))
(+ t 1)))))
(define (sha1:digest message)
(let*((h0 #x67452301)
(h1 #xEFCDAB89)
(h2 #x98BADCFE)
(h3 #x10325476)
(h4 #xC3D2E1F0)
(K '(#x5A827999 #x6ED9EBA1 #x8F1BBCDC #xCA62C1D6))
(padded-message (sha1-pad-message message))
(n (/ (string-length padded-message) 64)))
(let main ((i 0)
(A h0) (B h1) (C h2) (D h3) (E h4))
(if (= i n)
(fold append null
(list (word->list A) (word->list B) (word->list C) (word->list D) (word->list E)))
(let*((message (substring padded-message (* i 64) (+ (* i 64) 64)))
(W (message->words message)))
(let*((a b c d e ; round 1
(let loop ((a A) (b B) (c C) (d D) (e E) (t 0))
(if (< t 20)
(loop (->32
(+ (rol 5 a)
(OR (band b c) (band (NOT b) d))
e
(list-ref W t)
(list-ref K 0)))
a
(rol 30 b)
c
d
(+ t 1))
(values a b c d e))))
(a b c d e ; round 2
(let loop ((a a) (b b) (c c) (d d) (e e) (t 20))
(if (< t 40)
(loop (->32
(+ (rol 5 a)
(XOR b c d)
e
(list-ref W t)
(list-ref K 1)))
a
(rol 30 b)
c
d
(+ t 1))
(values a b c d e))))
(a b c d e ; round 3
(let loop ((a a) (b b) (c c) (d d) (e e) (t 40))
(if (< t 60)
(loop (->32
(+ (rol 5 a)
(OR (band b c) (band b d) (band c d))
e
(list-ref W t)
(list-ref K 2)))
a
(rol 30 b)
c
d
(+ t 1))
(values a b c d e))))
(a b c d e ; round 4
(let loop ((a a) (b b) (c c) (d d) (e e) (t 60))
(if (< t 80)
(loop (->32
(+ (rol 5 a)
(XOR b c d)
e
(list-ref W t)
(list-ref K 3)))
a
(rol 30 b)
c
d
(+ t 1))
(values a b c d e)))))
(main (+ i 1)
(->32 (+ A a))
(->32 (+ B b))
(->32 (+ C c))
(->32 (+ D d))
(->32 (+ E e)))))))))
))

View file

@ -0,0 +1,16 @@
(import (lib sha1))
(define (->string value)
(runes->string
(let ((L "0123456789abcdef"))
(let loop ((v value))
(if (null? v) null
(cons
(string-ref L (>> (car v) 4))
(cons
(string-ref L (band (car v) #xF))
(loop (cdr v)))))))))
(print (->string (sha1:digest "Rosetta Code")))
> 48c98f7e5a6e736d790ab740dfc3f51a61abe2b5
(print (->string (sha1:digest "")))
> da39a3ee5e6b4b0d3255bfef95601890afd80709