SHA-1: Difference between revisions

70,248 bytes added ,  3 months ago
m
m (→‎{{header|Ring}}: Remove vanity tags)
m (→‎{{header|Wren}}: Minor tidy)
 
(45 intermediate revisions by 27 users not shown)
Line 14:
This is much faster than a brute force attack of 2<sup>80</sup> operations. USgovernment [http://csrc.nist.gov/groups/ST/hash/statement.html deprecated SHA-1].
For production-grade cryptography, users may consider a stronger alternative, such as SHA-256 (from the SHA-2 family) or the upcoming SHA-3.}}
=={{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 */
 
/*******************************************/
/* Constantes file */
/*******************************************/
/* for this file see task include a file in language AArch64 assembly*/
.include "../includeConstantesARM64.inc"
 
.equ SHA_DIGEST_LENGTH, 20
 
//.include "../../ficmacros64.s"
 
/*********************************/
/* Initialized data */
/*********************************/
.data
szMessRosetta: .asciz "Rosetta Code"
szMessTest1: .asciz "abc"
szMessSup64: .ascii "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
.ascii "abcdefghijklmnopqrstuvwxyz"
.asciz "1234567890AZERTYUIOP"
szMessTest2: .asciz "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
szMessFinPgm: .asciz "Program End ok.\n"
szMessResult: .asciz "Rosetta Code => "
szCarriageReturn: .asciz "\n"
 
/* array constantes Hi */
tbConstHi: .int 0x67452301 // H0
.int 0xEFCDAB89 // H1
.int 0x98BADCFE // H2
.int 0x10325476 // H3
.int 0xC3D2E1F0 // H4
/* array constantes Kt */
tbConstKt: .int 0x5A827999
.int 0x6ED9EBA1
.int 0x8F1BBCDC
.int 0xCA62C1D6
 
 
/*********************************/
/* UnInitialized data */
/*********************************/
.bss
.align 4
iNbBlocs: .skip 8
sZoneConv: .skip 24
sZoneResult: .skip 24
sZoneTrav: .skip 1000
tbH: .skip 4 * 5 // 5 variables H
tbW: .skip 4 * 80 // 80 words W
/*********************************/
/* code section */
/*********************************/
.text
.global main
main: // entry of program
ldr x0,qAdrszMessRosetta
//ldr x0,qAdrszMessTest1
//ldr x0,qAdrszMessTest2
//ldr x0,qAdrszMessSup64
bl computeSHA1 // call routine SHA1
 
ldr x0,qAdrszMessResult
bl affichageMess // display message
 
ldr x0, qAdrsZoneResult
bl displaySHA1
 
ldr x0,qAdrszMessFinPgm
bl affichageMess // display message
 
100: // standard end of the program
mov x0,0 // return code
mov x8,EXIT // request to exit program
svc 0 // perform the system call
qAdrszCarriageReturn: .quad szCarriageReturn
qAdrszMessResult: .quad szMessResult
qAdrszMessRosetta: .quad szMessRosetta
qAdrszMessTest1: .quad szMessTest1
qAdrszMessTest2: .quad szMessTest2
qAdrsZoneTrav: .quad sZoneTrav
qAdrsZoneConv: .quad sZoneConv
qAdrszMessFinPgm: .quad szMessFinPgm
qAdrszMessSup64: .quad szMessSup64
/******************************************************************/
/* compute SHA1 */
/******************************************************************/
/* x0 contains the address of the message */
computeSHA1:
stp x1,lr,[sp,-16]! // save registers
ldr x1,qAdrsZoneTrav
mov x2,#0 // counter length
debCopy: // copy string in work area
ldrb w3,[x0,x2]
strb w3,[x1,x2]
cmp x3,#0
add x4,x2,1
csel x2,x4,x2,ne
bne debCopy
lsl x6,x2,#3 // initial message length in bits
mov x3,#0b10000000 // add bit 1 at end of string
strb w3,[x1,x2]
add x2,x2,#1 // length in bytes
lsl x4,x2,#3 // length in bits
mov x3,#0
addZeroes:
lsr x5,x2,#6
lsl x5,x5,#6
sub x5,x2,x5
cmp x5,#56
beq storeLength // yes -> end add
strb w3,[x1,x2] // add zero at message end
add x2,x2,#1 // increment lenght bytes
add x4,x4,#8 // increment length in bits
b addZeroes
storeLength:
add x2,x2,#4 // add four bytes
rev w6,w6 // inversion bits initials message length
str w6,[x1,x2] // and store at end
ldr x7,qAdrtbConstHi // constantes H address
ldr x4,qAdrtbH // start area H
mov x5,#0
loopConst: // init array H with start constantes
ldr w6,[x7,x5,lsl #2] // load constante
str w6,[x4,x5,lsl #2] // and store
add x5,x5,#1
cmp x5,#5
blt loopConst
// split into block of 64 bytes
add x2,x2,#4 // TODO : à revoir
lsr x4,x2,#6 // blocks number
ldr x0,qAdriNbBlocs
str x4,[x0] // save block maxi
mov x7,#0 // n° de block et x1 contient l'adresse zone de travail
loopBlock: // begin loop of each block of 64 bytes
mov x0,x7
bl inversion // inversion each word because little indian
ldr x3,qAdrtbW // working area W address
mov x6,#0 // indice t
/* x2 address begin each block */
ldr x1,qAdrsZoneTrav
add x2,x1,x7,lsl #6 // compute block begin indice * 4 * 16
 
loopPrep: // loop for expand 80 words
cmp x6,#15 //
bgt expand1
ldr w0,[x2,x6,lsl #2] // load four byte message
str w0,[x3,x6,lsl #2] // store in first 16 block
b expandEnd
expand1:
sub x8,x6,#3
ldr w9,[x3,x8,lsl #2]
sub x8,x6,#8
ldr w10,[x3,x8,lsl #2]
eor x9,x9,x10
sub x8,x6,#14
ldr w10,[x3,x8,lsl #2]
eor x9,x9,x10
sub x8,x6,#16
ldr w10,[x3,x8,lsl #2]
eor x9,x9,x10
ror w9,w9,#31
 
str w9,[x3,x6,lsl #2]
expandEnd:
add x6,x6,#1
cmp x6,#80 // 80 words ?
blt loopPrep // and loop
/* COMPUTING THE MESSAGE DIGEST */
/* x1 area H constantes address */
/* x3 working area W address */
/* x5 address constantes K */
/* x6 counter t */
/* x7 block counter */
/* x8 a, x9 b, x10 c, x11 d, x12 e */
 
// init variable a b c d e
ldr x0,qAdrtbH
ldr w8,[x0]
ldr w9,[x0,#4]
ldr w10,[x0,#8]
ldr w11,[x0,#12]
ldr w12,[x0,#16]
ldr x1,qAdrtbConstHi
ldr x5,qAdrtbConstKt
mov x6,#0
loop80T: // begin loop 80 t
cmp x6,#19
bgt T2
ldr w0,[x5] // load constantes k0
and x2,x9,x10 // b and c
mvn w4,w9 // not b
and x4,x4,x11 // and d
orr x2,x2,x4
b T_fin
T2:
cmp x6,#39
bgt T3
ldr w0,[x5,#4] // load constantes k1
eor x2,x9,x10
eor x2,x2,x11
b T_fin
T3:
cmp x6,#59
bgt T4
ldr w0,[x5,#8] // load constantes k2
and x2,x9,x10
and x4,x9,x11
orr x2,x2,x4
and x4,x10,x11
orr x2,x2,x4
b T_fin
T4:
ldr w0,[x5,#12] // load constantes k3
eor x2,x9,x10
eor x2,x2,x11
b T_fin
T_fin:
ror w4,w8,#27 // left rotate a to 5
add w2,w2,w4
//affregtit Tfin 0
//affregtit Tfin 8
add w2,w2,w12
ldr w4,[x3,x6,lsl #2] // Wt
add w2,w2,w4
add w2,w2,w0 // Kt
mov x12,x11 // e = d
mov x11,x10 // d = c
ror w10,w9,#2 // c
mov x9,x8 // b = a
mov x8,x2 // nouveau a
 
add x6,x6,#1 // increment t
cmp x6,#80
blt loop80T
// other bloc
add x7,x7,1 // increment block
ldr x0,qAdriNbBlocs
ldr w4,[x0] // restaur maxi block
cmp x7,x4 // maxi ?
bge End
// End block
ldr x0,qAdrtbH // start area H
ldr w3,[x0]
add w3,w3,w8
str w3,[x0] // store a in H0
ldr w3,[x0,#4]
add w3,w3,w9
str w3,[x0,#4] // store b in H1
ldr w3,[x0,#8]
add w3,w3,w10
str w3,[x0,#8] // store c in H2
ldr w3,[x0,#12]
add w3,w3,w11
str w3,[x0,#12] // store d in H3
ldr w3,[x0,#16]
add w3,w3,w12
str w3,[x0,#16] // store e in H4
b loopBlock // loop
 
End:
// compute final result
ldr x0,qAdrtbH // start area H
ldr x2,qAdrsZoneResult
ldr w1,[x0]
add x1,x1,x8
rev w1,w1
str w1,[x2]
ldr w1,[x0,#4]
add x1,x1,x9
rev w1,w1
str w1,[x2,#4]
ldr w1,[x0,#8]
add x1,x1,x10
rev w1,w1
str w1,[x2,#8]
ldr w1,[x0,#12]
add x1,x1,x11
rev w1,w1
str w1,[x2,#12]
ldr w1,[x0,#16]
add x1,x1,x12
rev w1,w1
str w1,[x2,#16]
mov x0,#0 // routine OK
100:
 
ldp x1,lr,[sp],16 // restaur 2 registers
ret // return to address lr x30
qAdrtbConstHi: .quad tbConstHi
qAdrtbConstKt: .quad tbConstKt
qAdrtbH: .quad tbH
qAdrtbW: .quad tbW
qAdrsZoneResult: .quad sZoneResult
qAdriNbBlocs: .quad iNbBlocs
/******************************************************************/
/* inversion des mots de 32 bits d'un bloc */
/******************************************************************/
/* x0 contains N° block */
inversion:
stp x1,lr,[sp,-16]! // save registers
stp x2,x3,[sp,-16]! // save registers
ldr x1,qAdrsZoneTrav
add x1,x1,x0,lsl 6 // debut du bloc
mov x2,#0
1: // start loop
ldr w3,[x1,x2,lsl #2]
rev w3,w3
str w3,[x1,x2,lsl #2]
add x2,x2,#1
cmp x2,#16
blt 1b
100:
ldp x2,x3,[sp],16 // restaur 2 registers
ldp x1,lr,[sp],16 // restaur 2 registers
ret // return to address lr x30
/******************************************************************/
/* display hash SHA1 */
/******************************************************************/
/* x0 contains the address of hash */
displaySHA1:
stp x1,lr,[sp,-16]! // save registers
stp x2,x3,[sp,-16]! // save registers
mov x3,x0
mov x2,#0
1:
ldr w0,[x3,x2,lsl #2] // load 4 bytes
rev w0,w0 // reverse bytes
ldr x1,qAdrsZoneConv
bl conversion16_4W // conversion hexa
ldr x0,qAdrsZoneConv
bl affichageMess
add x2,x2,#1
cmp x2,#SHA_DIGEST_LENGTH / 4
blt 1b // and loop
ldr x0,qAdrszCarriageReturn
bl affichageMess // display message
100:
ldp x2,x3,[sp],16 // restaur 2 registers
ldp x1,lr,[sp],16 // restaur 2 registers
ret // return to address lr x30
/******************************************************************/
/* conversion hexadecimal register 32 bits */
/******************************************************************/
/* x0 contains value and x1 address zone receptrice */
conversion16_4W:
stp x0,lr,[sp,-48]! // save registres
stp x1,x2,[sp,32] // save registres
stp x3,x4,[sp,16] // save registres
mov x2,#28 // start bit position
mov x4,#0xF0000000 // mask
mov x3,x0 // save entry value
1: // start loop
and x0,x3,x4 // value register and mask
lsr x0,x0,x2 // right shift
cmp x0,#10 // >= 10 ?
bge 2f // yes
add x0,x0,#48 // no is digit
b 3f
2:
add x0,x0,#55 // else is a letter A-F
3:
strb w0,[x1],#1 // load result and + 1 in address
lsr x4,x4,#4 // shift mask 4 bits left
subs x2,x2,#4 // decrement counter 4 bits <= zero ?
bge 1b // no -> loop
 
100: // fin standard de la fonction
ldp x3,x4,[sp,16] // restaur des 2 registres
ldp x1,x2,[sp,32] // restaur des 2 registres
ldp x0,lr,[sp],48 // restaur des 2 registres
ret
/********************************************************/
/* File Include fonctions */
/********************************************************/
/* for this file see task include a file in language AArch64 assembly */
.include "../includeARM64.inc"
</syntaxhighlight>
{{Output}}
<pre>
Rosetta Code => 48C98F7E5A6E736D790AB740DFC3F51A61ABE2B5
Program End ok.
</pre>
=={{header|Ada}}==
{{works with|GNAT}}
 
<langsyntaxhighlight Adalang="ada">with Ada.Text_IO;
with GNAT.SHA1;
 
Line 25 ⟶ 416:
Ada.Text_IO.Put_Line ("SHA1 (""Rosetta Code"") = " &
GNAT.SHA1.Digest ("Rosetta Code"));
end Main;</langsyntaxhighlight>
 
{{out}}
<pre>SHA1 ("Rosetta Code") = 48c98f7e5a6e736d790ab740dfc3f51a61abe2b5</pre>
 
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi}}
<B>with openssl library </B>
<syntaxhighlight lang="arm assembly">
 
/* ARM assembly Raspberry PI */
/* program sha1-1.s */
/* use with library openssl */
/* link with gcc option -lcrypto -lssl */
 
/* REMARK 1 : this program use routines in a include file
see task Include a file language arm assembly
for the routine affichageMess conversion10
see at end of this program the instruction include */
 
/* for constantes see task include a file in arm assembly */
/************************************/
/* Constantes */
/************************************/
.include "../constantes.inc"
 
.equ SHA_DIGEST_LENGTH, 20
 
/*******************************************/
/* Fichier des macros */
/********************************************/
.include "../../ficmacros.s"
 
/*********************************/
/* Initialized data */
/*********************************/
.data
szMessRosetta: .asciz "Rosetta Code"
.equ LGMESSROSETTA, . - szMessRosetta - 1
szCarriageReturn: .asciz "\n"
szMessSup64: .ascii "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
.ascii "abcdefghijklmnopqrstuvwxyz"
.asciz "1234567890AZERTYUIOP"
.equ LGMESSSUP64, . - szMessSup64 - 1
szMessTest2: .asciz "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
.equ LGMESSTEST2, . - szMessTest2 - 1
 
/*********************************/
/* UnInitialized data */
/*********************************/
.bss
.align 4
szMessResult: .skip 24
sZoneConv: .skip 24
/*********************************/
/* code section */
/*********************************/
.text
.global main
main: @ entry of program
 
ldr r0,iAdrszMessRosetta
mov r1,#LGMESSROSETTA
ldr r2,iAdrszMessResult
bl SHA1 @ appel fonction openssl
ldr r0,iAdrszMessResult
bl displaySHA1
 
100: @ standard end of the program
mov r0, #0 @ return code
mov r7, #EXIT @ request to exit program
svc #0 @ perform the system call
iAdrszMessRosetta: .int szMessRosetta
iAdrszCarriageReturn: .int szCarriageReturn
iAdrszMessResult: .int szMessResult
iAdrsZoneConv: .int sZoneConv
iAdrszMessSup64: .int szMessSup64
iAdrszMessTest2: .int szMessTest2
/******************************************************************/
/* display hash SHA1 */
/******************************************************************/
/* r0 contains the address of hash */
displaySHA1:
push {r1-r3,lr} @ save registres
mov r3,r0
mov r2,#0
1:
ldr r0,[r3,r2,lsl #2] @ load 4 bytes
rev r0,r0 @ reverse bytes
ldr r1,iAdrsZoneConv
bl conversion16 @ conversion hexa
ldr r0,iAdrsZoneConv
bl affichageMess
add r2,r2,#1
cmp r2,#SHA_DIGEST_LENGTH / 4
blt 1b @ and loop
ldr r0,iAdrszCarriageReturn
bl affichageMess @ display message
100:
pop {r1-r3,lr} @ restaur registers
bx lr @ return
/***************************************************/
/* ROUTINES INCLUDE */
/***************************************************/
.include "../affichage.inc"
</syntaxhighlight>
<B>with only instructions assembly ARM</B>
<syntaxhighlight lang="text">
/* ARM assembly Raspberry PI */
/* program sha1.s */
 
/* REMARK 1 : this program use routines in a include file
see task Include a file language arm assembly
for the routine affichageMess conversion10
see at end of this program the instruction include */
/* for constantes see task include a file in arm assembly */
/************************************/
/* Constantes */
/************************************/
.include "../constantes.inc"
 
.equ SHA_DIGEST_LENGTH, 20
 
.include "../../ficmacros.s"
 
/*********************************/
/* Initialized data */
/*********************************/
.data
szMessRosetta: .asciz "Rosetta Code"
szMessTest1: .asciz "abc"
szMessSup64: .ascii "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
.ascii "abcdefghijklmnopqrstuvwxyz"
.asciz "1234567890AZERTYUIOP"
szMessTest2: .asciz "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
szMessFinPgm: .asciz "Program End ok.\n"
szMessResult: .asciz "Rosetta Code => "
szCarriageReturn: .asciz "\n"
 
/* array constantes Hi */
tbConstHi: .int 0x67452301 @ H0
.int 0xEFCDAB89 @ H1
.int 0x98BADCFE @ H2
.int 0x10325476 @ H3
.int 0xC3D2E1F0 @ H4
/* array constantes Kt */
tbConstKt: .int 0x5A827999
.int 0x6ED9EBA1
.int 0x8F1BBCDC
.int 0xCA62C1D6
 
 
/*********************************/
/* UnInitialized data */
/*********************************/
.bss
.align 4
iNbBlocs: .skip 4
sZoneConv: .skip 24
sZoneResult: .skip 24
sZoneTrav: .skip 1000
tbH: .skip 4 * 5 @ 5 variables H
tbW: .skip 4 * 80 @ 80 words W
/*********************************/
/* code section */
/*********************************/
.text
.global main
main: @ entry of program
ldr r0,iAdrszMessRosetta
//ldr r0,iAdrszMessTest1
//ldr r0,iAdrszMessTest2
//ldr r0,iAdrszMessSup64
bl computeSHA1 @ call routine SHA1
 
ldr r0,iAdrszMessResult
bl affichageMess @ display message
 
ldr r0, iAdrsZoneResult
bl displaySHA1
 
ldr r0,iAdrszMessFinPgm
bl affichageMess @ display message
 
100: @ standard end of the program
mov r0, #0 @ return code
mov r7, #EXIT @ request to exit program
svc #0 @ perform the system call
iAdrszCarriageReturn: .int szCarriageReturn
iAdrszMessResult: .int szMessResult
iAdrszMessRosetta: .int szMessRosetta
iAdrszMessTest1: .int szMessTest1
iAdrszMessTest2: .int szMessTest2
iAdrsZoneTrav: .int sZoneTrav
iAdrsZoneConv: .int sZoneConv
iAdrszMessFinPgm: .int szMessFinPgm
iAdrszMessSup64: .int szMessSup64
/******************************************************************/
/* compute SHA1 */
/******************************************************************/
/* r0 contains the address of the message */
computeSHA1:
push {r1-r12,lr} @ save registres
ldr r1,iAdrsZoneTrav
mov r2,#0 @ counter length
debCopy: @ copy string in work area
ldrb r3,[r0,r2]
strb r3,[r1,r2]
cmp r3,#0
addne r2,r2,#1
bne debCopy
lsl r6,r2,#3 @ initial message length in bits
mov r3,#0b10000000 @ add bit 1 at end of string
strb r3,[r1,r2]
add r2,r2,#1 @ length in bytes
lsl r4,r2,#3 @ length in bits
mov r3,#0
addZeroes:
lsr r5,r2,#6
lsl r5,r5,#6
sub r5,r2,r5
cmp r5,#56
beq storeLength @ yes -> end add
strb r3,[r1,r2] @ add zero at message end
add r2,#1 @ increment lenght bytes
add r4,#8 @ increment length in bits
b addZeroes
storeLength:
add r2,#4 @ add four bytes
rev r6,r6 @ inversion bits initials message length
str r6,[r1,r2] @ and store at end
ldr r7,iAdrtbConstHi @ constantes H address
ldr r4,iAdrtbH @ start area H
mov r5,#0
loopConst: @ init array H with start constantes
ldr r6,[r7,r5,lsl #2] @ load constante
str r6,[r4,r5,lsl #2] @ and store
add r5,r5,#1
cmp r5,#5
blt loopConst
@ split into block of 64 bytes
add r2,#4 @ TODO : à revoir
lsr r4,r2,#6 @ blocks number
ldr r0,iAdriNbBlocs
str r4,[r0] @ save block maxi
mov r7,#0 @ n° de block et r1 contient adresse zone de travail
loopBlock: @ begin loop of each block of 64 bytes
mov r0,r7
bl inversion @ inversion each word because little indian
ldr r3,iAdrtbW @ working area W address
mov r6,#0 @ indice t
/* r2 address begin each block */
ldr r1,iAdrsZoneTrav
add r2,r1,r7,lsl #6 @ compute block begin indice * 4 * 16
//vidregtit avantloop
//mov r0,r2
//vidmemtit verifBloc r0 10
loopPrep: @ loop for expand 80 words
cmp r6,#15 @
bgt expand1
ldr r0,[r2,r6,lsl #2] @ load byte message
str r0,[r3,r6,lsl #2] @ store in first 16 block
b expandEnd
expand1:
sub r8,r6,#3
ldr r9,[r3,r8,lsl #2]
sub r8,r6,#8
ldr r10,[r3,r8,lsl #2]
eor r9,r9,r10
sub r8,r6,#14
ldr r10,[r3,r8,lsl #2]
eor r9,r9,r10
sub r8,r6,#16
ldr r10,[r3,r8,lsl #2]
eor r9,r9,r10
ror r9,r9,#31
 
str r9,[r3,r6,lsl #2]
expandEnd:
add r6,r6,#1
cmp r6,#80 @ 80 words ?
blt loopPrep @ and loop
/* COMPUTING THE MESSAGE DIGEST */
/* r1 area H constantes address */
/* r3 working area W address */
/* r5 address constantes K */
/* r6 counter t */
/* r7 block counter */
/* r8 a, r9 b, r10 c, r11 d, r12 e */
//ldr r0,iAdrtbW
//vidmemtit verifW80 r0 20
@ init variable a b c d e
ldr r0,iAdrtbH
ldr r8,[r0]
ldr r9,[r0,#4]
ldr r10,[r0,#8]
ldr r11,[r0,#12]
ldr r12,[r0,#16]
ldr r1,iAdrtbConstHi
ldr r5,iAdrtbConstKt
mov r6,#0
loop80T: @ begin loop 80 t
cmp r6,#19
bgt T2
ldr r0,[r5] @ load constantes k0
and r2,r9,r10 @ b and c
mvn r4,r9 @ not b
and r4,r4,r11 @ and d
orr r2,r2,r4
b T_fin
T2:
cmp r6,#39
bgt T3
ldr r0,[r5,#4] @ load constantes k1
eor r2,r9,r10
eor r2,r11
b T_fin
T3:
cmp r6,#59
bgt T4
ldr r0,[r5,#8] @ load constantes k2
and r2,r9,r10
and r4,r9,r11
orr r2,r4
and r4,r10,r11
orr r2,r4
b T_fin
T4:
ldr r0,[r5,#12] @ load constantes k3
eor r2,r9,r10
eor r2,r11
b T_fin
T_fin:
ror r4,r8,#27 @ left rotate a to 5
add r2,r4
add r2,r12
ldr r4,[r3,r6,lsl #2] @ Wt
add r2,r4
add r2,r0 @ Kt
mov r12,r11 @ e = d
mov r11,r10 @ d = c
ror r10,r9,#2 @ c
mov r9,r8 @ b = a
mov r8,r2 @ nouveau a
 
add r6,r6,#1 @ increment t
cmp r6,#80
blt loop80T
@ other bloc
add r7,#1 @ increment block
ldr r0,iAdriNbBlocs
ldr r4,[r0] @ restaur maxi block
cmp r7,r4 @ maxi ?
bge End
@ End block
ldr r0,iAdrtbH @ start area H
ldr r3,[r0]
add r3,r8
str r3,[r0] @ store a in H0
ldr r3,[r0,#4]
add r3,r9
str r3,[r0,#4] @ store b in H1
ldr r3,[r0,#8]
add r3,r10
str r3,[r0,#8] @ store c in H2
ldr r3,[r0,#12]
add r3,r11
str r3,[r0,#12] @ store d in H3
ldr r3,[r0,#16]
add r3,r12
str r3,[r0,#16] @ store e in H4
b loopBlock @ loop
 
End:
@ compute final result
ldr r0,iAdrtbH @ start area H
ldr r2,iAdrsZoneResult
ldr r1,[r0]
add r1,r8
rev r1,r1
str r1,[r2]
ldr r1,[r0,#4]
add r1,r9
rev r1,r1
str r1,[r2,#4]
ldr r1,[r0,#8]
add r1,r10
rev r1,r1
str r1,[r2,#8]
ldr r1,[r0,#12]
add r1,r11
rev r1,r1
str r1,[r2,#12]
ldr r1,[r0,#16]
add r1,r12
rev r1,r1
str r1,[r2,#16]
mov r0,#0 @ routine OK
100:
pop {r1-r12,lr} @ restaur registers
bx lr @ return
iAdrtbConstHi: .int tbConstHi
iAdrtbConstKt: .int tbConstKt
iAdrtbH: .int tbH
iAdrtbW: .int tbW
iAdrsZoneResult: .int sZoneResult
iAdriNbBlocs: .int iNbBlocs
/******************************************************************/
/* inversion des mots de 32 bits d un bloc */
/******************************************************************/
/* r0 contains N° block */
inversion:
push {r1-r3,lr} @ save registers
ldr r1,iAdrsZoneTrav
add r1,r0,lsl #6 @ debut du bloc
mov r2,#0
1: @ start loop
ldr r3,[r1,r2,lsl #2]
rev r3,r3
str r3,[r1,r2,lsl #2]
add r2,r2,#1
cmp r2,#16
blt 1b
100:
pop {r1-r3,lr} @ restaur registres
bx lr @return
/******************************************************************/
/* display hash SHA1 */
/******************************************************************/
/* r0 contains the address of hash */
displaySHA1:
push {r1-r3,lr} @ save registres
mov r3,r0
mov r2,#0
1:
ldr r0,[r3,r2,lsl #2] @ load 4 bytes
rev r0,r0 @ reverse bytes
ldr r1,iAdrsZoneConv
bl conversion16 @ conversion hexa
ldr r0,iAdrsZoneConv
bl affichageMess
add r2,r2,#1
cmp r2,#SHA_DIGEST_LENGTH / 4
blt 1b @ and loop
ldr r0,iAdrszCarriageReturn
bl affichageMess @ display message
100:
pop {r1-r3,lr} @ restaur registers
bx lr @ return
/***************************************************/
/* ROUTINES INCLUDE */
/***************************************************/
.include "../affichage.inc"
 
</syntaxhighlight>
{{Output}}
<pre>
Rosetta Code => 48C98F7E5A6E736D790AB740DFC3F51A61ABE2B5
Program End ok.
</pre>
=={{header|Arturo}}==
 
<syntaxhighlight lang="rebol">print digest.sha "The quick brown fox jumped over the lazy dog's back"</syntaxhighlight>
 
{{out}}
 
<pre>9f2cbbcba105f1390db79a263af5bf9554f935a4</pre>
 
=={{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 89 ⟶ 948:
StrPut(string, &data, floor(length / chrlength), encoding)
return CalcAddrHash(&data, length, algid, hash, hashlength)
}</langsyntaxhighlight>
{{out}}
<pre>String: Rosetta Code
Line 97 ⟶ 956:
===Library===
{{works with|BBC BASIC for Windows}}
<langsyntaxhighlight lang="bbcbasic"> PRINT FNsha1("Rosetta Code")
END
Line 117 ⟶ 976:
hash$ += RIGHT$("0" + STR$~buffer%?i%, 2)
NEXT
= hash$</langsyntaxhighlight>
{{out}}
<pre>
Line 125 ⟶ 984:
===Native===
{{works with|BBC BASIC for Windows}}
<langsyntaxhighlight lang="bbcbasic"> *FLOAT64
PRINT FNsha1("Rosetta Code")
END
Line 226 ⟶ 1,085:
WHILE n# > &7FFFFFFF : n# -= 2^32 : ENDWHILE
WHILE n# < &80000000 : n# += 2^32 : ENDWHILE
= n#</langsyntaxhighlight>
{{out}}
<pre>
Line 234 ⟶ 1,093:
=={{header|C}}==
{{libheader|OpenSSL}}
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Line 251 ⟶ 1,110:
return EXIT_SUCCESS;
}</langsyntaxhighlight>
 
=={{header|C sharp}}==
Tests the built-in SHA1CryptoServiceProvider:
<syntaxhighlight lang="csharp">using System;
using System.Security.Cryptography;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
 
namespace RosettaCode.SHA1
{
[TestClass]
public class SHA1CryptoServiceProviderTest
{
[TestMethod]
public void TestComputeHash()
{
var input = new UTF8Encoding().GetBytes("Rosetta Code");
var output = new SHA1CryptoServiceProvider().ComputeHash(input);
Assert.AreEqual(
"48-C9-8F-7E-5A-6E-73-6D-79-0A-B7-40-DF-C3-F5-1A-61-AB-E2-B5",
BitConverter.ToString(output));
}
}
}</syntaxhighlight>
 
=={{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 275 ⟶ 1,158:
<< " !" << std::endl ;
return 0 ;
}</langsyntaxhighlight>
{{out}}
<pre>Rosetta Code as a sha1 digest :48c98f7e5a6e736d790ab740dfc3f51a61abe2b5 !</pre>
 
===Without external libraries===
=={{header|C sharp}}==
<syntaxhighlight lang="c++">
Tests the built-in SHA1CryptoServiceProvider:
#include <bit>
<lang csharp>using System;
#include <cstdint>
using System.Security.Cryptography;
#include <iomanip>
using System.Text;
#include <iostream>
using Microsoft.VisualStudio.TestTools.UnitTesting;
#include <sstream>
#include <string>
#include <vector>
 
class SHA1 {
namespace RosettaCode.SHA1
public:
{
std::string message_digest(const std::string& message) {
[TestClass]
std::vector<uint32_t> state = { 0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0 };
public class SHA1CryptoServiceProviderTest
 
{
const std::vector<int8_t> bytes = add_padding(message);
[TestMethod]
for ( uint64_t i = 0; i < bytes.size() / BLOCK_LENGTH; ++i ) {
public void TestComputeHash()
std::vector<uint32_t> values(80, 0);
{
for ( uint32_t j = 0; j < BLOCK_LENGTH; ++j ) {
var input = new UTF8Encoding().GetBytes("Rosetta Code");
values[j / 4] |= ( bytes[i * BLOCK_LENGTH + j] & 0xff ) << ( ( 3 - j % 4 ) * 8 );
var output = new SHA1CryptoServiceProvider().ComputeHash(input);
}
Assert.AreEqual(
for ( uint32_t j = 16; j < 80; ++j ) {
"48-C9-8F-7E-5A-6E-73-6D-79-0A-B7-40-DF-C3-F5-1A-61-AB-E2-B5",
uint32_t value = values[j - 3] ^ values[j - 8] ^ values[j - 14] ^ values[j - 16];
BitConverter.ToString(output));
values[j] = std::rotl(value, 1);
}
}
}
 
}</lang>
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 317 ⟶ 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 323 ⟶ 1,264:
(ironclad:update-digest sha1 bin-data)
(ironclad:byte-array-to-hex-string (ironclad:produce-digest sha1))))
</syntaxhighlight>
</lang>
 
=={{header|Crystal}}==
<syntaxhighlight lang="ruby">require "openssl"
puts OpenSSL::Digest.new("sha1").update("Rosetta Code")</syntaxhighlight>
{{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
<syntaxhighlight lang="d">import std.stdio, std.string, std.conv, std.algorithm, std.format, std.array,
std.range, std.digest.sha;
 
int rol(int n, int b) {
return ((n << b) | (n >>> (32 - b))) & 0xffffffff;
}
 
int btoi(string bin) {
int total = 0;
foreach (b; bin) {
total *= 2;
(b == '1') ? total += 1 : total;
}
return total;
}
 
string sha1(char[] intake) {
int h0 = 0x67452301;
int h1 = 0xEFCDAB89;
int h2 = 0x98BADCFE;
int h3 = 0x10325476;
int h4 = 0xC3D2E1F0;
 
auto bins = intake.map!(x => format("%08b", x.to!int));
int binsize = bins.join().length.to!int;
string o = bins.join() ~ "1";
o ~= replicate("0", 448%512 - o.length.to!int%512) ~ format("%064b", binsize);
auto binchunks = chunks(o, 512).array;
foreach (chunk; binchunks) {
string[] words = chunk.chunks(512/16).array
.map!(x => "%032s".format(x)).array;
foreach (i; iota(16, 80)) {
int newWord = btoi(words[i-3]) ^ btoi(words[i-8]) ^
btoi(words[i-14]) ^ btoi(words[i-16]);
newWord = rol(newWord, 1);
words = words.array ~ "%032b".format(newWord);
}
int A = h0;
int B = h1;
int C = h2;
int D = h3;
int E = h4;
foreach (i; iota(0, 80)) {
int F = 0;
int K = 0;
if (i < 20) {
F = D ^ (B & (C ^ D));
K = 0x5A827999;
}
else if (i < 40) {
F = B ^ C ^ D;
K = 0x6ED9EBA1;
}
else if (i < 60) {
F = (B & C) | (B & D) | (C & D);
K = 0x8F1BBCDC;
}
else if (i < 80) {
F = B ^ C ^ D;
K = 0xCA62C1D6;
}
int tempA = A;
A = rol(A, 5) + F + E + K + btoi(words[i]) & 0xffffffff;
E = D;
D = C;
C = rol(B,30);
B = tempA;
}
 
h0 = btoi("%032b".format(h0 + A).retro.array[0 .. 32].retro.to!string);
h1 = btoi("%032b".format(h1 + B).retro.array[0 .. 32].retro.to!string);
h2 = btoi("%032b".format(h2 + C).retro.array[0 .. 32].retro.to!string);
h3 = btoi("%032b".format(h3 + D).retro.array[0 .. 32].retro.to!string);
h4 = btoi("%032b".format(h4 + E).retro.array[0 .. 32].retro.to!string);
}
return "%08x%08x%08x%08x%08x".format(h0, h1, h2, h3, h4);
}
 
void main() {
writeln(sha1("Rosetta Code".dup));
}</syntaxhighlight>
{{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 342 ⟶ 1,412:
b18c883f4da750164b5af362ea9b9f27f90904b4
</pre>
 
=={{header|Elixir}}==
Uses Erlang module 'crypto'
{{trans|Erlang}}
<syntaxhighlight 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>
 
=={{header|Emacs Lisp}}==
<syntaxhighlight lang="lisp">
(sha1 "Rosetta Code") ;=> "48c98f7e5a6e736d790ab740dfc3f51a61abe2b5"
(secure-hash 'sha1 "Rosetta Code") ;=> "48c98f7e5a6e736d790ab740dfc3f51a61abe2b5"
</syntaxhighlight>
 
=={{header|Erlang}}==
Line 351 ⟶ 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>
48 c9 8f 7e 5a 6e 73 6d 79 a b7 40 df c3 f5 1a 61 ab e2 b5
</pre>
 
=={{header|Factor}}==
Factor provides ''sha1'' in the ''checksums.sha'' vocabulary. In Factor, ''checksum-bytes'' returns a [[sequence]] of bytes; ''hex-string'' converts this sequence to a hexadecimal string.
Line 374 ⟶ 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_msha1_mod
use kernel32
use advapi32
Line 390 ⟶ 1,476:
integer(BYTE) :: hash(SHA1LEN)
integer(UINT64) :: filesize
 
dwStatus = 0
filesize = 0
hFile = CreateFile(trim(name) // char(0), GENERIC_READ, FILE_SHARE_READ, NULL, &
OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, NULL)
 
if (hFile == INVALID_HANDLE_VALUE) then
dwStatus = GetLastError()
Line 401 ⟶ 1,487:
return
end if
if (CryptAcquireContext(hProv, NULL, NULL, PROV_RSA_FULL, &
CRYPT_VERIFYCONTEXT) == FALSE) then
dwStatus = GetLastError()
print *, "CryptAcquireContext failed."
goto 3
end if
if (CryptCreateHash(hProv, CALG_SHA1, 0_ULONG_PTR, 0_DWORD, hHash) == FALSE) then
 
dwStatus = GetLastError()
print *, "CryptCreateHash failed."
go to 2
end if
do
status = ReadFile(hFile, loc(buffer), BUFLEN, loc(nRead), NULL)
if (status == FALSE .or. nRead == 0) exit
filesize = filesize + nRead
Line 427 ⟶ 1,513:
end if
end do
if (status == FALSE) then
dwStatus = GetLastError()
Line 433 ⟶ 1,519:
go to 1
end if
nRead = SHA1LEN
if (CryptGetHashParam(hHash, HP_HASHVAL, hash, nRead, 0) == FALSE) then
Line 439 ⟶ 1,525:
print *, "CryptGetHashParam failed.", status, nRead, dwStatus
end if
1 status = CryptDestroyHash(hHash)
2 status = CryptReleaseContext(hProv, 0)
Line 445 ⟶ 1,531:
end subroutine
end module
 
program sha1
use sha1_msha1_mod
implicit none
integer :: n, m, i, j
Line 454 ⟶ 1,540:
integer(BYTE) :: hash(SHA1LEN)
integer(UINT64) :: filesize
n = command_argument_count()
do i = 1, n
Line 469 ⟶ 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 577 ⟶ 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''.
 
<syntaxhighlight lang="genie">print Checksum.compute_for_string(ChecksumType.SHA1, "Rosetta code", -1)</syntaxhighlight>
 
(The -1 is NUL byte terminated string indicator for length)
 
See [[SHA-256#Genie]].
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import (
Line 593 ⟶ 1,697:
h.Write([]byte("Rosetta Code"))
fmt.Printf("%x\n", h.Sum(nil))
}</langsyntaxhighlight>
{{out}}
<pre>
Line 600 ⟶ 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 608 ⟶ 1,734:
 
=={{header|Haskell}}==
<langsyntaxhighlight Haskelllang="haskell">module Digestor
where
import Data.Digest.Pure.SHA
Line 622 ⟶ 1,748:
putStr "Rosetta Code SHA1-codiert: "
putStrLn $ convertToSHA1 "Rosetta Code"
</syntaxhighlight>
</lang>
{{Out}}
<pre>
Rosetta Code SHA1-codiert: 48c98f7e5a6e736d790ab740dfc3f51a61abe2b5
</pre>
 
=={{header|Haxe}}==
<syntaxhighlight lang="haxe">import haxe.crypto.Sha1;
 
class Main {
static function main() {
var sha1 = Sha1.encode("Rosetta Code");
Sys.println(sha1);
}
}</syntaxhighlight>
 
{{out}}
<pre>48c98f7e5a6e736d790ab740dfc3f51a61abe2b5</pre>
 
=={{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 678 ⟶ 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 692 ⟶ 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}}==
<syntaxhighlight lang="javascript">/* SHA-1 hash in Jsish */
var str = 'Rosetta code';
puts(Util.hash(str, {type:'sha1'}));
 
/*
=!EXPECTSTART!=
b18c883f4da750164b5af362ea9b9f27f90904b4
=!EXPECTEND!=
*/</syntaxhighlight>
 
{{out}}
<pre>prompt$ jsish sha-1.jsi
b18c883f4da750164b5af362ea9b9f27f90904b4
prompt$ jsish -u sha-1.jsi
[PASS] sha-1.jsi</pre>
 
=={{header|Julia}}==
{{works with|Julia|0.6}}
 
<langsyntaxhighlight lang="julia">using Nettle
 
testdict = Dict("abc" => "a9993e364706816aba3e25717850c26c9cd0d89d",
Line 707 ⟶ 1,943:
if length(text) > 50 text = text[1:50] * "..." end
println("# $text\n -> digest: $digest\n -> expect: $expect")
end</langsyntaxhighlight>
 
{{out}}
Line 721 ⟶ 1,957:
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="scala">// version 1.0.6
 
import java.security.MessageDigest
Line 732 ⟶ 1,968:
for (byte in digest) print("%02x".format(byte))
println()
}</langsyntaxhighlight>
 
{{out}}
Line 740 ⟶ 1,976:
 
=={{header|Lasso}}==
<langsyntaxhighlight Lassolang="lasso">cipher_digest('Rosetta Code', -digest='SHA1',-hex=true)</langsyntaxhighlight>
 
{{out}}
Line 746 ⟶ 1,982:
 
=={{header|Liberty BASIC}}==
<syntaxhighlight lang="lb">
<lang lb>
'--------------------------------------------------------------------------------
' FAST SHA1 CALCULATION BASED ON MS ADVAPI32.DLL BY CRYPTOMAN '
Line 817 ⟶ 2,053:
next
end function
</syntaxhighlight>
</lang>
{{Out}}
<pre>48C98F7E5A6E736D790AB740DFC3F51A61ABE2B5</pre>
Line 823 ⟶ 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 843 ⟶ 2,079:
{{libheader|sha1}} ([https://github.com/kikito/sha1.lua luarocks install sha1])
 
<langsyntaxhighlight Lualang="lua">#!/usr/bin/lua
 
local sha1 = require "sha1"
Line 849 ⟶ 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 857 ⟶ 2,093:
</pre>
 
=={{header|MathematicaMaple}}==
<lang Mathematica>IntegerString[Hash["Rosetta Code", "SHA1"], 16]
 
<syntaxhighlight lang="maple">with(StringTools):
-> 48c98f7e5a6e736d790ab740dfc3f51a61abe2b5</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>
 
=={{header|min}}==
{{works with|min|0.19.6}}
<syntaxhighlight lang="min">"Rosetta Code" sha1 puts!</syntaxhighlight>
{{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 914 ⟶ 2,189:
return digestSum
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 924 ⟶ 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 966 ⟶ 2,234:
Out.String("SHA1: ");Utils.PrintHex(str,0,h.size);Out.Ln
END SHA1.
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 972 ⟶ 2,240:
48C98F7E 5A6E736D 790AB740 DFC3F51A 61ABE2B5
</pre>
 
=={{header|OCaml}}==
 
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 992 ⟶ 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 1,003 ⟶ 2,272:
=={{header|Pascal}}==
{{works with|Free_Pascal}} {{libheader|sha1}}
<langsyntaxhighlight lang="pascal">program RosettaSha1;
uses
sha1;
Line 1,011 ⟶ 2,280:
d:=SHA1String('Rosetta Code');
WriteLn(SHA1Print(d));
end.</langsyntaxhighlight>
 
{{out}}
Line 1,018 ⟶ 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|Perl 6Phix}}==
<!--<syntaxhighlight lang="phix">(phixonline)-->
 
<span style="color: #000080;font-style:italic;">--
A pure Perl 6 implementation that closely follows the description of SHA-1 in FIPS 180-1. Slow.
-- demo\rosetta\sha1.exw
 
-- =====================
<lang perl6>sub postfix:<mod2³²> { $^x % 2**32 }
--
sub infix:<⊕> { ($^x + $^y)mod2³² }
-- NB no longer considered secure. Non-optimised.
sub S { ($^x +< $^n)mod2³² +| ($x +> (32-$n)) }
--</span>
 
<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>
my \f = -> \B,\C,\D { (B +& C) +| ((+^B)mod2³² +& D) },
<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>
-> \B,\C,\D { B +^ C +^ D },
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
-> \B,\C,\D { (B +& C) +| (B +& D) +| (C +& D) },
-> \B,\C,\D { B +^ C +^ D };
<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>
 
<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>
my \K = 0x5A827999, 0x6ED9EBA1, 0x8F1BBCDC, 0xCA62C1D6;
<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>
 
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
sub sha1-pad(Blob $msg)
<span style="color: #008080;">return</span> <span style="color: #000000;">s</span>
{
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
my \bits = 8 * $msg.elems;
my @padded = flat $msg.list, 0x80, 0x00 xx (-($msg.elems + 1 + 8) % 64);
<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>
flat @padded.map({ :256[$^a,$^b,$^c,$^d] }), (bits +> 32)mod2³², (bits)mod2³²;
<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>
 
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
sub sha1-block(@H, @M is copy)
{
<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>
@M.push: S(1, [+^] @M[$_ «-« <3 8 14 16>] ) for 16 .. 79;
<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>
my ($A,$B,$C,$D,$E) = @H;
<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>
for 0..79 -> \t {
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
($A, $B, $C, $D, $E) =
<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>
S(5,$A) ⊕ f[t div 20]($B,$C,$D) ⊕ $E ⊕ @M[t] ⊕ K[t div 20],
<span style="color: #008080;">return</span> <span style="color: #000000;">result</span>
$A, S(30,$B), $C, $D;
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
}
@H »⊕=« ($A,$B,$C,$D,$E);
<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>
}
<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>
sub sha1(Blob $msg) returns Blob
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
{
my @M = sha1-pad($msg);
<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>
my @H = 0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0;
<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>
sha1-block(@H,@M[$_..$_+15]) for 0, 16...^ +@M;
<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>
Blob.new: flat map { reverse .polymod(256 xx 3) }, @H;
<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>
}
<span style="color: #000000;">h1</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0xefcdab89</span><span style="color: #0000FF;">,</span>
 
<span style="color: #000000;">h2</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0x98badcfe</span><span style="color: #0000FF;">,</span>
say sha1(.encode('ascii')), " $_"
<span style="color: #000000;">h3</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0x10325476</span><span style="color: #0000FF;">,</span>
for 'abc',
<span style="color: #000000;">h4</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0xc3d2e1f0</span>
'abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq',
'Rosetta Code',
<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>
'Ars longa, vita brevis';</lang>
<span style="color: #000000;">msg</span> <span style="color: #0000FF;">&=</span> <span style="color: #000000;">#80</span>
{{Out}}
<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>
<pre>Buf:0x<a9 99 3e 36 47 06 81 6a ba 3e 25 71 78 50 c2 6c 9c d0 d8 9d> abc
<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>
Buf:0x<84 98 3e 44 1c 3b d2 6e ba ae 4a a1 f9 51 29 e5 e5 46 70 f1> abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq
Buf:0x<48 c9 8f 7e 5a 6e 73 6d 79 0a b7 40 df c3 f5 1a 61 ab e2 b5> Rosetta Code
<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>
Buf:0x<e6 40 d2 85 24 28 86 eb 96 ab 80 cb f8 58 38 9b 3d f5 2f 43> Ars longa, vita brevis</pre>
<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>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<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>
<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>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</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> <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>
<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>
<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>
<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>
<span style="color: #000000;">k</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">#5A827999</span>
<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>
<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>
<span style="color: #000000;">k</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">#6ED9EBA1</span>
<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>
<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>
<span style="color: #000000;">k</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">#8F1BBCDC</span>
<span style="color: #008080;">else</span> <span style="color: #000080;font-style:italic;">-- i&lt;=80</span>
<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>
<span style="color: #000000;">k</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">#CA62C1D6</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</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> <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>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</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: #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>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<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>
<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>
<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>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<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>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<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>
<!--</syntaxhighlight>-->
{{out}}
<pre>
"48c98f7e 5a6e736d 790ab740 dfc3f51a 61abe2b5"
</pre>
 
=={{header|PHP}}==
<langsyntaxhighlight lang="php"><?php
$string = 'Rosetta Code';
echo sha1( $string ), "\n";
?></langsyntaxhighlight>
{{out}}
<pre>48c98f7e5a6e736d790ab740dfc3f51a61abe2b5</pre>
Line 1,095 ⟶ 2,402:
=={{header|PicoLisp}}==
Library and implementation.
<langsyntaxhighlight PicoLisplang="picolisp">(de leftRotate (X C)
(| (mod32 (>> (- C) X)) (>> (- 32 C) X)) )
 
Line 1,210 ⟶ 2,517:
'(NIL (20)) ) ) ) ) )
(bye)</langsyntaxhighlight>
 
=={{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">
<lang PowerShell>
Function Calculate-SHA1( $String ){
$Enc = [system.Text.Encoding]::UTF8
Line 1,225 ⟶ 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 1,236 ⟶ 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}}==
<syntaxhighlight lang="rsplus">
library(digest)
 
input <- "Rosetta Code"
cat(digest(input, algo = "sha1", serialize = FALSE), "\n")
</syntaxhighlight>
{{out}}
<pre>
48c98f7e5a6e736d790ab740dfc3f51a61abe2b5
</pre>
 
=={{header|Racket}}==
Line 1,251 ⟶ 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}}==
(formerly Perl 6)
===Pure Raku===
A pure Raku implementation that closely follows the description of SHA-1 in FIPS 180-1. Slow.
 
<syntaxhighlight lang="raku" line>sub postfix:<mod2³²> { $^x % 2**32 }
sub infix:<⊕> { ($^x + $^y)mod2³² }
sub S { ($^x +< $^n)mod2³² +| ($x +> (32-$n)) }
 
my \f = -> \B,\C,\D { (B +& C) +| ((+^B)mod2³² +& D) },
-> \B,\C,\D { B +^ C +^ D },
-> \B,\C,\D { (B +& C) +| (B +& D) +| (C +& D) },
-> \B,\C,\D { B +^ C +^ D };
 
my \K = 0x5A827999, 0x6ED9EBA1, 0x8F1BBCDC, 0xCA62C1D6;
 
sub sha1-pad(Blob $msg)
{
my \bits = 8 * $msg.elems;
my @padded = flat $msg.list, 0x80, 0x00 xx (-($msg.elems + 1 + 8) % 64);
flat @padded.map({ :256[$^a,$^b,$^c,$^d] }), (bits +> 32)mod2³², (bits)mod2³²;
}
 
sub sha1-block(@H, @M is copy)
{
@M.push: S(1, [+^] @M[$_ «-« <3 8 14 16>] ) for 16 .. 79;
 
my ($A,$B,$C,$D,$E) = @H;
for 0..79 -> \t {
($A, $B, $C, $D, $E) =
S(5,$A) ⊕ f[t div 20]($B,$C,$D) ⊕ $E ⊕ @M[t] ⊕ K[t div 20],
$A, S(30,$B), $C, $D;
}
@H »⊕=« ($A,$B,$C,$D,$E);
}
 
sub sha1(Blob $msg) returns Blob
{
my @M = sha1-pad($msg);
my @H = 0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0;
sha1-block(@H,@M[$_..$_+15]) for 0, 16...^ +@M;
Blob.new: flat map { reverse .polymod(256 xx 3) }, @H;
}
 
say sha1(.encode('ascii')), " $_"
for 'abc',
'abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq',
'Rosetta Code',
'Ars longa, vita brevis';</syntaxhighlight>
{{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
Buf:0x<84 98 3e 44 1c 3b d2 6e ba ae 4a a1 f9 51 29 e5 e5 46 70 f1> abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq
Buf:0x<48 c9 8f 7e 5a 6e 73 6d 79 0a b7 40 df c3 f5 1a 61 ab e2 b5> Rosetta Code
Buf:0x<e6 40 d2 85 24 28 86 eb 96 ab 80 cb f8 58 38 9b 3d f5 2f 43> Ars longa, vita brevis</pre>
 
===Library based implementation===
Quite speedy.
<syntaxhighlight lang="raku" line>use Digest::SHA1::Native;
 
# use sha1-hex() if you want a hex string
 
say sha1($_), " $_" for
'abc',
'abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq',
'Rosetta Code',
'Ars longa, vita brevis'
;</syntaxhighlight>
{{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
Blob:0x<84 98 3E 44 1C 3B D2 6E BA AE 4A A1 F9 51 29 E5 E5 46 70 F1> abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq
Blob:0x<48 C9 8F 7E 5A 6E 73 6D 79 0A B7 40 DF C3 F5 1A 61 AB E2 B5> Rosetta Code
Blob:0x<E6 40 D2 85 24 28 86 EB 96 AB 80 CB F8 58 38 9B 3D F5 2F 43> Ars longa, vita brevis</pre>
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
# Project : SHA-1
 
Line 1,274 ⟶ 2,686:
see "SHA-1: "
see sha1(str) + nl
</syntaxhighlight>
</lang>
Output:
<pre>
Line 1,285 ⟶ 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 1,359 ⟶ 2,771:
'Rosetta Code',
].each {|s| printf("%s:\n %s\n", s, *sha1(s).unpack('H*'))}
end</langsyntaxhighlight>
{{out}}
<pre>
Line 1,370 ⟶ 2,782:
</pre>
 
=={{header|Rust}}==
Using sha1 crate: https://docs.rs/sha1/0.6.0/sha1/
<syntaxhighlight lang="rust">use sha1::Sha1;
 
fn main() {
let mut hash_msg = Sha1::new();
hash_msg.update(b"Rosetta Code");
println!("{}", hash_msg.digest().to_string());
}
</syntaxhighlight>
Output
<pre>
48c98f7e5a6e736d790ab740dfc3f51a61abe2b5
</pre>
 
=={{header|S-lang}}==
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 1,384 ⟶ 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 1,474 ⟶ 2,900:
println(hash("Rosetta Code"))
}
</syntaxhighlight>
</lang>
 
{{out}}
<pre>48c98f7e5a6e736d790ab740dfc3f51a61abe2b5</pre>
 
 
=={{header|Scheme}}==
{{works with|Ol}}
 
<langsyntaxhighlight lang="scheme">
; band - binary AND operation
; bor - binary OR operation
Line 1,644 ⟶ 3,069:
(->32 (+ D d))
(->32 (+ E e)))))))))
</syntaxhighlight>
</lang>
 
{{out}}
<langsyntaxhighlight lang="scheme">
(define (->string value)
(runes->string
Line 1,663 ⟶ 3,088:
(print (->string (sha1:digest "")))
> da39a3ee5e6b4b0d3255bfef95601890afd80709
</syntaxhighlight>
</lang>
 
=={{header|Seed7}}==
<langsyntaxhighlight lang="seed7">$ include "seed7_05.s7i";
include "msgdigest.s7i";
 
Line 1,672 ⟶ 3,097:
begin
writeln(hex(sha1("Rosetta Code")));
end func;</langsyntaxhighlight>
 
{{out}}
Line 1,680 ⟶ 3,105:
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">var sha = frequire('Digest::SHA');
say sha.sha1_hex('Rosetta Code');</langsyntaxhighlight>
{{out}}
<pre>48c98f7e5a6e736d790ab740dfc3f51a61abe2b5</pre>
Line 1,687 ⟶ 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 1,702 ⟶ 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 1,725 ⟶ 3,204:
 
zkl: MsgHash.SHA1("a"*1000,1000); // hash 1000 a's 1000 times
34aa973cd4c4daa4f61eeb2bdbad27316534016f</langsyntaxhighlight>
 
 
9,476

edits