WeixiangYan/CodeTransOcean huggingface dataset:
Task: Undefined values

C: #include <stdio.h>
#include <stdlib.h>

int main()
{
  int junk, *junkp;

  
  printf("junk: %d\n", junk);

  
  junkp = malloc(sizeof *junkp);
  if (junkp)
    printf("*junkp: %d\n", *junkp);
  return 0;
}
Task: Undefined values


C++: #include <iostream>

int main()
{
    int undefined;
    if (undefined == 42) 
    {
        std::cout << "42";
    }
    
    if (undefined != 42)
    {
        std::cout << "not 42";
    }
}
Task: Undefined values







Python: 
try: name
except NameError: print "name is undefined at first check"


name = "Chocolate"


try: name
except NameError: print "name is undefined at second check"


del name


try: name
except NameError: print "name is undefined at third check"


name = 42


try: name
except NameError: print "name is undefined at fourth check"



print "Done"
Task: Undefined values




Go: package main

import "fmt"

var (
    s []int
    p *int
    f func()
    i interface{}
    m map[int]int
    c chan int
)

func main() {
    fmt.Println("Exercise nil objects:")
    status()

    
    s = make([]int, 1)
    p = &s[0] 
    f = func() { fmt.Println("function call") }
    i = user(0) 
    m = make(map[int]int)
    c = make(chan int, 1)

    fmt.Println("\nExercise objects after initialization:")
    status()
}

type user int

func (user) m() {
    fmt.Println("method call")
}

func status() {
    trySlice()
    tryPointer()
    tryFunction()
    tryInterface()
    tryMap()
    tryChannel()
}

func reportPanic() {
    if x := recover(); x != nil {
        fmt.Println("panic:", x)
    }
}

func trySlice() {
    defer reportPanic()
    fmt.Println("s[0] =", s[0])
}

func tryPointer() {
    defer reportPanic()
    fmt.Println("*p =", *p)
}

func tryFunction() {
    defer reportPanic()
    f()
}

func tryInterface() {
    defer reportPanic()

    
    
    
    switch i.(type) {
    case nil:
        fmt.Println("i is nil interface")
    case interface {
        m()
    }:
        fmt.Println("i has method m")
    }

    
    i.(interface {
        m()
    }).m()
}

func tryMap() {
    defer reportPanic()
    m[0] = 0
    fmt.Println("m[0] =", m[0])
}

func tryChannel() {
    defer reportPanic()
    close(c)
    fmt.Println("channel closed")
}
Task: Undefined values



C#: string foo = null;
Task: Undefined values





Java: String string = null;        
System.out.println(string);           
System.out.println(string.length());
Task: Undefined values


C++: #include <iostream>

int main()
{
    int undefined;
    if (undefined == 42) 
    {
        std::cout << "42";
    }
    
    if (undefined != 42)
    {
        std::cout << "not 42";
    }
}









Fortran: IsNaN(x)
Task: Undefined values

C: #include <stdio.h>
#include <stdlib.h>

int main()
{
  int junk, *junkp;

  
  printf("junk: %d\n", junk);

  
  junkp = malloc(sizeof *junkp);
  if (junkp)
    printf("*junkp: %d\n", *junkp);
  return 0;
}










Fortran: IsNaN(x)
Task: Undefined values




Go: package main

import "fmt"

var (
    s []int
    p *int
    f func()
    i interface{}
    m map[int]int
    c chan int
)

func main() {
    fmt.Println("Exercise nil objects:")
    status()

    
    s = make([]int, 1)
    p = &s[0] 
    f = func() { fmt.Println("function call") }
    i = user(0) 
    m = make(map[int]int)
    c = make(chan int, 1)

    fmt.Println("\nExercise objects after initialization:")
    status()
}

type user int

func (user) m() {
    fmt.Println("method call")
}

func status() {
    trySlice()
    tryPointer()
    tryFunction()
    tryInterface()
    tryMap()
    tryChannel()
}

func reportPanic() {
    if x := recover(); x != nil {
        fmt.Println("panic:", x)
    }
}

func trySlice() {
    defer reportPanic()
    fmt.Println("s[0] =", s[0])
}

func tryPointer() {
    defer reportPanic()
    fmt.Println("*p =", *p)
}

func tryFunction() {
    defer reportPanic()
    f()
}

func tryInterface() {
    defer reportPanic()

    
    
    
    switch i.(type) {
    case nil:
        fmt.Println("i is nil interface")
    case interface {
        m()
    }:
        fmt.Println("i has method m")
    }

    
    i.(interface {
        m()
    }).m()
}

