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, FIPS 180-1, defines SHA-1.

Find the SHA-1 message digest for a string of octets. You may either call a SHA-1 library, or implement SHA-1 in your language. Both approaches interest Rosetta Code.



 Warning: SHA-1 has known weaknesses. Theoretical attacks may find a collision after 252 operations, or perhaps fewer.
This is much faster than a brute force attack of 280 operations. USgovernment 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.


 
'--------------------------------------------------------------------------------
'           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
 