SHA-1: Difference between revisions
m
syntax highlighting fixup automation
No edit summary |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 16:
=={{header|AArch64 Assembly}}==
{{works with|as|Raspberry Pi 3B version Buster 64 bits}}
<syntaxhighlight lang="aarch64 assembly">
/* ARM assembly AARCH64 Raspberry PI 3B */
/* program sha1_64.s */
Line 400:
/* for this file see task include a file in language AArch64 assembly */
.include "../includeARM64.inc"
</syntaxhighlight>
{{Output}}
<pre>
Line 409:
{{works with|GNAT}}
<
with GNAT.SHA1;
Line 416:
Ada.Text_IO.Put_Line ("SHA1 (""Rosetta Code"") = " &
GNAT.SHA1.Digest ("Rosetta Code"));
end Main;</
{{out}}
Line 424:
{{works with|as|Raspberry Pi}}
<B>with openssl library </B>
<syntaxhighlight lang="arm assembly">
/* ARM assembly Raspberry PI */
Line 522:
/***************************************************/
.include "../affichage.inc"
</syntaxhighlight>
<B>with only instructions assembly ARM</B>
<syntaxhighlight lang="text">
/* ARM assembly Raspberry PI */
/* program sha1.s */
Line 875:
.include "../affichage.inc"
</syntaxhighlight>
{{Output}}
<pre>
Line 883:
=={{header|Arturo}}==
<
{{out}}
Line 890:
=={{header|Astro}}==
<
let hash = sha1.hexdigest('Ars longa, vita brevis')
print hash
</syntaxhighlight>
=={{header|AutoHotkey}}==
Source: [https://github.com/jNizM/AutoHotkey_Scripts/tree/master/Functions/Checksums SHA-1 @github] by jNizM
<
MsgBox, % "String:`n" (str) "`n`nSHA:`n" SHA(str)
Line 948:
StrPut(string, &data, floor(length / chrlength), encoding)
return CalcAddrHash(&data, length, algid, hash, hashlength)
}</
{{out}}
<pre>String: Rosetta Code
Line 956:
===Library===
{{works with|BBC BASIC for Windows}}
<
END
Line 976:
hash$ += RIGHT$("0" + STR$~buffer%?i%, 2)
NEXT
= hash$</
{{out}}
<pre>
Line 984:
===Native===
{{works with|BBC BASIC for Windows}}
<
PRINT FNsha1("Rosetta Code")
END
Line 1,085:
WHILE n# > &7FFFFFFF : n# -= 2^32 : ENDWHILE
WHILE n# < &80000000 : n# += 2^32 : ENDWHILE
= n#</
{{out}}
<pre>
Line 1,093:
=={{header|C}}==
{{libheader|OpenSSL}}
<
#include <stdlib.h>
#include <string.h>
Line 1,110:
return EXIT_SUCCESS;
}</
=={{header|C sharp}}==
Tests the built-in SHA1CryptoServiceProvider:
<
using System.Security.Cryptography;
using System.Text;
Line 1,134:
}
}
}</
=={{header|C++}}==
{{libheader|Poco}}
Compiling with <code>g++ -lPocoCrypto shaexample.cpp -o shaexample</code>:
<
#include <iostream>
#include "Poco/SHA1Engine.h"
Line 1,158:
<< " !" << std::endl ;
return 0 ;
}</
{{out}}
<pre>Rosetta Code as a sha1 digest :48c98f7e5a6e736d790ab740dfc3f51a61abe2b5 !</pre>
Line 1,176:
This example uses the [http://method-combination.net/lisp/ironclad/ Ironclad] cryptography library (available via Quicklisp as well).
<
(defun sha1-hash (data)
(let ((sha1 (ironclad:make-digest 'ironclad:sha1))
Line 1,182:
(ironclad:update-digest sha1 bin-data)
(ironclad:byte-array-to-hex-string (ironclad:produce-digest sha1))))
</syntaxhighlight>
=={{header|Crystal}}==
<
puts OpenSSL::Digest.new("sha1").update("Rosetta Code")</
{{out}}
<pre>48c98f7e5a6e736d790ab740dfc3f51a61abe2b5</pre>
Line 1,193:
'''First:''' Use native 'std.digest.sha' library
{{trans|Python}}
<
import std.stdio, std.digest.sha;
writefln("%-(%02x%)", "Ars longa, vita brevis".sha1Of);
}</
{{out}}
<pre>e640d285242886eb96ab80cbf858389b3df52f43</pre>
'''Second:''' Re-implement SHA-1 in D
<
std.range, std.digest.sha;
Line 1,282:
void main() {
writeln(sha1("Rosetta Code".dup));
}</
{{out}}
<pre>48c98f7e5a6e736d790ab740dfc3f51a61abe2b5</pre>
Line 1,288:
{{libheader| System.SysUtils}}
{{libheader| DCPsha1}} Part of '''DCPcrypt Cryptographic Component Library v2.1'''[https://bitbucket.org/wpostma/dcpcrypt2010] by David Barton.
<syntaxhighlight lang="delphi">
program Sha_1;
Line 1,318:
Writeln(SHA1('Rosetta Code'));
readln;
end.</
{{out}}
<pre>
Line 1,324:
</pre>
=={{header|DWScript}}==
<
{{out}}
Line 1,334:
Uses Erlang module 'crypto'
{{trans|Erlang}}
<
iex(1)> :crypto.hash(:sha, "A string")
<<110, 185, 174, 8, 151, 66, 9, 104, 174, 225, 10, 43, 9, 92, 82, 190, 197, 150,
224, 92>>
</syntaxhighlight>
=={{header|Emacs Lisp}}==
<
(sha1 "Rosetta Code") ;=> "48c98f7e5a6e736d790ab740dfc3f51a61abe2b5"
(secure-hash 'sha1 "Rosetta Code") ;=> "48c98f7e5a6e736d790ab740dfc3f51a61abe2b5"
</syntaxhighlight>
=={{header|Erlang}}==
Line 1,354:
=={{header|F_Sharp|F#}}==
<
let n = System.Security.Cryptography.SHA1.Create()
Array.iter (printf "%x ") (n.ComputeHash "Rosetta Code"B)
</syntaxhighlight>
{{out}}
<pre>
Line 1,378:
Using Windows API. See [https://msdn.microsoft.com/en-us/library/aa379886.aspx CryptAcquireContext], [https://msdn.microsoft.com/en-us/library/aa379908.aspx CryptCreateHash], [https://msdn.microsoft.com/en-us/library/aa380202.aspx CryptHashData] and [https://msdn.microsoft.com/en-us/library/aa379947.aspx CryptGetHashParam] on MSDN.
<
use kernel32
use advapi32
Line 1,473:
deallocate(name)
end do
end program</
=={{header|FreeBASIC}}==
<
' started with SHA-1/FIPS-180-1
' but used the BBC BASIC native version to finish.
Line 1,581:
Print : Print "hit any key to end program"
Sleep
End</
{{out}}
<pre>Rosetta Code => 48C98F7E5A6E736D790AB740DFC3F51A61ABE2B5</pre>
Line 1,588:
Frink has convenience methods to use any message hashing algorithm provided by your Java Virtual Machine. The result can be returned as a hexadecimal string, an integer, or an array of bytes.
<
{{out}}
Line 1,597:
SHA-1, being overtaken, is not recommended but is supported in GLib checksum, ''ChecksumType.SHA1''.
<
(The -1 is NUL byte terminated string indicator for length)
Line 1,604:
=={{header|Go}}==
<
import (
Line 1,615:
h.Write([]byte("Rosetta Code"))
fmt.Printf("%x\n", h.Sum(nil))
}</
{{out}}
<pre>
Line 1,622:
=={{header|Halon}}==
<
echo sha1($var);</
{{out}}
<pre>
Line 1,630:
=={{header|Hare}}==
<
use encoding::hex;
use fmt;
Line 1,645:
hex::encode(os::stdout, sum)!;
fmt::println()!;
};</
{{out}}
<pre>
Line 1,652:
=={{header|Haskell}}==
<
where
import Data.Digest.Pure.SHA
Line 1,666:
putStr "Rosetta Code SHA1-codiert: "
putStrLn $ convertToSHA1 "Rosetta Code"
</syntaxhighlight>
{{Out}}
<pre>
Line 1,673:
=={{header|Haxe}}==
<
class Main {
Line 1,680:
Sys.println(sha1);
}
}</
{{out}}
Line 1,687:
=={{header|J}}==
From J8 the <tt>ide/qt</tt> addon includes bindings to the Qt library function for a number of hash algorithms incluing SHA-1. Thus:
<
getsha1=: 'sha1'&gethash_jqtide_
getsha1 'Rosetta Code'
48c98f7e5a6e736d790ab740dfc3f51a61abe2b5</
From J8.06, the sha family of hashes have builtin support.
<
sha1'Rosetta Code'
48c98f7e5a6e736d790ab740dfc3f51a61abe2b5</
A implementation of SHA-1 in J follows:
<
f=:4 :0
Line 1,735:
)
sha1=: [:> [: process&.>/ (<H) (,~ |.) _512<\ pad</
Example use:
<
bits2hex=: '0123456789abcdef' {~ _4 #.\ ,
bits2hex sha1 text2bits 'Rosetta Code'
48c98f7e5a6e736d790ab740dfc3f51a61abe2b5</
Remember that SHA-1 is an obsolete standard (and if you *really* want high speed you'd probably be using [[wp:Application-specific_integrated_circuit|ASICs]] rather than a general purpose computing platform).
Line 1,751:
=={{header|Jsish}}==
<
var str = 'Rosetta code';
puts(Util.hash(str, {type:'sha1'}));
Line 1,759:
b18c883f4da750164b5af362ea9b9f27f90904b4
=!EXPECTEND!=
*/</
{{out}}
Line 1,770:
{{works with|Julia|0.6}}
<
testdict = Dict("abc" => "a9993e364706816aba3e25717850c26c9cd0d89d",
Line 1,781:
if length(text) > 50 text = text[1:50] * "..." end
println("# $text\n -> digest: $digest\n -> expect: $expect")
end</
{{out}}
Line 1,795:
=={{header|Kotlin}}==
<
import java.security.MessageDigest
Line 1,806:
for (byte in digest) print("%02x".format(byte))
println()
}</
{{out}}
Line 1,814:
=={{header|Lasso}}==
<
{{out}}
Line 1,820:
=={{header|Liberty BASIC}}==
<syntaxhighlight lang="lb">
'--------------------------------------------------------------------------------
' FAST SHA1 CALCULATION BASED ON MS ADVAPI32.DLL BY CRYPTOMAN '
Line 1,891:
next
end function
</syntaxhighlight>
{{Out}}
<pre>48C98F7E5A6E736D790AB740DFC3F51A61ABE2B5</pre>
Line 1,897:
=={{header|Lingo}}==
{{libheader|Crypto Xtra}}
<
put crypto.cx_sha1_string("Rosetta Code")</
{{out}}
<pre>-- "48c98f7e5a6e736d790ab740dfc3f51a61abe2b5"</pre>
=={{header|LiveCode}}==
<
local shex, sha1
put sha1Digest("Rosetta Code") into sha1
get binaryDecode("H*",sha1,shex)
put shex
end shaRosettaCode</
{{out}}
<pre>48c98f7e5a6e736d790ab740dfc3f51a61abe2b5</pre>
Line 1,917:
{{libheader|sha1}} ([https://github.com/kikito/sha1.lua luarocks install sha1])
<
local sha1 = require "sha1"
Line 1,923:
for i, str in ipairs{"Rosetta code", "Rosetta Code"} do
print(string.format("SHA-1(%q) = %s", str, sha1(str)))
end</
{{out}}
Line 1,933:
=={{header|Maple}}==
<
Hash("Ars longa, vita brevis",method="SHA1");
# "e640d285242886eb96ab80cbf858389b3df52f43"</
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang="text">Hash["Rosetta code","SHA1","HexString"]</
{{out}}
<pre>48c98f7e5a6e736d790ab740dfc3f51a61abe2b5</pre>
Line 1,945:
=={{header|min}}==
{{works with|min|0.19.6}}
<
{{out}}
<pre>
Line 1,954:
SHA-1 was added in Neko 2.2.
<syntaxhighlight lang="actionscript">/**
SHA-1 in Neko
Tectonics:
Line 1,968:
/* Output in lowercase hex */
$print(base_encode(result, "0123456789abcdef"));</
{{out}}
Line 1,977:
=={{header|NetRexx}}==
This solution is basically the same as that for [[MD5#NetRExx|MD5]], substituting "SHA-1" for "MD5" as the algorithm to use in the <tt>MessageDigest</tt> instance.
<
options replace format comments java crossref savelog symbols binary
Line 2,027:
return digestSum
</syntaxhighlight>
{{out}}
<pre>
Line 2,037:
=={{header|NewLISP}}==
<
;; (import native functions from the crypto library, provided by OpenSSL)
(module "crypto.lsp")
(crypto:sha1 "Rosetta Code")</
=={{header|Nim}}==
Nim standard library provides the module “std/sha1” to compute SHA1 digests.
<
echo secureHash("Rosetta Code")</
{{out}}
Line 2,054:
=={{header|Oberon-2}}==
{{works with|oo2c}} {{libheader|crypto}}
<
MODULE SHA1;
IMPORT
Line 2,072:
Out.String("SHA1: ");Utils.PrintHex(str,0,h.size);Out.Ln
END SHA1.
</syntaxhighlight>
{{out}}
<pre>
Line 2,083:
Using the library <code>ocaml-sha</code> in the interactive loop:
<
Objective Caml version 3.12.1
# Sha1.to_hex (Sha1.string "Rosetta Code") ;;
- : string = "48c98f7e5a6e736d790ab740dfc3f51a61abe2b5"</
=={{header|Octave}}==
Normally SHA-1 is available in the [http://octave.sourceforge.net/general/function/SHA1.html general package].
<
{{out}}
<pre>ans = 48c98f7e5a6e736d790ab740dfc3f51a61abe2b5</pre>
Line 2,099:
It works on Linux systems.
<
The code above creates a new function sha1(s) which returns SHA-1 hash of item s.
Line 2,110:
=={{header|Pascal}}==
{{works with|Free_Pascal}} {{libheader|sha1}}
<
uses
sha1;
Line 2,118:
d:=SHA1String('Rosetta Code');
WriteLn(SHA1Print(d));
end.</
{{out}}
Line 2,125:
=={{header|Perl}}==
{{libheader|Digest::SHA}}
<
print sha1_hex('Rosetta Code'), "\n";</
{{out}}
<pre>48c98f7e5a6e736d790ab740dfc3f51a61abe2b5</pre>
The same in OO manner
<
my $sha1 = Digest::SHA->new(1);
$sha1->add('Rosetta Code');
print $sha1->hexdigest, "\n";</
=={{header|Phix}}==
<!--<
<span style="color: #000080;font-style:italic;">--
-- demo\rosetta\sha1.exw
Line 2,224:
<span style="color: #0000FF;">?</span><span style="color: #000000;">sha1</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"Rosetta Code"</span><span style="color: #0000FF;">)</span>
<!--</
{{out}}
<pre>
Line 2,231:
=={{header|PHP}}==
<
$string = 'Rosetta Code';
echo sha1( $string ), "\n";
?></
{{out}}
<pre>48c98f7e5a6e736d790ab740dfc3f51a61abe2b5</pre>
Line 2,240:
=={{header|PicoLisp}}==
Library and implementation.
<
(| (mod32 (>> (- C) X)) (>> (- 32 C) X)) )
Line 2,355:
'(NIL (20)) ) ) ) ) )
(bye)</
=={{header|Pike}}==
<syntaxhighlight lang="pike">
string input = "Rosetta Code";
string out = Crypto.SHA1.hash(input);
write( String.string2hex(out) +"\n");
</syntaxhighlight>
{{Out}}
<pre>48c98f7e5a6e736d790ab740dfc3f51a61abe2b5</pre>
=={{header|PowerShell}}==
<syntaxhighlight lang="powershell">
Function Calculate-SHA1( $String ){
$Enc = [system.Text.Encoding]::UTF8
Line 2,379:
[System.Convert]::ToBase64String($Result)
}
</syntaxhighlight>
taken from [http://stackoverflow.com/questions/8051713/convert-a-string-to-a-byte-array-in-powershell-version-2 Stackoverflow] with a little modification
=={{header|PureBasic}}==
PB Version 5.40
<
UseSHA1Fingerprint() : b$=StringFingerprint(a$, #PB_Cipher_SHA1)
Line 2,390:
OpenConsole()
Print("[SHA1] Text: "+a$+" ==> "+b$)
Input()</
{{out}}
<pre>[SHA1] Text: Rosetta Code ==> 48c98f7e5a6e736d790ab740dfc3f51a61abe2b5</pre>
=={{header|Python}}==
<
h = hashlib.sha1()
h.update(bytes("Ars longa, vita brevis", encoding="ASCII"))
h.hexdigest()
# "e640d285242886eb96ab80cbf858389b3df52f43"</
=={{header|R}}==
<
library(digest)
input <- "Rosetta Code"
cat(digest(input, algo = "sha1", serialize = FALSE), "\n")
</syntaxhighlight>
{{out}}
<pre>
Line 2,417:
With the built-in <tt>file/sha1</tt> library:
<
#lang racket
(require file/sha1)
(sha1 (open-input-string "Rosetta Code"))
</syntaxhighlight>
With the faster <tt>openssl/sha1</tt> library (requires OpenSSL to be installed):
<
#lang racket
(require openssl/sha1)
(sha1 (open-input-string "Rosetta Code"))
</syntaxhighlight>
=={{header|Raku}}==
Line 2,436:
A pure Raku implementation that closely follows the description of SHA-1 in FIPS 180-1. Slow.
<syntaxhighlight lang="raku"
sub infix:<⊕> { ($^x + $^y)mod2³² }
sub S { ($^x +< $^n)mod2³² +| ($x +> (32-$n)) }
Line 2,479:
'abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq',
'Rosetta Code',
'Ars longa, vita brevis';</
{{Out}}
<pre>Buf:0x<a9 99 3e 36 47 06 81 6a ba 3e 25 71 78 50 c2 6c 9c d0 d8 9d> abc
Line 2,488:
===Library based implementation===
Quite speedy.
<syntaxhighlight lang="raku"
# use sha1-hex() if you want a hex string
Line 2,497:
'Rosetta Code',
'Ars longa, vita brevis'
;</
{{Out}}
<pre>Blob:0x<A9 99 3E 36 47 06 81 6A BA 3E 25 71 78 50 C2 6C 9C D0 D8 9D> abc
Line 2,505:
=={{header|Ring}}==
<
# Project : SHA-1
Line 2,513:
see "SHA-1: "
see sha1(str) + nl
</syntaxhighlight>
Output:
<pre>
Line 2,524:
'''First:''' Use 'digest' from Ruby's standard library.
<
puts Digest::SHA1.hexdigest('Rosetta Code')</
'''Second:''' Use 'openssl' from Ruby's standard library. {{libheader|OpenSSL}}
<
puts OpenSSL::Digest::SHA1.hexdigest('Rosetta Code')</
'''Third:''' Reimplement SHA-1 in Ruby.
<
# Calculates SHA-1 message digest of _string_. Returns binary digest.
Line 2,598:
'Rosetta Code',
].each {|s| printf("%s:\n %s\n", s, *sha1(s).unpack('H*'))}
end</
{{out}}
<pre>
Line 2,611:
=={{header|Rust}}==
Using sha1 crate: https://docs.rs/sha1/0.6.0/sha1/
<
fn main() {
Line 2,618:
println!("{}", hash_msg.digest().to_string());
}
</syntaxhighlight>
Output
<pre>
Line 2,627:
Support for MD5 and SHA-1 are included in the standard "chksum" library:
<
print(sha1sum("Rosetta Code"));</
{{out}}
Line 2,637:
The solution to this task would be a small modification to [[MD5#Scala|MD5]] (replacing "MD5" with "SHA-1" as noted [http://docs.oracle.com/javase/7/docs/technotes/guides/security/StandardNames.html#MessageDigest here]).
<
import java.nio._
Line 2,727:
println(hash("Rosetta Code"))
}
</syntaxhighlight>
{{out}}
Line 2,735:
{{works with|Ol}}
<
; band - binary AND operation
; bor - binary OR operation
Line 2,896:
(->32 (+ D d))
(->32 (+ E e)))))))))
</syntaxhighlight>
{{out}}
<
(define (->string value)
(runes->string
Line 2,915:
(print (->string (sha1:digest "")))
> da39a3ee5e6b4b0d3255bfef95601890afd80709
</syntaxhighlight>
=={{header|Seed7}}==
<
include "msgdigest.s7i";
Line 2,924:
begin
writeln(hex(sha1("Rosetta Code")));
end func;</
{{out}}
Line 2,932:
=={{header|Sidef}}==
<
say sha.sha1_hex('Rosetta Code');</
{{out}}
<pre>48c98f7e5a6e736d790ab740dfc3f51a61abe2b5</pre>
Line 2,939:
=={{header|Smalltalk}}==
{{works with|GNU Smalltalk}}
<
(SHA1 hexDigestOf: 'Rosetta Code') displayNl.</
{{works with|Smalltalk/X}}
<
=={{header|Tcl}}==
{{tcllib|sha1}}
<
puts [sha1::sha1 "Rosetta Code"]</
{{out}}
<pre>48c98f7e5a6e736d790ab740dfc3f51a61abe2b5</pre>
Line 2,954:
=={{header|UNIX Shell}}==
{{works with|OpenBSD|2.2 [http://www.openbsd.org/cgi-bin/cvsweb/src/bin/md5/Makefile (link)]}}
<
9e9aeefe5563845ec5c42c5630842048c0fc261b</
{{libheader|OpenSSL}}
<
9e9aeefe5563845ec5c42c5630842048c0fc261b</
=={{header|Vlang}}==
<
fn main() {
Line 2,970:
h.write('Rosetta Code'.bytes()) ?
println('${h.checksum().map(it.hex()).join('')}')
}</
{{out}}
<pre>
Line 2,980:
{{libheader|Wren-crypto}}
{{libheader|Wren-fmt}}
<
import "/fmt" for Fmt
Line 2,999:
var hash = Sha1.digest(s)
Fmt.print("$s <== '$0s'", hash, s)
}</
{{out}}
Line 3,017:
=={{header|zkl}}==
Using zklMsgHash so. Can return the hash as a string of hex digits or bytes, can hash the hash N times.
<
zkl: var MsgHash=Import("zklMsgHash")
MsgHash
Line 3,031:
zkl: MsgHash.SHA1("a"*1000,1000); // hash 1000 a's 1000 times
34aa973cd4c4daa4f61eeb2bdbad27316534016f</
|