func tryMap() {
    defer reportPanic()
    m[0] = 0
    fmt.Println("m[0] =", m[0])
}

func tryChannel() {
    defer reportPanic()
    close(c)
    fmt.Println("channel closed")
}







Fortran: IsNaN(x)
Task: Undefined values







Python: 
try: name
except NameError: print "name is undefined at first check"


name = "Chocolate"


try: name
except NameError: print "name is undefined at second check"


del name


try: name
except NameError: print "name is undefined at third check"


name = 42


try: name
except NameError: print "name is undefined at fourth check"



print "Done"




Fortran: IsNaN(x)
Task: Undefined values






PHP: <?php

if (!isset($var))
    echo "var is undefined at first check\n";

$var = "Chocolate";


if (!isset($var))
    echo "var is undefined at second check\n";

unset($var);


if (!isset($var))
    echo "var is undefined at third check\n";

$var = 42;


if (!isset($var))
    echo "var is undefined at fourth check\n";


echo "Done\n";
?>





Fortran: IsNaN(x)
Task: Undefined values






PHP: <?php

if (!isset($var))
    echo "var is undefined at first check\n";

$var = "Chocolate";


if (!isset($var))
    echo "var is undefined at second check\n";

unset($var);


if (!isset($var))
    echo "var is undefined at third check\n";

$var = 42;


if (!isset($var))
    echo "var is undefined at fourth check\n";


echo "Done\n";
?>
Task: Undefined values






PHP: <?php

if (!isset($var))
    echo "var is undefined at first check\n";

$var = "Chocolate";


if (!isset($var))
    echo "var is undefined at second check\n";

unset($var);


if (!isset($var))
    echo "var is undefined at third check\n";

$var = 42;


if (!isset($var))
    echo "var is undefined at fourth check\n";


echo "Done\n";
?>






Ada: pragma Initialize_Scalars; 
with Ada.Text_IO;  use Ada.Text_IO;

procedure Invalid_Value is
   type Color is (Red, Green, Blue);
   X : Float;
   Y : Color;
