SHA-1: Difference between revisions
m
→{{header|Wren}}: Minor tidy
(SHA-1: Add Emacs Lisp example) |
m (→{{header|Wren}}: Minor tidy) |
||
(9 intermediate revisions by 6 users not shown) | |||
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>
===Without external libraries===
<syntaxhighlight lang="c++">
#include <bit>
#include <cstdint>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
class SHA1 {
public:
std::string message_digest(const std::string& message) {
std::vector<uint32_t> state = { 0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0 };
const std::vector<int8_t> bytes = add_padding(message);
for ( uint64_t i = 0; i < bytes.size() / BLOCK_LENGTH; ++i ) {
std::vector<uint32_t> values(80, 0);
for ( uint32_t j = 0; j < BLOCK_LENGTH; ++j ) {
values[j / 4] |= ( bytes[i * BLOCK_LENGTH + j] & 0xff ) << ( ( 3 - j % 4 ) * 8 );
}
for ( uint32_t j = 16; j < 80; ++j ) {
uint32_t value = values[j - 3] ^ values[j - 8] ^ values[j - 14] ^ values[j - 16];
values[j] = std::rotl(value, 1);
}
uint32_t a = state[0], b = state[1], c = state[2], d = state[3], e = state[4];
uint32_t f = 0, k = 0;
for ( uint32_t j = 0; j < 80; ++j ) {
switch ( j / 20 ) {
case 0 : { f = ( b & c ) | ( ~b & d ); k = 0x5a827999; break; }
case 1 : { f = b ^ c ^ d; k = 0x6ed9eba1; break; }
case 2 : { f = ( b & c ) | ( b & d ) | ( c & d ); k = 0x8f1bbcdc; break; }
case 3 : { f = b ^ c ^ d; k = 0xca62c1d6; break; }
}
uint32_t temp = std::rotl(a, 5) + f + e + k + values[j];
e = d; d = c; c = std::rotl(b, 30); b = a; a = temp;
}
state[0] += a; state[1] += b; state[2] += c; state[3] += d; state[4] += e;
}
std::stringstream stream;
for ( uint32_t i = 0; i < 20; ++i ) {
int8_t byte_value = static_cast<int8_t>(state[i / 4] >> ( 24 - ( i % 4 ) * 8));
stream << std::setfill('0') << std::setw(2) << std::hex << ( byte_value & 0xff );
}
return stream.str();
}
private:
std::vector<int8_t> add_padding(const std::string& message) {
std::vector<int8_t> bytes(message.begin(), message.end());
bytes.emplace_back(static_cast<uint8_t>(0x80));
uint32_t padding = BLOCK_LENGTH - ( bytes.size() % BLOCK_LENGTH );
if ( padding < 8 ) {
padding += BLOCK_LENGTH;
}
bytes.resize(bytes.size() + padding - 8, static_cast<int8_t>(0x0));
const uint64_t bit_length = 8 * message.length();
for ( int32_t i = 7; i >= 0; --i ) {
bytes.emplace_back(static_cast<int8_t>(bit_length >> ( 8 * i )));
}
return bytes;
}
const uint32_t BLOCK_LENGTH = 64;
};
int main() {
SHA1 sha1;
std::cout << sha1.message_digest("Rosetta Code") << std::endl;
}
</syntaxhighlight>
{{ out }}
<pre>
48c98f7e5a6e736d790ab740dfc3f51a61abe2b5
</pre>
=={{header|Caché ObjectScript}}==
Line 1,176 ⟶ 1,258:
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 ⟶ 1,264:
(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 ⟶ 1,275:
'''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 ⟶ 1,364:
void main() {
writeln(sha1("Rosetta Code".dup));
}</
{{out}}
<pre>48c98f7e5a6e736d790ab740dfc3f51a61abe2b5</pre>
Line 1,288 ⟶ 1,370:
{{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 ⟶ 1,400:
Writeln(SHA1('Rosetta Code'));
readln;
end.</
{{out}}
<pre>
Line 1,324 ⟶ 1,406:
</pre>
=={{header|DWScript}}==
<
{{out}}
Line 1,334 ⟶ 1,416:
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 ⟶ 1,436:
=={{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 ⟶ 1,460:
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 ⟶ 1,555:
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 ⟶ 1,663:
Print : Print "hit any key to end program"
Sleep
End</
{{out}}
<pre>Rosetta Code => 48C98F7E5A6E736D790AB740DFC3F51A61ABE2B5</pre>
Line 1,588 ⟶ 1,670:
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 ⟶ 1,679:
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 ⟶ 1,686:
=={{header|Go}}==
<
import (
Line 1,615 ⟶ 1,697:
h.Write([]byte("Rosetta Code"))
fmt.Printf("%x\n", h.Sum(nil))
}</
{{out}}
<pre>
Line 1,622 ⟶ 1,704:
=={{header|Halon}}==
<
echo sha1($var);</
{{out}}
<pre>
48c98f7e5a6e736d790ab740dfc3f51a61abe2b5
</pre>
=={{header|Hare}}==
<syntaxhighlight lang="hare">use crypto::sha1;
use encoding::hex;
use fmt;
use hash;
use os;
use strings;
export fn main() void = {
const sha = sha1::sha1();
hash::write(&sha, strings::toutf8("Rosetta Code"));
let sum: [sha1::SIZE]u8 = [0...];
hash::sum(&sha, sum);
hex::encode(os::stdout, sum)!;
fmt::println()!;
};</syntaxhighlight>
{{out}}
<pre>
Line 1,630 ⟶ 1,734:
=={{header|Haskell}}==
<
where
import Data.Digest.Pure.SHA
Line 1,644 ⟶ 1,748:
putStr "Rosetta Code SHA1-codiert: "
putStrLn $ convertToSHA1 "Rosetta Code"
</syntaxhighlight>
{{Out}}
<pre>
Line 1,651 ⟶ 1,755:
=={{header|Haxe}}==
<
class Main {
Line 1,658 ⟶ 1,762:
Sys.println(sha1);
}
}</
{{out}}
Line 1,665 ⟶ 1,769:
=={{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,713 ⟶ 1,817:
)
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,727 ⟶ 1,831:
=={{header|Java}}==
The solution to this task would be a small modification to [[MD5#Java|MD5]] (replacing "MD5" with "SHA-1" as noted [http://docs.oracle.com/javase/7/docs/technotes/guides/security/StandardNames.html#MessageDigest here]).
===Implementation===
A direct implementation of the SHA-1 algorithm.
<syntaxhighlight lang="java">
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
public final class SHA1Task {
public static void main(String[] args) {
System.out.println(SHA1.messageDigest("Rosetta Code"));
}
}
final class SHA1 {
public static String messageDigest(String message) {
int[] state = { 0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0 };
byte[] bytes = addPadding(message);
for ( int i = 0; i < bytes.length / BLOCK_LENGTH; i++ ) {
int[] values = new int[80];
for ( int j = 0; j < BLOCK_LENGTH; j++ ) {
values[j / 4] |= ( bytes[i * BLOCK_LENGTH + j] & 0xff ) << ( ( 3 - j % 4 ) * 8 );
}
for ( int j = 16; j < 80; j++ ) {
values[j] = Integer.rotateLeft(values[j - 3] ^ values[j - 8] ^ values[j - 14] ^ values[j - 16], 1);
}
int a = state[0], b = state[1], c = state[2], d = state[3], e = state[4];
int f = 0, k = 0;
for ( int j = 0; j < 80; j++ ) {
switch ( j / 20 ) {
case 0 -> { f = ( b & c ) | ( ~b & d ); k = 0x5a827999; }
case 1 -> { f = b ^ c ^ d; k = 0x6ed9eba1; }
case 2 -> { f = ( b & c ) | ( b & d ) | ( c & d ); k = 0x8f1bbcdc; }
case 3 -> { f = b ^ c ^ d; k = 0xca62c1d6; }
}
int temp = Integer.rotateLeft(a, 5) + f + e + k + values[j];
e = d; d = c; c = Integer.rotateLeft(b, 30); b = a; a = temp;
}
state[0] += a; state[1] += b; state[2] += c; state[3] += d; state[4] += e;
}
StringBuilder result = new StringBuilder();
for ( int i = 0; i < 20; i++ ) {
result.append(String.format("%02x", ( state[i / 4] >>> 24 - ( i % 4 ) * 8 ) & 0xFF ));
}
return result.toString();
}
private static byte[] addPadding(String message) {
byte[] bytes = message.getBytes(StandardCharsets.UTF_8);
bytes = Arrays.copyOf(bytes, bytes.length + 1);
bytes[bytes.length - 1] = (byte) 0x80;
int padding = BLOCK_LENGTH - ( bytes.length % BLOCK_LENGTH );
if ( padding < 8 ) {
padding += BLOCK_LENGTH;
}
bytes = Arrays.copyOf(bytes, bytes.length + padding);
final long bitLength = message.length() * 8;
for ( int i = 0; i < 8; i++ ) {
bytes[bytes.length - 1 - i] = (byte) ( bitLength >>> ( 8 * i ) );
}
return bytes;
}
private static final int BLOCK_LENGTH = 64;
}
</syntaxhighlight>
{{ out }}
<pre>
48c98f7e5a6e736d790ab740dfc3f51a61abe2b5
</pre>
=={{header|Jsish}}==
<
var str = 'Rosetta code';
puts(Util.hash(str, {type:'sha1'}));
Line 1,737 ⟶ 1,921:
b18c883f4da750164b5af362ea9b9f27f90904b4
=!EXPECTEND!=
*/</
{{out}}
Line 1,748 ⟶ 1,932:
{{works with|Julia|0.6}}
<
testdict = Dict("abc" => "a9993e364706816aba3e25717850c26c9cd0d89d",
Line 1,759 ⟶ 1,943:
if length(text) > 50 text = text[1:50] * "..." end
println("# $text\n -> digest: $digest\n -> expect: $expect")
end</
{{out}}
Line 1,773 ⟶ 1,957:
=={{header|Kotlin}}==
<
import java.security.MessageDigest
Line 1,784 ⟶ 1,968:
for (byte in digest) print("%02x".format(byte))
println()
}</
{{out}}
Line 1,792 ⟶ 1,976:
=={{header|Lasso}}==
<
{{out}}
Line 1,798 ⟶ 1,982:
=={{header|Liberty BASIC}}==
<syntaxhighlight lang="lb">
'--------------------------------------------------------------------------------
' FAST SHA1 CALCULATION BASED ON MS ADVAPI32.DLL BY CRYPTOMAN '
Line 1,869 ⟶ 2,053:
next
end function
</syntaxhighlight>
{{Out}}
<pre>48C98F7E5A6E736D790AB740DFC3F51A61ABE2B5</pre>
Line 1,875 ⟶ 2,059:
=={{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,895 ⟶ 2,079:
{{libheader|sha1}} ([https://github.com/kikito/sha1.lua luarocks install sha1])
<
local sha1 = require "sha1"
Line 1,901 ⟶ 2,085:
for i, str in ipairs{"Rosetta code", "Rosetta Code"} do
print(string.format("SHA-1(%q) = %s", str, sha1(str)))
end</
{{out}}
Line 1,911 ⟶ 2,095:
=={{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,923 ⟶ 2,107:
=={{header|min}}==
{{works with|min|0.19.6}}
<
{{out}}
<pre>
Line 1,932 ⟶ 2,116:
SHA-1 was added in Neko 2.2.
<syntaxhighlight lang="actionscript">/**
SHA-1 in Neko
Tectonics:
Line 1,946 ⟶ 2,130:
/* Output in lowercase hex */
$print(base_encode(result, "0123456789abcdef"));</
{{out}}
Line 1,955 ⟶ 2,139:
=={{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,005 ⟶ 2,189:
return digestSum
</syntaxhighlight>
{{out}}
<pre>
Line 2,015 ⟶ 2,199:
=={{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,032 ⟶ 2,216:
=={{header|Oberon-2}}==
{{works with|oo2c}} {{libheader|crypto}}
<
MODULE SHA1;
IMPORT
Line 2,050 ⟶ 2,234:
Out.String("SHA1: ");Utils.PrintHex(str,0,h.size);Out.Ln
END SHA1.
</syntaxhighlight>
{{out}}
<pre>
Line 2,061 ⟶ 2,245:
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,077 ⟶ 2,261:
It works on Linux systems.
<
The code above creates a new function sha1(s) which returns SHA-1 hash of item s.
Line 2,088 ⟶ 2,272:
=={{header|Pascal}}==
{{works with|Free_Pascal}} {{libheader|sha1}}
<
uses
sha1;
Line 2,096 ⟶ 2,280:
d:=SHA1String('Rosetta Code');
WriteLn(SHA1Print(d));
end.</
{{out}}
Line 2,103 ⟶ 2,287:
=={{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,202 ⟶ 2,386:
<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,209 ⟶ 2,393:
=={{header|PHP}}==
<
$string = 'Rosetta Code';
echo sha1( $string ), "\n";
?></
{{out}}
<pre>48c98f7e5a6e736d790ab740dfc3f51a61abe2b5</pre>
Line 2,218 ⟶ 2,402:
=={{header|PicoLisp}}==
Library and implementation.
<
(| (mod32 (>> (- C) X)) (>> (- 32 C) X)) )
Line 2,333 ⟶ 2,517:
'(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,357 ⟶ 2,541:
[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|Prolog}}==
{{works with|SWI-Prolog}}
SWI-Prolog has SHA1 hashing as the default in its <code>sha_hash</code> function.
<syntaxhighlight lang="Prolog">:- use_module(library(sha)).
sha_hex(Str,Hex):-
sha_hash(Str, Hash, []),
hash_atom(Hash, Hex).</syntaxhighlight>
<syntaxhighlight lang="Prolog">?- sha_hex("Rosetta Code",Hex).
Hex = '48c98f7e5a6e736d790ab740dfc3f51a61abe2b5'.</syntaxhighlight>
=={{header|PureBasic}}==
PB Version 5.40
<
UseSHA1Fingerprint() : b$=StringFingerprint(a$, #PB_Cipher_SHA1)
Line 2,368 ⟶ 2,563:
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,395 ⟶ 2,590:
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,414 ⟶ 2,609:
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,457 ⟶ 2,652:
'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,466 ⟶ 2,661:
===Library based implementation===
Quite speedy.
<syntaxhighlight lang="raku"
# use sha1-hex() if you want a hex string
Line 2,475 ⟶ 2,670:
'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,483 ⟶ 2,678:
=={{header|Ring}}==
<
# Project : SHA-1
Line 2,491 ⟶ 2,686:
see "SHA-1: "
see sha1(str) + nl
</syntaxhighlight>
Output:
<pre>
Line 2,502 ⟶ 2,697:
'''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,576 ⟶ 2,771:
'Rosetta Code',
].each {|s| printf("%s:\n %s\n", s, *sha1(s).unpack('H*'))}
end</
{{out}}
<pre>
Line 2,589 ⟶ 2,784:
=={{header|Rust}}==
Using sha1 crate: https://docs.rs/sha1/0.6.0/sha1/
<
fn main() {
Line 2,596 ⟶ 2,791:
println!("{}", hash_msg.digest().to_string());
}
</syntaxhighlight>
Output
<pre>
Line 2,605 ⟶ 2,800:
Support for MD5 and SHA-1 are included in the standard "chksum" library:
<
print(sha1sum("Rosetta Code"));</
{{out}}
Line 2,615 ⟶ 2,810:
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,705 ⟶ 2,900:
println(hash("Rosetta Code"))
}
</syntaxhighlight>
{{out}}
Line 2,713 ⟶ 2,908:
{{works with|Ol}}
<
; band - binary AND operation
; bor - binary OR operation
Line 2,874 ⟶ 3,069:
(->32 (+ D d))
(->32 (+ E e)))))))))
</syntaxhighlight>
{{out}}
<
(define (->string value)
(runes->string
Line 2,893 ⟶ 3,088:
(print (->string (sha1:digest "")))
> da39a3ee5e6b4b0d3255bfef95601890afd80709
</syntaxhighlight>
=={{header|Seed7}}==
<
include "msgdigest.s7i";
Line 2,902 ⟶ 3,097:
begin
writeln(hex(sha1("Rosetta Code")));
end func;</
{{out}}
Line 2,910 ⟶ 3,105:
=={{header|Sidef}}==
<
say sha.sha1_hex('Rosetta Code');</
{{out}}
<pre>48c98f7e5a6e736d790ab740dfc3f51a61abe2b5</pre>
Line 2,917 ⟶ 3,112:
=={{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,932 ⟶ 3,127:
=={{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|V (Vlang)}}==
<syntaxhighlight lang="v (vlang)">import crypto.sha1
fn main() {
println("${sha1.hexhash('Rosetta Code')}")//Rosetta code
mut h := sha1.new()
h.write('Rosetta Code'.bytes()) ?
println('${h.checksum().map(it.hex()).join('')}')
}</syntaxhighlight>
{{out}}
<pre>
48c98f7e5a6e736d790ab740dfc3f51a61abe2b5
48c98f7e5a6e736d790ab740dfc3f51a61abe2b5
</pre>
=={{header|Wren}}==
{{libheader|Wren-crypto}}
{{libheader|Wren-fmt}}
<
import "./fmt" for Fmt
var strings = [
Line 2,961 ⟶ 3,172:
var hash = Sha1.digest(s)
Fmt.print("$s <== '$0s'", hash, s)
}</
{{out}}
Line 2,979 ⟶ 3,190:
=={{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 2,993 ⟶ 3,204:
zkl: MsgHash.SHA1("a"*1000,1000); // hash 1000 a's 1000 times
34aa973cd4c4daa4f61eeb2bdbad27316534016f</
|