SHA-1: Difference between revisions

8,818 bytes added ,  3 months ago
m
(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">
<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>
</lang>
{{Output}}
<pre>
Line 409:
{{works with|GNAT}}
 
<langsyntaxhighlight Adalang="ada">with Ada.Text_IO;
with GNAT.SHA1;
 
Line 416:
Ada.Text_IO.Put_Line ("SHA1 (""Rosetta Code"") = " &
GNAT.SHA1.Digest ("Rosetta Code"));
end Main;</langsyntaxhighlight>
 
{{out}}
Line 424:
{{works with|as|Raspberry Pi}}
<B>with openssl library </B>
<syntaxhighlight lang="arm assembly">
<lang ARM Assembly>
 
/* ARM assembly Raspberry PI */
Line 522:
/***************************************************/
.include "../affichage.inc"
</syntaxhighlight>
</lang>
<B>with only instructions assembly ARM</B>
<syntaxhighlight lang="text">
/* ARM assembly Raspberry PI */
/* program sha1.s */
Line 875:
.include "../affichage.inc"
 
</syntaxhighlight>
</lang>
{{Output}}
<pre>
Line 883:
=={{header|Arturo}}==
 
<langsyntaxhighlight lang="rebol">print digest.sha "The quick brown fox jumped over the lazy dog's back"</langsyntaxhighlight>
 
{{out}}
Line 890:
 
=={{header|Astro}}==
<langsyntaxhighlight lang="python">import crypto { sha1 }
let hash = sha1.hexdigest('Ars longa, vita brevis')
print hash
</syntaxhighlight>
</lang>
 
=={{header|AutoHotkey}}==
Source: [https://github.com/jNizM/AutoHotkey_Scripts/tree/master/Functions/Checksums SHA-1 @github] by jNizM
<langsyntaxhighlight AutoHotkeylang="autohotkey">str := "Rosetta Code"
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)
}</langsyntaxhighlight>
{{out}}
<pre>String: Rosetta Code
Line 956:
===Library===
{{works with|BBC BASIC for Windows}}
<langsyntaxhighlight lang="bbcbasic"> PRINT FNsha1("Rosetta Code")
END
Line 976:
hash$ += RIGHT$("0" + STR$~buffer%?i%, 2)
NEXT
= hash$</langsyntaxhighlight>
{{out}}
<pre>
Line 984:
===Native===
{{works with|BBC BASIC for Windows}}
<langsyntaxhighlight lang="bbcbasic"> *FLOAT64
PRINT FNsha1("Rosetta Code")
END
Line 1,085:
WHILE n# > &7FFFFFFF : n# -= 2^32 : ENDWHILE
WHILE n# < &80000000 : n# += 2^32 : ENDWHILE
= n#</langsyntaxhighlight>
{{out}}
<pre>
Line 1,093:
=={{header|C}}==
{{libheader|OpenSSL}}
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Line 1,110:
return EXIT_SUCCESS;
}</langsyntaxhighlight>
 
=={{header|C sharp}}==
Tests the built-in SHA1CryptoServiceProvider:
<langsyntaxhighlight lang="csharp">using System;
using System.Security.Cryptography;
using System.Text;
Line 1,134:
}
}
}</langsyntaxhighlight>
 