begin
   if not X'Valid then
      Put_Line ("X is not valid");
   end if;
   X := 1.0;
   if X'Valid then
      Put_Line ("X is" & Float'Image (X));
   end if;   
   if not Y'Valid then
      Put_Line ("Y is not valid");
   end if;
   Y := Green;
   if Y'Valid then
      Put_Line ("Y is " & Color'Image (Y));
   end if;
end Invalid_Value;
Task: Balanced ternary







Python: class BalancedTernary:
    

    str2dig = {'+': 1, '-': -1, '0': 0} 
    dig2str = {1: '+', -1: '-', 0: '0'} 
    table = ((0, -1), (1, -1), (-1, 0), (0, 0), (1, 0), (-1, 1), (0, 1)) 

    def __init__(self, inp):
        if isinstance(inp, str):
            self.digits = [BalancedTernary.str2dig[c] for c in reversed(inp)]
        elif isinstance(inp, int):
            self.digits = self._int2ternary(inp)
        elif isinstance(inp, BalancedTernary):
            self.digits = list(inp.digits)
        elif isinstance(inp, list):
            if all(d in (0, 1, -1) for d in inp):
                self.digits = list(inp)
            else:
                raise ValueError("BalancedTernary: Wrong input digits.")
        else:
            raise TypeError("BalancedTernary: Wrong constructor input.")

    @staticmethod
    def _int2ternary(n):
        if n == 0: return []
        if (n % 3) == 0: return [0] + BalancedTernary._int2ternary(n // 3)
        if (n % 3) == 1: return [1] + BalancedTernary._int2ternary(n // 3)
        if (n % 3) == 2: return [-1] + BalancedTernary._int2ternary((n + 1) // 3)

    def to_int(self):
        return reduce(lambda y,x: x + 3 * y, reversed(self.digits), 0)

    def __repr__(self):
        if not self.digits: return "0"
        return "".join(BalancedTernary.dig2str[d] for d in reversed(self.digits))

    @staticmethod
    def _neg(digs):
        return [-d for d in digs]

    def __neg__(self):
        return BalancedTernary(BalancedTernary._neg(self.digits))

    @staticmethod
    def _add(a, b, c=0):
        if not (a and b):
            if c == 0:
                return a or b
            else:
                return BalancedTernary._add([c], a or b)
        else:
            (d, c) = BalancedTernary.table[3 + (a[0] if a else 0) + (b[0] if b else 0) + c]
            res = BalancedTernary._add(a[1:], b[1:], c)
            
            if res or d != 0:
                return [d] + res
            else:
                return res

    def __add__(self, b):
        return BalancedTernary(BalancedTernary._add(self.digits, b.digits))

    def __sub__(self, b):
        return self + (-b)

    @staticmethod
    def _mul(a, b):
        if not (a and b):
            return []
        else:
            if   a[0] == -1: x = BalancedTernary._neg(b)
            elif a[0] ==  0: x = []
            elif a[0] ==  1: x = b
            else: assert False
            y = [0] + BalancedTernary._mul(a[1:], b)
            return BalancedTernary._add(x, y)

    def __mul__(self, b):
        return BalancedTernary(BalancedTernary._mul(self.digits, b.digits))


def main():
    a = BalancedTernary("+-0++0+")
    print "a:", a.to_int(), a

    b = BalancedTernary(-436)
    print "b:", b.to_int(), b

    c = BalancedTernary("+-++-")
    print "c:", c.to_int(), c

    r = a * (b - c)
    print "a * (b - c):", r.to_int(), r

main()
Task: SHA-1



C#: 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));
        }
    }
}









Ada: with Ada.Text_IO;
with GNAT.SHA1;

procedure Main is
begin
   Ada.Text_IO.Put_Line ("SHA1 (""Rosetta Code"") = " &
                         GNAT.SHA1.Digest ("Rosetta Code"));
end Main;
Task: SHA-1

C: #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/sha.h>

int main()
{
  int i;
  unsigned char result[SHA_DIGEST_LENGTH];
  const char *string = "Rosetta Code";
 
  SHA1(string, strlen(string), result);
 
  for(i = 0; i < SHA_DIGEST_LENGTH; i++)
    printf("%02x%c", result[i], i < (SHA_DIGEST_LENGTH-1) ? ' ' : '\n');
 
  return EXIT_SUCCESS;
}











Ada: with Ada.Text_IO;
with GNAT.SHA1;

procedure Main is
begin
   Ada.Text_IO.Put_Line ("SHA1 (""Rosetta Code"") = " &
                         GNAT.SHA1.Digest ("Rosetta Code"));
end Main;
Task: SHA-1


C++: #include <string>
#include <iostream>
#include "Poco/SHA1Engine.h"
#include "Poco/DigestStream.h"

using Poco::DigestEngine ;
using Poco::SHA1Engine ;
using Poco::DigestOutputStream ;

int main( ) {
   std::string myphrase ( "Rosetta Code" ) ;
   SHA1Engine sha1 ;
   DigestOutputStream outstr( sha1 ) ;
   outstr << myphrase ;
   outstr.flush( ) ; 
   const DigestEngine::Digest& digest = sha1.digest( ) ;
   std::cout << myphrase << " as a sha1 digest :" << DigestEngine::digestToHex( digest ) 
      << " !" << std::endl ;
   return 0 ;
}










Ada: with Ada.Text_IO;
with GNAT.SHA1;

procedure Main is
begin
   Ada.Text_IO.Put_Line ("SHA1 (""Rosetta Code"") = " &
                         GNAT.SHA1.Digest ("Rosetta Code"));
end Main;
Task: SHA-1




Go: package main

import (
    "crypto/sha1"
    "fmt"
)

func main() {
    h := sha1.New()
    h.Write([]byte("Rosetta Code"))
    fmt.Printf("%x\n", h.Sum(nil))
}








Ada: with Ada.Text_IO;
with GNAT.SHA1;

procedure Main is
begin
   Ada.Text_IO.Put_Line ("SHA1 (""Rosetta Code"") = " &
                         GNAT.SHA1.Digest ("Rosetta Code"));
end Main;
Task: SHA-1







Python: import crypto { sha1 }
let hash = sha1.hexdigest('Ars longa, vita brevis')
print hash





Ada: with Ada.Text_IO;
with GNAT.SHA1;

procedure Main is
begin
   Ada.Text_IO.Put_Line ("SHA1 (""Rosetta Code"") = " &
                         GNAT.SHA1.Digest ("Rosetta Code"));
end Main;
Task: SHA-1

C: #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/sha.h>

int main()
{
  int i;
  unsigned char result[SHA_DIGEST_LENGTH];
  const char *string = "Rosetta Code";
 
  SHA1(string, strlen(string), result);
 
  for(i = 0; i < SHA_DIGEST_LENGTH; i++)
    printf("%02x%c", result[i], i < (SHA_DIGEST_LENGTH-1) ? ' ' : '\n');
 
  return EXIT_SUCCESS;
}
Task: SHA-1



C#: 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));
        }
    }
}
Task: SHA-1


