SHA-1: Difference between revisions

34,185 bytes added ,  3 months ago
m
(add Elixir)
m (→‎{{header|Wren}}: Minor tidy)
 
(24 intermediate revisions by 17 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 arturolang="rebol">print [sha1digest.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>
 
 
=={{header|D}}==
'''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,283 ⟶ 1,364:
void main() {
writeln(sha1("Rosetta Code".dup));
}</langsyntaxhighlight>
{{out}}
<pre>48c98f7e5a6e736d790ab740dfc3f51a61abe2b5</pre>
=={{header|Delphi}}==
{{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;
 
{$APPTYPE CONSOLE}
 
uses
System.SysUtils,
DCPsha1;
 
function SHA1(const Str: string): string;
var
HashDigest: array of byte;
d: Byte;
begin
Result := '';
with TDCP_sha1.Create(nil) do
begin
Init;
UpdateStr(Str);
SetLength(HashDigest, GetHashSize div 8);
final(HashDigest[0]);
for d in HashDigest do
Result := Result + d.ToHexString(2);
Free;
end;
end;
 
begin
Writeln(SHA1('Rosetta Code'));
readln;
end.</syntaxhighlight>
{{out}}
<pre>
48C98F7E5A6E736D790AB740DFC3F51A61ABE2B5
</pre>
=={{header|DWScript}}==
<langsyntaxhighlight lang="delphi">PrintLn( HashSHA1.HashData('Rosetta code') );</langsyntaxhighlight>
 
{{out}}
Line 1,298 ⟶ 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}}==
<syntaxhighlight lang="lisp">
(sha1 "Rosetta Code") ;=> "48c98f7e5a6e736d790ab740dfc3f51a61abe2b5"
(secure-hash 'sha1 "Rosetta Code") ;=> "48c98f7e5a6e736d790ab740dfc3f51a61abe2b5"
</syntaxhighlight>
 
=={{header|Erlang}}==
Line 1,312 ⟶ 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,336 ⟶ 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,431 ⟶ 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,539 ⟶ 1,663:
Print : Print "hit any key to end program"
Sleep
End</langsyntaxhighlight>
{{out}}
<pre>Rosetta Code => 48C98F7E5A6E736D790AB740DFC3F51A61ABE2B5</pre>
 
=={{header|Frink}}==
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.
 
<syntaxhighlight lang="frink">println[messageDigest["Rosetta Code", "SHA-1"]]</syntaxhighlight>
 
{{out}}
<pre>48c98f7e5a6e736d790ab740dfc3f51a61abe2b5
</pre>
 
=={{header|Genie}}==
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,553 ⟶ 1,686:
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import (
Line 1,564 ⟶ 1,697:
h.Write([]byte("Rosetta Code"))
fmt.Printf("%x\n", h.Sum(nil))
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,571 ⟶ 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,579 ⟶ 1,734:
 
=={{header|Haskell}}==
<langsyntaxhighlight Haskelllang="haskell">module Digestor
where
import Data.Digest.Pure.SHA
Line 1,593 ⟶ 1,748:
putStr "Rosetta Code SHA1-codiert: "
putStrLn $ convertToSHA1 "Rosetta Code"
</syntaxhighlight>
</lang>
{{Out}}
<pre>
Line 1,600 ⟶ 1,755:
 
=={{header|Haxe}}==
<langsyntaxhighlight lang="haxe">import haxe.crypto.Sha1;
 
class Main {
Line 1,607 ⟶ 1,762:
Sys.println(sha1);
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,614 ⟶ 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,662 ⟶ 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,676 ⟶ 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,686 ⟶ 1,921:
b18c883f4da750164b5af362ea9b9f27f90904b4
=!EXPECTEND!=
*/</langsyntaxhighlight>
 
{{out}}
Line 1,697 ⟶ 1,932:
{{works with|Julia|0.6}}
 
<langsyntaxhighlight lang="julia">using Nettle
 
testdict = Dict("abc" => "a9993e364706816aba3e25717850c26c9cd0d89d",
Line 1,708 ⟶ 1,943:
if length(text) > 50 text = text[1:50] * "..." end
println("# $text\n -> digest: $digest\n -> expect: $expect")
end</langsyntaxhighlight>
 
{{out}}
Line 1,722 ⟶ 1,957:
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="scala">// version 1.0.6
 
import java.security.MessageDigest
Line 1,733 ⟶ 1,968:
for (byte in digest) print("%02x".format(byte))
println()
}</langsyntaxhighlight>
 
{{out}}
Line 1,741 ⟶ 1,976:
 
=={{header|Lasso}}==
<langsyntaxhighlight Lassolang="lasso">cipher_digest('Rosetta Code', -digest='SHA1',-hex=true)</langsyntaxhighlight>
 
{{out}}
Line 1,747 ⟶ 1,982:
 
=={{header|Liberty BASIC}}==
<syntaxhighlight lang="lb">
<lang lb>
'--------------------------------------------------------------------------------
' FAST SHA1 CALCULATION BASED ON MS ADVAPI32.DLL BY CRYPTOMAN '
Line 1,818 ⟶ 2,053:
next
end function
</syntaxhighlight>
</lang>
{{Out}}
<pre>48C98F7E5A6E736D790AB740DFC3F51A61ABE2B5</pre>
Line 1,824 ⟶ 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,844 ⟶ 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,850 ⟶ 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,858 ⟶ 2,093:
</pre>
 
=={{header|MathematicaMaple}}==
 
<syntaxhighlight lang="maple">with(StringTools):
<lang>Hash["Rosetta code","SHA1","HexString"]</lang>
Hash("Ars longa, vita brevis",method="SHA1");
 
# "e640d285242886eb96ab80cbf858389b3df52f43"</syntaxhighlight>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang="text">Hash["Rosetta code","SHA1","HexString"]</syntaxhighlight>
{{out}}
<pre>48c98f7e5a6e736d790ab740dfc3f51a61abe2b5</pre>
Line 1,867 ⟶ 2,107:
=={{header|min}}==
{{works with|min|0.19.6}}
<langsyntaxhighlight lang="min">"Rosetta Code" sha1 puts!</langsyntaxhighlight>
{{out}}
<pre>
48c98f7e5a6e736d790ab740dfc3f51a61abe2b5
</pre>
 
=={{header|Neko}}==
SHA-1 was added in Neko 2.2.
 
<syntaxhighlight lang="actionscript">/**
SHA-1 in Neko
Tectonics:
nekoc SHA-1.neko
neko SHA-1
*/
 
var SHA1 = $loader.loadprim("std@make_sha1", 3);
var base_encode = $loader.loadprim("std@base_encode", 2);
 
var msg = "Rosetta Code";
var result = SHA1(msg, 0, $ssize(msg));
 
/* Output in lowercase hex */
$print(base_encode(result, "0123456789abcdef"));</syntaxhighlight>
 
{{out}}
<pre>prompt$ nekoc SHA-1.neko
prompt$ neko SHA-1.n
48c98f7e5a6e736d790ab740dfc3f51a61abe2b5prompt$</pre>
 
=={{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 1,925 ⟶ 2,189:
return digestSum
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,935 ⟶ 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.
{{libheader|OpenSSL}}
Compile with <code>nim -d:ssl c sha1.nim</code>:
<lang nim>import strutils
 
<syntaxhighlight lang="nim">import std/sha1
const SHA1Len = 20
 
echo secureHash("Rosetta Code")</syntaxhighlight>
proc SHA1(d: cstring, n: culong, md: cstring = nil): cstring {.cdecl, dynlib: "libssl.so", importc.}
 
{{out}}
proc SHA1(s: string): string =
<pre>48C98F7E5A6E736D790AB740DFC3F51A61ABE2B5</pre>
result = ""
var s = SHA1(s.cstring, s.len.culong)
for i in 0 .. < SHA1Len:
result.add s[i].BiggestInt.toHex(2).toLower
 
echo SHA1("Rosetta Code")</lang>
 
=={{header|Oberon-2}}==
{{works with|oo2c}} {{libheader|crypto}}
<langsyntaxhighlight lang="oberon2">
MODULE SHA1;
IMPORT
Line 1,977 ⟶ 2,234:
Out.String("SHA1: ");Utils.PrintHex(str,0,h.size);Out.Ln
END SHA1.
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,988 ⟶ 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,004 ⟶ 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,015 ⟶ 2,272:
=={{header|Pascal}}==
{{works with|Free_Pascal}} {{libheader|sha1}}
<langsyntaxhighlight lang="pascal">program RosettaSha1;
uses
sha1;
Line 2,023 ⟶ 2,280:
d:=SHA1String('Rosetta Code');
WriteLn(SHA1Print(d));
end.</langsyntaxhighlight>
 
{{out}}
Line 2,030 ⟶ 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}}==
<!--<syntaxhighlight lang="phix">(phixonline)-->
<lang Phix>--
<span style="color: #000080;font-style:italic;">--
-- demo\rosetta\sha1.exw
-- demo\rosetta\sha1.exw
-- =====================
-- =====================
--
--
-- NB no longer considered secure. Non-optimised.
-- NB no longer considered secure. Non-optimised.
--
--</span>
constant m4 = allocate(4) -- scratch area, for uint32
<span style="color: #008080;">function</span> <span style="color: #000000;">uint32</span><span style="color: #0000FF;">(</span><span style="color: #004080;">atom</span> <span style="color: #000000;">v</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">return</span> <span style="color: #7060A8;">and_bitsu</span><span style="color: #0000FF;">(</span><span style="color: #000000;">v</span><span style="color: #0000FF;">,</span><span style="color: #000000;">#FFFFFFFF</span><span style="color: #0000FF;">)</span>
function uint32(atom v)
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
poke4(m4,v)
return peek4u(m4)
<span style="color: #008080;">function</span> <span style="color: #000000;">sq_uint32</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
end function
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">uint32</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">])</span>
function sq_uint32(sequence s)
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
for i=1 to length(s) do
<span style="color: #008080;">return</span> <span style="color: #000000;">s</span>
s[i] = uint32(s[i])
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
end for
return s
<span style="color: #008080;">function</span> <span style="color: #000000;">dword</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">msg</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">)</span>
end function
<span style="color: #000080;font-style:italic;">-- get dword as big-endian</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">msg</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]*</span><span style="color: #000000;">#1000000</span><span style="color: #0000FF;">+</span><span style="color: #000000;">msg</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]*</span><span style="color: #000000;">#10000</span><span style="color: #0000FF;">+</span><span style="color: #000000;">msg</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">+</span><span style="color: #000000;">2</span><span style="color: #0000FF;">]*</span><span style="color: #000000;">#100</span><span style="color: #0000FF;">+</span><span style="color: #000000;">msg</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">+</span><span style="color: #000000;">3</span><span style="color: #0000FF;">]</span>
function dword(string msg, integer i)
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
-- get dword as big-endian
return msg[i]*#1000000+msg[i+1]*#10000+msg[i+2]*#100+msg[i+3]
<span style="color: #008080;">function</span> <span style="color: #000000;">xor_all</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
end function
<span style="color: #004080;">atom</span> <span style="color: #000000;">result</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
function xor_all(sequence s)
<span style="color: #000000;">result</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">xor_bits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">result</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">])</span>
atom result = 0
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
for i=1 to length(s) do
<span style="color: #000000;">result</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">uint32</span><span style="color: #0000FF;">(</span><span style="color: #000000;">result</span><span style="color: #0000FF;">)</span>
result = xor_bits(result, s[i])
<span style="color: #008080;">return</span> <span style="color: #000000;">result</span>
end for
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
result = uint32(result)
return result
<span style="color: #008080;">function</span> <span style="color: #000000;">rol</span><span style="color: #0000FF;">(</span><span style="color: #004080;">atom</span> <span style="color: #000000;">word</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">bits</span><span style="color: #0000FF;">)</span>
end function
<span style="color: #000080;font-style:italic;">-- left rotate the bits of a 32-bit number by the specified number of bits</span>
 
<span style="color: #008080;">return</span> <span style="color: #000000;">uint32</span><span style="color: #0000FF;">(</span><span style="color: #000000;">word</span><span style="color: #0000FF;">*</span><span style="color: #7060A8;">power</span><span style="color: #0000FF;">(</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">bits</span><span style="color: #0000FF;">))+</span><span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">word</span><span style="color: #0000FF;">/</span><span style="color: #7060A8;">power</span><span style="color: #0000FF;">(</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">32</span><span style="color: #0000FF;">-</span><span style="color: #000000;">bits</span><span style="color: #0000FF;">))</span>
function rol(atom word, integer bits)
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
-- left rotate the bits of a 32-bit number by the specified number of bits
return uint32(word*power(2,bits))+floor(word/power(2,32-bits))
<span style="color: #008080;">function</span> <span style="color: #000000;">sha1</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">msg</span><span style="color: #0000FF;">)</span>
end function
<span style="color: #004080;">atom</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">,</span><span style="color: #000000;">b</span><span style="color: #0000FF;">,</span><span style="color: #000000;">c</span><span style="color: #0000FF;">,</span><span style="color: #000000;">d</span><span style="color: #0000FF;">,</span><span style="color: #000000;">e</span><span style="color: #0000FF;">,</span><span style="color: #000000;">temp</span><span style="color: #0000FF;">,</span><span style="color: #000000;">k</span>
 
<span style="color: #004080;">sequence</span> <span style="color: #000000;">w</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">80</span><span style="color: #0000FF;">)</span>
function sha1(string msg)
<span style="color: #004080;">atom</span> <span style="color: #000000;">h0</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0x67452301</span><span style="color: #0000FF;">,</span>
atom a,b,c,d,e,temp,k
<span style="color: #000000;">h1</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0xefcdab89</span><span style="color: #0000FF;">,</span>
sequence w = repeat(0,80)
<span style="color: #000000;">h2</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0x98badcfe</span><span style="color: #0000FF;">,</span>
atom h0 = 0x67452301,
<span style="color: #000000;">h3</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0x10325476</span><span style="color: #0000FF;">,</span>
h1 = 0xefcdab89,
<span style="color: #000000;">h4</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0xc3d2e1f0</span>
h2 = 0x98badcfe,
h3 = 0x10325476,
<span style="color: #004080;">integer</span> <span style="color: #000000;">bits</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">msg</span><span style="color: #0000FF;">)*</span><span style="color: #000000;">8</span>
h4 = 0xc3d2e1f0
<span style="color: #000000;">msg</span> <span style="color: #0000FF;">&=</span> <span style="color: #000000;">#80</span>
 
<span style="color: #008080;">while</span> <span style="color: #7060A8;">mod</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">msg</span><span style="color: #0000FF;">),</span><span style="color: #000000;">64</span><span style="color: #0000FF;">)!=</span><span style="color: #000000;">56</span> <span style="color: #008080;">do</span> <span style="color: #000000;">msg</span> <span style="color: #0000FF;">&=</span> <span style="color: #008000;">'\0'</span> <span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
integer bits = length(msg)*8
<span style="color: #000000;">msg</span> <span style="color: #0000FF;">&=</span> <span style="color: #7060A8;">reverse</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">int_to_bytes</span><span style="color: #0000FF;">(</span><span style="color: #000000;">bits</span><span style="color: #0000FF;">,</span><span style="color: #000000;">8</span><span style="color: #0000FF;">))</span>
msg &= #80
while mod(length(msg),64)!=56 do msg &= '\0' end while
<span style="color: #008080;">for</span> <span style="color: #000000;">chunk</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">msg</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">by</span> <span style="color: #000000;">64</span> <span style="color: #008080;">do</span>
msg &= reverse(int_to_bytes(bits,8))
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">16</span> <span style="color: #008080;">do</span>
 
<span style="color: #000000;">w</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">dword</span><span style="color: #0000FF;">(</span><span style="color: #000000;">msg</span><span style="color: #0000FF;">,</span><span style="color: #000000;">chunk</span><span style="color: #0000FF;">+(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)*</span><span style="color: #000000;">4</span><span style="color: #0000FF;">)</span>
for chunk=1 to length(msg) by 64 do
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
for i=1 to 16 do
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">17</span> <span style="color: #008080;">to</span> <span style="color: #000000;">80</span> <span style="color: #008080;">do</span>
w[i] = dword(msg,chunk+(i-1)*4)
<span style="color: #000000;">w</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">rol</span><span style="color: #0000FF;">(</span><span style="color: #000000;">xor_all</span><span style="color: #0000FF;">({</span><span style="color: #000000;">w</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">-</span><span style="color: #000000;">3</span><span style="color: #0000FF;">],</span><span style="color: #000000;">w</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">-</span><span style="color: #000000;">8</span><span style="color: #0000FF;">],</span><span style="color: #000000;">w</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">-</span><span style="color: #000000;">14</span><span style="color: #0000FF;">],</span><span style="color: #000000;">w</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">-</span><span style="color: #000000;">16</span><span style="color: #0000FF;">]}),</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
end for
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
for i=17 to 80 do
<span style="color: #0000FF;">{</span><span style="color: #000000;">a</span><span style="color: #0000FF;">,</span><span style="color: #000000;">b</span><span style="color: #0000FF;">,</span><span style="color: #000000;">c</span><span style="color: #0000FF;">,</span><span style="color: #000000;">d</span><span style="color: #0000FF;">,</span><span style="color: #000000;">e</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">h0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">h1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">h2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">h3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">h4</span><span style="color: #0000FF;">}</span>
w[i] = rol(xor_all({w[i-3],w[i-8],w[i-14],w[i-16]}),1)
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">80</span> <span style="color: #008080;">do</span>
end for
<span style="color: #008080;">if</span> <span style="color: #000000;">i</span><span style="color: #0000FF;"><=</span><span style="color: #000000;">20</span> <span style="color: #008080;">then</span>
{a,b,c,d,e} = {h0,h1,h2,h3,h4}
<span style="color: #000000;">temp</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">or_bits</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">and_bits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">b</span><span style="color: #0000FF;">,</span><span style="color: #000000;">c</span><span style="color: #0000FF;">),</span><span style="color: #7060A8;">and_bits</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">not_bits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">b</span><span style="color: #0000FF;">),</span><span style="color: #000000;">d</span><span style="color: #0000FF;">))</span>
for i=1 to 80 do
<span style="color: #000000;">k</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">#5A827999</span>
if i<=20 then
<span style="color: #008080;">elsif</span> <span style="color: #000000;">i</span><span style="color: #0000FF;"><=</span><span style="color: #000000;">40</span> <span style="color: #008080;">then</span>
temp = or_bits(and_bits(b,c),and_bits(not_bits(b),d))
<span style="color: #000000;">temp</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">xor_bits</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">xor_bits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">b</span><span style="color: #0000FF;">,</span><span style="color: #000000;">c</span><span style="color: #0000FF;">),</span><span style="color: #000000;">d</span><span style="color: #0000FF;">)</span>
k = #5A827999
<span style="color: #000000;">k</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">#6ED9EBA1</span>
elsif i<=40 then
<span style="color: #008080;">elsif</span> <span style="color: #000000;">i</span><span style="color: #0000FF;"><=</span><span style="color: #000000;">60</span> <span style="color: #008080;">then</span>
temp = xor_bits(xor_bits(b,c),d)
<span style="color: #000000;">temp</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">or_bits</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">or_bits</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">and_bits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">b</span><span style="color: #0000FF;">,</span><span style="color: #000000;">c</span><span style="color: #0000FF;">),</span><span style="color: #7060A8;">and_bits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">b</span><span style="color: #0000FF;">,</span><span style="color: #000000;">d</span><span style="color: #0000FF;">)),</span><span style="color: #7060A8;">and_bits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">c</span><span style="color: #0000FF;">,</span><span style="color: #000000;">d</span><span style="color: #0000FF;">))</span>
k = #6ED9EBA1
<span style="color: #000000;">k</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">#8F1BBCDC</span>
elsif i<=60 then
<span style="color: #008080;">else</span> <span style="color: #000080;font-style:italic;">-- i&lt;=80</span>
temp = or_bits(or_bits(and_bits(b,c),and_bits(b,d)),and_bits(c,d))
<span style="color: #000000;">temp</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">xor_bits</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">xor_bits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">b</span><span style="color: #0000FF;">,</span><span style="color: #000000;">c</span><span style="color: #0000FF;">),</span><span style="color: #000000;">d</span><span style="color: #0000FF;">)</span>
k = #8F1BBCDC
<span style="color: #000000;">k</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">#CA62C1D6</span>
else -- i<=80
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
temp = xor_bits(xor_bits(b,c),d)
<span style="color: #0000FF;">{</span><span style="color: #000000;">a</span><span style="color: #0000FF;">,</span><span style="color: #000000;">b</span><span style="color: #0000FF;">,</span><span style="color: #000000;">c</span><span style="color: #0000FF;">,</span><span style="color: #000000;">d</span><span style="color: #0000FF;">,</span><span style="color: #000000;">e</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">uint32</span><span style="color: #0000FF;">(</span><span style="color: #000000;">rol</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">)+</span><span style="color: #000000;">temp</span><span style="color: #0000FF;">+</span><span style="color: #000000;">e</span><span style="color: #0000FF;">+</span><span style="color: #000000;">k</span><span style="color: #0000FF;">+</span><span style="color: #000000;">w</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]),</span><span style="color: #000000;">a</span><span style="color: #0000FF;">,</span><span style="color: #000000;">rol</span><span style="color: #0000FF;">(</span><span style="color: #000000;">b</span><span style="color: #0000FF;">,</span><span style="color: #000000;">30</span><span style="color: #0000FF;">),</span><span style="color: #000000;">c</span><span style="color: #0000FF;">,</span><span style="color: #000000;">d</span><span style="color: #0000FF;">}</span>
k = #CA62C1D6
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
end if
<span style="color: #0000FF;">{</span><span style="color: #000000;">h0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">h1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">h2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">h3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">h4</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">sq_uint32</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">sq_add</span><span style="color: #0000FF;">({</span><span style="color: #000000;">h0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">h1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">h2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">h3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">h4</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">a</span><span style="color: #0000FF;">,</span><span style="color: #000000;">b</span><span style="color: #0000FF;">,</span><span style="color: #000000;">c</span><span style="color: #0000FF;">,</span><span style="color: #000000;">d</span><span style="color: #0000FF;">,</span><span style="color: #000000;">e</span><span style="color: #0000FF;">}))</span>
{a,b,c,d,e} = {uint32(rol(a,5)+temp+e+k+w[i]),a,rol(b,30),c,d}
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
end for
<span style="color: #004080;">sequence</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">h0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">h1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">h2</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">h3</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">h4</span><span style="color: #0000FF;">}</span>
{h0,h1,h2,h3,h4} = sq_uint32(sq_add({h0,h1,h2,h3,h4},{a,b,c,d,e}))
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
end for
<span style="color: #000000;">res</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"%08X"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">res</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">])</span>
sequence res = {h0, h1, h2, h3, h4}
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
for i=1 to length(res) do
<span style="color: #008080;">return</span> <span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">)</span>
res[i] = sprintf("%08X",res[i])
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
end for
return join(res)
<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>
end function
<!--</syntaxhighlight>-->
?sha1("Rosetta Code")</lang>
{{out}}
<pre>
Line 2,137 ⟶ 2,393:
 
=={{header|PHP}}==
<langsyntaxhighlight lang="php"><?php
$string = 'Rosetta Code';
echo sha1( $string ), "\n";
?></langsyntaxhighlight>
{{out}}
<pre>48c98f7e5a6e736d790ab740dfc3f51a61abe2b5</pre>
Line 2,146 ⟶ 2,402:
=={{header|PicoLisp}}==
Library and implementation.
<langsyntaxhighlight PicoLisplang="picolisp">(de leftRotate (X C)
(| (mod32 (>> (- C) X)) (>> (- 32 C) X)) )
 
Line 2,261 ⟶ 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,285 ⟶ 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,296 ⟶ 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,323 ⟶ 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,342 ⟶ 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,385 ⟶ 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,394 ⟶ 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,403 ⟶ 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,411 ⟶ 2,678:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
# Project : SHA-1
 
Line 2,419 ⟶ 2,686:
see "SHA-1: "
see sha1(str) + nl
</syntaxhighlight>
</lang>
Output:
<pre>
Line 2,430 ⟶ 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,504 ⟶ 2,771:
'Rosetta Code',
].each {|s| printf("%s:\n %s\n", s, *sha1(s).unpack('H*'))}
end</langsyntaxhighlight>
{{out}}
<pre>
Line 2,517 ⟶ 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,524 ⟶ 2,791:
println!("{}", hash_msg.digest().to_string());
}
</syntaxhighlight>
</lang>
Output
<pre>
Line 2,533 ⟶ 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,543 ⟶ 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,633 ⟶ 2,900:
println(hash("Rosetta Code"))
}
</syntaxhighlight>
</lang>
 
{{out}}
Line 2,641 ⟶ 2,908:
{{works with|Ol}}
 
<langsyntaxhighlight lang="scheme">
; band - binary AND operation
; bor - binary OR operation
Line 2,802 ⟶ 3,069:
(->32 (+ D d))
(->32 (+ E e)))))))))
</syntaxhighlight>
</lang>
 
{{out}}
<langsyntaxhighlight lang="scheme">
(define (->string value)
(runes->string
Line 2,821 ⟶ 3,088:
(print (->string (sha1:digest "")))
> da39a3ee5e6b4b0d3255bfef95601890afd80709
</syntaxhighlight>
</lang>
 
=={{header|Seed7}}==
<langsyntaxhighlight lang="seed7">$ include "seed7_05.s7i";
include "msgdigest.s7i";
 
Line 2,830 ⟶ 3,097:
begin
writeln(hex(sha1("Rosetta Code")));
end func;</langsyntaxhighlight>
 
{{out}}
Line 2,838 ⟶ 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,845 ⟶ 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,860 ⟶ 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}}
<syntaxhighlight lang="wren">import "./crypto" for Sha1
import "./fmt" for Fmt
var strings = [
"",
"a",
"abc",
"message digest",
"abcdefghijklmnopqrstuvwxyz",
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
"12345678901234567890123456789012345678901234567890123456789012345678901234567890",
"The quick brown fox jumps over the lazy dog",
"The quick brown fox jumps over the lazy cog",
"Rosetta Code"
]
for (s in strings) {
var hash = Sha1.digest(s)
Fmt.print("$s <== '$0s'", hash, s)
}</syntaxhighlight>
 
{{out}}
<pre>
da39a3ee5e6b4b0d3255bfef95601890afd80709 <== ''
86f7e437faa5a7fce15d1ddcb9eaeaea377667b8 <== 'a'
a9993e364706816aba3e25717850c26c9cd0d89d <== 'abc'
c12252ceda8be8994d5fa0290a47231c1d16aae3 <== 'message digest'
32d10c7b8cf96570ca04ce37f2a19d84240d3a89 <== 'abcdefghijklmnopqrstuvwxyz'
761c457bf73b14d27e9e9265c46f4b4dda11f940 <== 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
50abf5706a150990a08b2c5ea40fa0e585554732 <== '12345678901234567890123456789012345678901234567890123456789012345678901234567890'
2fd4e1c67a2d28fced849ee1bb76e7391b93eb12 <== 'The quick brown fox jumps over the lazy dog'
de9f2c7fd25e1b3afad3e85a0bd17d9b100db4b3 <== 'The quick brown fox jumps over the lazy cog'
48c98f7e5a6e736d790ab740dfc3f51a61abe2b5 <== 'Rosetta Code'
</pre>
 
=={{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,883 ⟶ 3,204:
 
zkl: MsgHash.SHA1("a"*1000,1000); // hash 1000 a's 1000 times
34aa973cd4c4daa4f61eeb2bdbad27316534016f</langsyntaxhighlight>
 
 
9,476

edits