=={{header|C++}}==
{{libheader|Poco}}
Compiling with <code>g++ -lPocoCrypto shaexample.cpp -o shaexample</code>:
<langsyntaxhighlight lang="cpp">#include <string>
#include <iostream>
#include "Poco/SHA1Engine.h"
Line 1,158:
<< " !" << std::endl ;
return 0 ;
}</langsyntaxhighlight>
{{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).
<langsyntaxhighlight lang="lisp">;;; in addition to sha1, ironclad provides sha224, sha256, sha384, and sha512.
(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>
</lang>
 
=={{header|Crystal}}==
<langsyntaxhighlight lang="ruby">require "openssl"
puts OpenSSL::Digest.new("sha1").update("Rosetta Code")</langsyntaxhighlight>
{{out}}
<pre>48c98f7e5a6e736d790ab740dfc3f51a61abe2b5</pre>
Line 1,193 ⟶ 1,275:
'''First:''' Use native 'std.digest.sha' library
{{trans|Python}}
<langsyntaxhighlight lang="d">void main() {
import std.stdio, std.digest.sha;
 
writefln("%-(%02x%)", "Ars longa, vita brevis".sha1Of);
}</langsyntaxhighlight>
{{out}}
<pre>e640d285242886eb96ab80cbf858389b3df52f43</pre>
 
'''Second:''' Re-implement SHA-1 in D
<langsyntaxhighlight lang="d">import std.stdio, std.string, std.conv, std.algorithm, std.format, std.array,
std.range, std.digest.sha;
 
Line 1,282 ⟶ 1,364:
void main() {
writeln(sha1("Rosetta Code".dup));
}</langsyntaxhighlight>
{{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">
<lang Delphi>
program Sha_1;
 
Line 1,318 ⟶ 1,400:
Writeln(SHA1('Rosetta Code'));
readln;
end.</langsyntaxhighlight>
{{out}}
<pre>
Line 1,324 ⟶ 1,406:
</pre>
=={{header|DWScript}}==
<langsyntaxhighlight lang="delphi">PrintLn( HashSHA1.HashData('Rosetta code') );</langsyntaxhighlight>
 
{{out}}
Line 1,334 ⟶ 1,416:
Uses Erlang module 'crypto'
{{trans|Erlang}}
<langsyntaxhighlight lang="elixir">
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>
</lang>
 
=={{header|Emacs Lisp}}==
<langsyntaxhighlight lang="lisp">
(sha1 "Rosetta Code") ;=> "48c98f7e5a6e736d790ab740dfc3f51a61abe2b5"
(secure-hash 'sha1 "Rosetta Code") ;=> "48c98f7e5a6e736d790ab740dfc3f51a61abe2b5"
</syntaxhighlight>
</lang>
 
=={{header|Erlang}}==
Line 1,354 ⟶ 1,436:
 
=={{header|F_Sharp|F#}}==
<langsyntaxhighlight lang="fsharp">
let n = System.Security.Cryptography.SHA1.Create()
Array.iter (printf "%x ") (n.ComputeHash "Rosetta Code"B)
</syntaxhighlight>
</lang>
{{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.
 
<langsyntaxhighlight lang="fortran">module sha1_mod
use kernel32
use advapi32
Line 1,473 ⟶ 1,555:
deallocate(name)
end do
end program</langsyntaxhighlight>
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">' version 18-10-2016
' 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</langsyntaxhighlight>
{{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.
 
<langsyntaxhighlight lang="frink">println[messageDigest["Rosetta Code", "SHA-1"]]</langsyntaxhighlight>
 
{{out}}
Line 1,597 ⟶ 1,679:
SHA-1, being overtaken, is not recommended but is supported in GLib checksum, ''ChecksumType.SHA1''.
 
<langsyntaxhighlight lang="genie">print Checksum.compute_for_string(ChecksumType.SHA1, "Rosetta code", -1)</langsyntaxhighlight>
 
(The -1 is NUL byte terminated string indicator for length)
Line 1,604 ⟶ 1,686:
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import (
Line 1,615 ⟶ 1,697:
h.Write([]byte("Rosetta Code"))
fmt.Printf("%x\n", h.Sum(nil))
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,622 ⟶ 1,704:
 
=={{header|Halon}}==
<langsyntaxhighlight lang="halon">$var = "Rosetta Code";
echo sha1($var);</langsyntaxhighlight>
{{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}}==
<langsyntaxhighlight Haskelllang="haskell">module Digestor
where
import Data.Digest.Pure.SHA
Line 1,644 ⟶ 1,748:
putStr "Rosetta Code SHA1-codiert: "
putStrLn $ convertToSHA1 "Rosetta Code"
</syntaxhighlight>
</lang>
{{Out}}
<pre>
Line 1,651 ⟶ 1,755:
 
=={{header|Haxe}}==
<langsyntaxhighlight lang="haxe">import haxe.crypto.Sha1;
 
class Main {
Line 1,658 ⟶ 1,762:
Sys.println(sha1);
}
}</langsyntaxhighlight>
 
{{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:
<langsyntaxhighlight lang="j"> require '~addons/ide/qt/qt.ijs'
getsha1=: 'sha1'&gethash_jqtide_
getsha1 'Rosetta Code'
48c98f7e5a6e736d790ab740dfc3f51a61abe2b5</langsyntaxhighlight>
 
From J8.06, the sha family of hashes have builtin support.
 
<langsyntaxhighlight lang="j"> sha1=:128!:6
sha1'Rosetta Code'
48c98f7e5a6e736d790ab740dfc3f51a61abe2b5</langsyntaxhighlight>
 
A implementation of SHA-1 in J follows:
 
<langsyntaxhighlight Jlang="j">pad=: ,1,(0#~512 | [: - 65 + #),(64#2)#:#
 
f=:4 :0
Line 1,713 ⟶ 1,817:
)
 
sha1=: [:> [: process&.>/ (<H) (,~ |.) _512<\ pad</langsyntaxhighlight>
 
Example use:
 
<langsyntaxhighlight Jlang="j"> text2bits=: (8#2) ,@:#: a. i. ]
bits2hex=: '0123456789abcdef' {~ _4 #.\ ,
 
bits2hex sha1 text2bits 'Rosetta Code'
48c98f7e5a6e736d790ab740dfc3f51a61abe2b5</langsyntaxhighlight>
 
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}}==
<langsyntaxhighlight lang="javascript">/* SHA-1 hash in Jsish */
var str = 'Rosetta code';
puts(Util.hash(str, {type:'sha1'}));
Line 1,737 ⟶ 1,921:
b18c883f4da750164b5af362ea9b9f27f90904b4
=!EXPECTEND!=
*/</langsyntaxhighlight>
 
{{out}}
Line 1,748 ⟶ 1,932:
{{works with|Julia|0.6}}
 
<langsyntaxhighlight lang="julia">using Nettle
 
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</langsyntaxhighlight>
 
{{out}}
Line 1,773 ⟶ 1,957:
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="scala">// version 1.0.6
 
import java.security.MessageDigest
Line 1,784 ⟶ 1,968:
for (byte in digest) print("%02x".format(byte))
println()
}</langsyntaxhighlight>
 
{{out}}
Line 1,792 ⟶ 1,976:
 
=={{header|Lasso}}==
<langsyntaxhighlight Lassolang="lasso">cipher_digest('Rosetta Code', -digest='SHA1',-hex=true)</langsyntaxhighlight>
 
{{out}}
Line 1,798 ⟶ 1,982:
 
=={{header|Liberty BASIC}}==
<syntaxhighlight lang="lb">
<lang lb>
'--------------------------------------------------------------------------------
' FAST SHA1 CALCULATION BASED ON MS ADVAPI32.DLL BY CRYPTOMAN '
Line 1,869 ⟶ 2,053:
next
end function
</syntaxhighlight>
</lang>
{{Out}}
<pre>48C98F7E5A6E736D790AB740DFC3F51A61ABE2B5</pre>
Line 1,875 ⟶ 2,059:
=={{header|Lingo}}==
{{libheader|Crypto Xtra}}
<langsyntaxhighlight lang="lingo">crypto = xtra("Crypto").new()
put crypto.cx_sha1_string("Rosetta Code")</langsyntaxhighlight>
{{out}}
<pre>-- "48c98f7e5a6e736d790ab740dfc3f51a61abe2b5"</pre>
 
=={{header|LiveCode}}==
<langsyntaxhighlight LiveCodelang="livecode">command shaRosettaCode
local shex, sha1
put sha1Digest("Rosetta Code") into sha1
get binaryDecode("H*",sha1,shex)
put shex
end shaRosettaCode</langsyntaxhighlight>
{{out}}
<pre>48c98f7e5a6e736d790ab740dfc3f51a61abe2b5</pre>
Line 1,895 ⟶ 2,079:
{{libheader|sha1}} ([https://github.com/kikito/sha1.lua luarocks install sha1])
 
<langsyntaxhighlight Lualang="lua">#!/usr/bin/lua
 
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</langsyntaxhighlight>
 
{{out}}
Line 1,911 ⟶ 2,095:
=={{header|Maple}}==
 
<langsyntaxhighlight lang="maple">with(StringTools):
Hash("Ars longa, vita brevis",method="SHA1");
 
# "e640d285242886eb96ab80cbf858389b3df52f43"</langsyntaxhighlight>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang="text">Hash["Rosetta code","SHA1","HexString"]</langsyntaxhighlight>
{{out}}
<pre>48c98f7e5a6e736d790ab740dfc3f51a61abe2b5</pre>
Line 1,923 ⟶ 2,107:
=={{header|min}}==
{{works with|min|0.19.6}}
<langsyntaxhighlight lang="min">"Rosetta Code" sha1 puts!</langsyntaxhighlight>
{{out}}
<pre>
Line 1,932 ⟶ 2,116:
SHA-1 was added in Neko 2.2.
 
<syntaxhighlight lang="actionscript">/**
<lang ActionScript>/**
SHA-1 in Neko
Tectonics:
Line 1,946 ⟶ 2,130:
 
/* Output in lowercase hex */
$print(base_encode(result, "0123456789abcdef"));</langsyntaxhighlight>
 
{{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.
<langsyntaxhighlight NetRexxlang="netrexx">/* NetRexx */
options replace format comments java crossref savelog symbols binary
 
Line 2,005 ⟶ 2,189:
return digestSum
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,015 ⟶ 2,199:
 
=={{header|NewLISP}}==
<langsyntaxhighlight NewLISPlang="newlisp">;; 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")</langsyntaxhighlight>
 
=={{header|Nim}}==
Nim standard library provides the module “std/sha1” to compute SHA1 digests.
 
<langsyntaxhighlight lang="nim">import std/sha1
 
echo secureHash("Rosetta Code")</langsyntaxhighlight>
 
{{out}}
Line 2,032 ⟶ 2,216:
=={{header|Oberon-2}}==
{{works with|oo2c}} {{libheader|crypto}}
<langsyntaxhighlight lang="oberon2">
MODULE SHA1;
IMPORT
Line 2,050 ⟶ 2,234:
Out.String("SHA1: ");Utils.PrintHex(str,0,h.size);Out.Ln
END SHA1.
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,061 ⟶ 2,245:
Using the library <code>ocaml-sha</code> in the interactive loop:
 
<langsyntaxhighlight lang="ocaml">$ ocaml -I +sha sha1.cma
Objective Caml version 3.12.1
 
# Sha1.to_hex (Sha1.string "Rosetta Code") ;;
- : string = "48c98f7e5a6e736d790ab740dfc3f51a61abe2b5"</langsyntaxhighlight>
 
=={{header|Octave}}==
Normally SHA-1 is available in the [http://octave.sourceforge.net/general/function/SHA1.html general package].
 
<langsyntaxhighlight lang="octave">sprintf("%02x", SHA1(+"Rosetta Code"(:)))</langsyntaxhighlight>
{{out}}
<pre>ans = 48c98f7e5a6e736d790ab740dfc3f51a61abe2b5</pre>
Line 2,077 ⟶ 2,261:
It works on Linux systems.
 
<langsyntaxhighlight lang="parigp">sha1(s)=extern("echo \"Str(`echo -n '"Str(s)"'|sha1sum|cut -d' ' -f1`)\"")</langsyntaxhighlight>
 
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}}
<langsyntaxhighlight lang="pascal">program RosettaSha1;
uses
sha1;
Line 2,096 ⟶ 2,280:
d:=SHA1String('Rosetta Code');
WriteLn(SHA1Print(d));
end.</langsyntaxhighlight>
 
{{out}}
Line 2,103 ⟶ 2,287:
=={{header|Perl}}==
{{libheader|Digest::SHA}}
<langsyntaxhighlight lang="perl">use Digest::SHA qw(sha1_hex);
 
print sha1_hex('Rosetta Code'), "\n";</langsyntaxhighlight>
{{out}}
<pre>48c98f7e5a6e736d790ab740dfc3f51a61abe2b5</pre>
 
The same in OO manner
<langsyntaxhighlight lang="perl">use Digest::SHA;
 
my $sha1 = Digest::SHA->new(1);
$sha1->add('Rosetta Code');
print $sha1->hexdigest, "\n";</langsyntaxhighlight>
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<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>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 2,209 ⟶ 2,393:
 
=={{header|PHP}}==
<langsyntaxhighlight lang="php"><?php
$string = 'Rosetta Code';
echo sha1( $string ), "\n";
?></langsyntaxhighlight>
{{out}}
<pre>48c98f7e5a6e736d790ab740dfc3f51a61abe2b5</pre>
Line 2,218 ⟶ 2,402:
=={{header|PicoLisp}}==
Library and implementation.
<langsyntaxhighlight PicoLisplang="picolisp">(de leftRotate (X C)
(| (mod32 (>> (- C) X)) (>> (- 32 C) X)) )
 
Line 2,333 ⟶ 2,517:
'(NIL (20)) ) ) ) ) )
(bye)</langsyntaxhighlight>
 
=={{header|Pike}}==
<syntaxhighlight lang="pike">
<lang Pike>
string input = "Rosetta Code";
string out = Crypto.SHA1.hash(input);
write( String.string2hex(out) +"\n");
</syntaxhighlight>
</lang>
{{Out}}
<pre>48c98f7e5a6e736d790ab740dfc3f51a61abe2b5</pre>
 
=={{header|PowerShell}}==
<syntaxhighlight lang="powershell">
<lang PowerShell>
Function Calculate-SHA1( $String ){
$Enc = [system.Text.Encoding]::UTF8
Line 2,357 ⟶ 2,541:
[System.Convert]::ToBase64String($Result)
}
</syntaxhighlight>
</lang>
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
<langsyntaxhighlight lang="purebasic">a$="Rosetta Code"
 
UseSHA1Fingerprint() : b$=StringFingerprint(a$, #PB_Cipher_SHA1)
Line 2,368 ⟶ 2,563:
OpenConsole()
Print("[SHA1] Text: "+a$+" ==> "+b$)
Input()</langsyntaxhighlight>
{{out}}
<pre>[SHA1] Text: Rosetta Code ==> 48c98f7e5a6e736d790ab740dfc3f51a61abe2b5</pre>
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">import hashlib
h = hashlib.sha1()
h.update(bytes("Ars longa, vita brevis", encoding="ASCII"))
h.hexdigest()
# "e640d285242886eb96ab80cbf858389b3df52f43"</langsyntaxhighlight>
 
=={{header|R}}==
<langsyntaxhighlight lang="rsplus">
library(digest)
 
input <- "Rosetta Code"
cat(digest(input, algo = "sha1", serialize = FALSE), "\n")
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,395 ⟶ 2,590:
With the built-in <tt>file/sha1</tt> library:
 
<langsyntaxhighlight lang="racket">
#lang racket
(require file/sha1)
(sha1 (open-input-string "Rosetta Code"))
</syntaxhighlight>
</lang>
 
With the faster <tt>openssl/sha1</tt> library (requires OpenSSL to be installed):
 
<langsyntaxhighlight lang="racket">
#lang racket
(require openssl/sha1)
(sha1 (open-input-string "Rosetta Code"))
</syntaxhighlight>
</lang>
 
=={{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" perl6line>sub postfix:<mod2³²> { $^x % 2**32 }
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';</langsyntaxhighlight>
{{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" perl6line>use Digest::SHA1::Native;
 
# use sha1-hex() if you want a hex string
Line 2,475 ⟶ 2,670:
'Rosetta Code',
'Ars longa, vita brevis'
;</langsyntaxhighlight>
{{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}}==
<langsyntaxhighlight lang="ring">
# Project : SHA-1
 
Line 2,491 ⟶ 2,686:
see "SHA-1: "
see sha1(str) + nl
</syntaxhighlight>
</lang>
Output:
<pre>
Line 2,502 ⟶ 2,697:
 
'''First:''' Use 'digest' from Ruby's standard library.
<langsyntaxhighlight lang="ruby">require 'digest'
puts Digest::SHA1.hexdigest('Rosetta Code')</langsyntaxhighlight>
 
'''Second:''' Use 'openssl' from Ruby's standard library. {{libheader|OpenSSL}}
<langsyntaxhighlight lang="ruby">require 'openssl'
puts OpenSSL::Digest::SHA1.hexdigest('Rosetta Code')</langsyntaxhighlight>
 
'''Third:''' Reimplement SHA-1 in Ruby.
<langsyntaxhighlight lang="ruby">require 'stringio'
 
# 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</langsyntaxhighlight>
{{out}}
<pre>
Line 2,589 ⟶ 2,784:
=={{header|Rust}}==
Using sha1 crate: https://docs.rs/sha1/0.6.0/sha1/
<langsyntaxhighlight Rustlang="rust">use sha1::Sha1;
 
fn main() {
Line 2,596 ⟶ 2,791:
println!("{}", hash_msg.digest().to_string());
}
</syntaxhighlight>
</lang>
Output
<pre>
Line 2,605 ⟶ 2,800:
Support for MD5 and SHA-1 are included in the standard "chksum" library:
 
<langsyntaxhighlight Slang="s-lang">require("chksum");
print(sha1sum("Rosetta Code"));</langsyntaxhighlight>
 
{{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]).
 
<langsyntaxhighlight lang="scala">
import java.nio._
 
Line 2,705 ⟶ 2,900:
println(hash("Rosetta Code"))
}
</syntaxhighlight>
</lang>
 
{{out}}
Line 2,713 ⟶ 2,908:
{{works with|Ol}}
 
<langsyntaxhighlight lang="scheme">
; band - binary AND operation
; bor - binary OR operation
Line 2,874 ⟶ 3,069:
(->32 (+ D d))
(->32 (+ E e)))))))))
</syntaxhighlight>
</lang>
 
{{out}}
<langsyntaxhighlight lang="scheme">
(define (->string value)
(runes->string
Line 2,893 ⟶ 3,088:
(print (->string (sha1:digest "")))
> da39a3ee5e6b4b0d3255bfef95601890afd80709
</syntaxhighlight>
</lang>
 
=={{header|Seed7}}==
<langsyntaxhighlight lang="seed7">$ include "seed7_05.s7i";
include "msgdigest.s7i";
 
Line 2,902 ⟶ 3,097:
begin
writeln(hex(sha1("Rosetta Code")));
end func;</langsyntaxhighlight>
 
{{out}}
Line 2,910 ⟶ 3,105:
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">var sha = frequire('Digest::SHA');
say sha.sha1_hex('Rosetta Code');</langsyntaxhighlight>
{{out}}
<pre>48c98f7e5a6e736d790ab740dfc3f51a61abe2b5</pre>
Line 2,917 ⟶ 3,112:
=={{header|Smalltalk}}==
{{works with|GNU Smalltalk}}
<langsyntaxhighlight lang="smalltalk">PackageLoader fileInPackage: 'Digest'.
(SHA1 hexDigestOf: 'Rosetta Code') displayNl.</langsyntaxhighlight>
{{works with|Smalltalk/X}}
<langsyntaxhighlight lang="smalltalk">(SHA1Stream hashValueOf:'Rosetta Code')</langsyntaxhighlight>
 
=={{header|Tcl}}==
{{tcllib|sha1}}
<langsyntaxhighlight lang="tcl">package require sha1
puts [sha1::sha1 "Rosetta Code"]</langsyntaxhighlight>
{{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)]}}
<langsyntaxhighlight lang="bash">$ echo -n 'ASCII string' | sha1
9e9aeefe5563845ec5c42c5630842048c0fc261b</langsyntaxhighlight>
 
{{libheader|OpenSSL}}
<langsyntaxhighlight lang="bash">$ echo -n 'ASCII string' | openssl sha1 | sed 's/.*= //'
9e9aeefe5563845ec5c42c5630842048c0fc261b</langsyntaxhighlight>
 
=={{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}}
<langsyntaxhighlight ecmascriptlang="wren">import "./crypto" for Sha1
import "./fmt" for Fmt
var strings = [
Line 2,961 ⟶ 3,172:
var hash = Sha1.digest(s)
Fmt.print("$s <== '$0s'", hash, s)
}</langsyntaxhighlight>
 
{{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.
<langsyntaxhighlight lang="zkl">$ zkl // run the REPL
zkl: var MsgHash=Import("zklMsgHash")
MsgHash
Line 2,993 ⟶ 3,204:
 
zkl: MsgHash.SHA1("a"*1000,1000); // hash 1000 a's 1000 times
34aa973cd4c4daa4f61eeb2bdbad27316534016f</langsyntaxhighlight>
 
 
9,477

edits