C++: #include <string>
#include <iostream>
#include "Poco/SHA1Engine.h"
#include "Poco/DigestStream.h"

using Poco::DigestEngine ;
using Poco::SHA1Engine ;
using Poco::DigestOutputStream ;

int main( ) {
   std::string myphrase ( "Rosetta Code" ) ;
   SHA1Engine sha1 ;
   DigestOutputStream outstr( sha1 ) ;
   outstr << myphrase ;
   outstr.flush( ) ; 
   const DigestEngine::Digest& digest = sha1.digest( ) ;
   std::cout << myphrase << " as a sha1 digest :" << DigestEngine::digestToHex( digest ) 
      << " !" << std::endl ;
   return 0 ;
}
Task: SHA-1







Python: import crypto { sha1 }
let hash = sha1.hexdigest('Ars longa, vita brevis')
print hash
Task: SHA-1




Go: package main

import (
    "crypto/sha1"
    "fmt"
)

func main() {
    h := sha1.New()
    h.Write([]byte("Rosetta Code"))
    fmt.Printf("%x\n", h.Sum(nil))
}
Task: SHA-1




Go: package main

import (
    "crypto/sha1"
    "fmt"
)

func main() {
    h := sha1.New()
    h.Write([]byte("Rosetta Code"))
    fmt.Printf("%x\n", h.Sum(nil))
}







Fortran: module sha1_mod
    use kernel32
    use advapi32
    implicit none
    integer, parameter :: SHA1LEN = 20
contains
    subroutine sha1hash(name, hash, dwStatus, filesize)
        implicit none
        character(*) :: name
        integer, parameter :: BUFLEN = 32768
        integer(HANDLE) :: hFile, hProv, hHash
        integer(DWORD) :: dwStatus, nRead
        integer(BOOL) :: status
        integer(BYTE) :: buffer(BUFLEN)
        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()
            print *, "CreateFile failed."
            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, nRead, NULL)
            if (status == FALSE .or. nRead == 0) exit
            filesize = filesize + nRead
            if (CryptHashData(hHash, buffer, nRead, 0) == FALSE) then
                dwStatus = GetLastError()
                print *, "CryptHashData failed."
                go to 1
            end if
        end do
 
        if (status == FALSE) then
            dwStatus = GetLastError()
            print *, "ReadFile failed."
            go to 1
        end if
 
        nRead = SHA1LEN
        if (CryptGetHashParam(hHash, HP_HASHVAL, hash, nRead, 0) == FALSE) then
            dwStatus = GetLastError()
            print *, "CryptGetHashParam failed.", status, nRead, dwStatus
        end if
 
      1 status = CryptDestroyHash(hHash)
      2 status = CryptReleaseContext(hProv, 0)
      3 status = CloseHandle(hFile)
    end subroutine
end module
 
program sha1
    use sha1_mod
    implicit none
    integer :: n, m, i, j
    character(:), allocatable :: name
    integer(DWORD) :: dwStatus
    integer(BYTE) :: hash(SHA1LEN)
    integer(UINT64) :: filesize
 
    n = command_argument_count()
    do i = 1, n
        call get_command_argument(i, length=m)
        allocate(character(m) :: name)
        call get_command_argument(i, name)
        call sha1hash(name, hash, dwStatus, filesize)
        if (dwStatus == 0) then
            do j = 1, SHA1LEN
                write(*, "(Z2.2)", advance="NO") hash(j)
            end do
            write(*, "(' ',A,' (',G0,' bytes)')") name, filesize
        end if
        deallocate(name)
    end do
end program
Task: SHA-1







Python: import crypto { sha1 }
let hash = sha1.hexdigest('Ars longa, vita brevis')
print hash




Fortran: module sha1_mod
    use kernel32
    use advapi32
    implicit none
    integer, parameter :: SHA1LEN = 20
contains
    subroutine sha1hash(name, hash, dwStatus, filesize)
        implicit none
        character(*) :: name
        integer, parameter :: BUFLEN = 32768
        integer(HANDLE) :: hFile, hProv, hHash
        integer(DWORD) :: dwStatus, nRead
        integer(BOOL) :: status
        integer(BYTE) :: buffer(BUFLEN)
        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()
            print *, "CreateFile failed."
            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, nRead, NULL)
            if (status == FALSE .or. nRead == 0) exit
            filesize = filesize + nRead
            if (CryptHashData(hHash, buffer, nRead, 0) == FALSE) then
                dwStatus = GetLastError()
                print *, "CryptHashData failed."
                go to 1
            end if
        end do
 
        if (status == FALSE) then
            dwStatus = GetLastError()
            print *, "ReadFile failed."
            go to 1
        end if
 
        nRead = SHA1LEN
        if (CryptGetHashParam(hHash, HP_HASHVAL, hash, nRead, 0) == FALSE) then
            dwStatus = GetLastError()
            print *, "CryptGetHashParam failed.", status, nRead, dwStatus
        end if
 
      1 status = CryptDestroyHash(hHash)
      2 status = CryptReleaseContext(hProv, 0)
      3 status = CloseHandle(hFile)
    end subroutine
end module
 
program sha1
    use sha1_mod
    implicit none
    integer :: n, m, i, j
    character(:), allocatable :: name
    integer(DWORD) :: dwStatus
    integer(BYTE) :: hash(SHA1LEN)
    integer(UINT64) :: filesize
 
    n = command_argument_count()
    do i = 1, n
        call get_command_argument(i, length=m)
        allocate(character(m) :: name)
        call get_command_argument(i, name)
        call sha1hash(name, hash, dwStatus, filesize)
        if (dwStatus == 0) then
            do j = 1, SHA1LEN
                write(*, "(Z2.2)", advance="NO") hash(j)
            end do
            write(*, "(' ',A,' (',G0,' bytes)')") name, filesize
        end if
        deallocate(name)
    end do
end program
Task: SHA-1






PHP: <?php
$string = 'Rosetta Code';
echo sha1( $string ), "\n";
?>





Fortran: module sha1_mod
    use kernel32
    use advapi32
    implicit none
    integer, parameter :: SHA1LEN = 20
contains
    subroutine sha1hash(name, hash, dwStatus, filesize)
        implicit none
        character(*) :: name
        integer, parameter :: BUFLEN = 32768
        integer(HANDLE) :: hFile, hProv, hHash
        integer(DWORD) :: dwStatus, nRead
        integer(BOOL) :: status
        integer(BYTE) :: buffer(BUFLEN)
        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()
            print *, "CreateFile failed."
            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, nRead, NULL)
            if (status == FALSE .or. nRead == 0) exit
            filesize = filesize + nRead
            if (CryptHashData(hHash, buffer, nRead, 0) == FALSE) then
                dwStatus = GetLastError()
                print *, "CryptHashData failed."
                go to 1
            end if
        end do
 
        if (status == FALSE) then
            dwStatus = GetLastError()
            print *, "ReadFile failed."
            go to 1
        end if
 
        nRead = SHA1LEN
        if (CryptGetHashParam(hHash, HP_HASHVAL, hash, nRead, 0) == FALSE) then
            dwStatus = GetLastError()
            print *, "CryptGetHashParam failed.", status, nRead, dwStatus
        end if
 
      1 status = CryptDestroyHash(hHash)
      2 status = CryptReleaseContext(hProv, 0)
      3 status = CloseHandle(hFile)
    end subroutine
end module
 
program sha1
    use sha1_mod
    implicit none
    integer :: n, m, i, j
    character(:), allocatable :: name
    integer(DWORD) :: dwStatus
    integer(BYTE) :: hash(SHA1LEN)
    integer(UINT64) :: filesize
 
    n = command_argument_count()
    do i = 1, n
        call get_command_argument(i, length=m)
        allocate(character(m) :: name)
        call get_command_argument(i, name)
        call sha1hash(name, hash, dwStatus, filesize)
        if (dwStatus == 0) then
            do j = 1, SHA1LEN
                write(*, "(Z2.2)", advance="NO") hash(j)
            end do
            write(*, "(' ',A,' (',G0,' bytes)')") name, filesize
        end if
        deallocate(name)
    end do
end program
Task: SHA-1






PHP: <?php
$string = 'Rosetta Code';
echo sha1( $string ), "\n";
?>
Task: SHA-1






PHP: <?php
$string = 'Rosetta Code';
echo sha1( $string ), "\n";
?>






Ada: with Ada.Text_IO;
with GNAT.SHA1;

procedure Main is
begin
   Ada.Text_IO.Put_Line ("SHA1 (""Rosetta Code"") = " &
                         GNAT.SHA1.Digest ("Rosetta Code"));
end Main;