Instruction
stringlengths
45
106
input_code
stringlengths
1
13.7k
output_code
stringlengths
1
13.7k
Please provide an equivalent version of this BBC_Basic code in C++.
PRINT FNsha1("Rosetta Code") END DEF FNsha1(message$) LOCAL buflen%, buffer%, hprov%, hhash%, hash$, i% CALG_SHA1 = &8004 CRYPT_VERIFYCONTEXT = &F0000000 HP_HASHVAL = 2 PROV_RSA_FULL = 1 buflen% = 64 DIM buffer% LOCAL buflen%-1 SYS "CryptAcquireContext", ^hprov%, 0, 0, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT SYS "CryptCreateHash", hprov%, CALG_SHA1, 0, 0, ^hhash% SYS "CryptHashData", hhash%, message$, LEN(message$), 0 SYS "CryptGetHashParam", hhash%, HP_HASHVAL, buffer%, ^buflen%, 0 SYS "CryptDestroyHash", hhash% SYS "CryptReleaseContext", hprov% FOR i% = 0 TO buflen%-1 hash$ += RIGHT$("0" + STR$~buffer%?i%, 2) NEXT = hash$
#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 ; }
Write the same algorithm in Python as shown in this BBC_Basic implementation.
PRINT FNsha1("Rosetta Code") END DEF FNsha1(message$) LOCAL buflen%, buffer%, hprov%, hhash%, hash$, i% CALG_SHA1 = &8004 CRYPT_VERIFYCONTEXT = &F0000000 HP_HASHVAL = 2 PROV_RSA_FULL = 1 buflen% = 64 DIM buffer% LOCAL buflen%-1 SYS "CryptAcquireContext", ^hprov%, 0, 0, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT SYS "CryptCreateHash", hprov%, CALG_SHA1, 0, 0, ^hhash% SYS "CryptHashData", hhash%, message$, LEN(message$), 0 SYS "CryptGetHashParam", hhash%, HP_HASHVAL, buffer%, ^buflen%, 0 SYS "CryptDestroyHash", hhash% SYS "CryptReleaseContext", hprov% FOR i% = 0 TO buflen%-1 hash$ += RIGHT$("0" + STR$~buffer%?i%, 2) NEXT = hash$
import crypto { sha1 } let hash = sha1.hexdigest('Ars longa, vita brevis') print hash
Can you help me rewrite this code in Python instead of BBC_Basic, keeping it the same logically?
PRINT FNsha1("Rosetta Code") END DEF FNsha1(message$) LOCAL buflen%, buffer%, hprov%, hhash%, hash$, i% CALG_SHA1 = &8004 CRYPT_VERIFYCONTEXT = &F0000000 HP_HASHVAL = 2 PROV_RSA_FULL = 1 buflen% = 64 DIM buffer% LOCAL buflen%-1 SYS "CryptAcquireContext", ^hprov%, 0, 0, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT SYS "CryptCreateHash", hprov%, CALG_SHA1, 0, 0, ^hhash% SYS "CryptHashData", hhash%, message$, LEN(message$), 0 SYS "CryptGetHashParam", hhash%, HP_HASHVAL, buffer%, ^buflen%, 0 SYS "CryptDestroyHash", hhash% SYS "CryptReleaseContext", hprov% FOR i% = 0 TO buflen%-1 hash$ += RIGHT$("0" + STR$~buffer%?i%, 2) NEXT = hash$
import crypto { sha1 } let hash = sha1.hexdigest('Ars longa, vita brevis') print hash
Can you help me rewrite this code in Go instead of BBC_Basic, keeping it the same logically?
PRINT FNsha1("Rosetta Code") END DEF FNsha1(message$) LOCAL buflen%, buffer%, hprov%, hhash%, hash$, i% CALG_SHA1 = &8004 CRYPT_VERIFYCONTEXT = &F0000000 HP_HASHVAL = 2 PROV_RSA_FULL = 1 buflen% = 64 DIM buffer% LOCAL buflen%-1 SYS "CryptAcquireContext", ^hprov%, 0, 0, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT SYS "CryptCreateHash", hprov%, CALG_SHA1, 0, 0, ^hhash% SYS "CryptHashData", hhash%, message$, LEN(message$), 0 SYS "CryptGetHashParam", hhash%, HP_HASHVAL, buffer%, ^buflen%, 0 SYS "CryptDestroyHash", hhash% SYS "CryptReleaseContext", hprov% FOR i% = 0 TO buflen%-1 hash$ += RIGHT$("0" + STR$~buffer%?i%, 2) NEXT = hash$
package main import ( "crypto/sha1" "fmt" ) func main() { h := sha1.New() h.Write([]byte("Rosetta Code")) fmt.Printf("%x\n", h.Sum(nil)) }
Port the provided BBC_Basic code into Go while preserving the original functionality.
PRINT FNsha1("Rosetta Code") END DEF FNsha1(message$) LOCAL buflen%, buffer%, hprov%, hhash%, hash$, i% CALG_SHA1 = &8004 CRYPT_VERIFYCONTEXT = &F0000000 HP_HASHVAL = 2 PROV_RSA_FULL = 1 buflen% = 64 DIM buffer% LOCAL buflen%-1 SYS "CryptAcquireContext", ^hprov%, 0, 0, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT SYS "CryptCreateHash", hprov%, CALG_SHA1, 0, 0, ^hhash% SYS "CryptHashData", hhash%, message$, LEN(message$), 0 SYS "CryptGetHashParam", hhash%, HP_HASHVAL, buffer%, ^buflen%, 0 SYS "CryptDestroyHash", hhash% SYS "CryptReleaseContext", hprov% FOR i% = 0 TO buflen%-1 hash$ += RIGHT$("0" + STR$~buffer%?i%, 2) NEXT = hash$
package main import ( "crypto/sha1" "fmt" ) func main() { h := sha1.New() h.Write([]byte("Rosetta Code")) fmt.Printf("%x\n", h.Sum(nil)) }
Maintain the same structure and functionality when rewriting this code in C.
(defun sha1-hash (data) (let ((sha1 (ironclad:make-digest 'ironclad:sha1)) (bin-data (ironclad:ascii-string-to-byte-array data))) (ironclad:update-digest sha1 bin-data) (ironclad:byte-array-to-hex-string (ironclad:produce-digest sha1))))
#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; }
Change the following Common_Lisp code into C without altering its purpose.
(defun sha1-hash (data) (let ((sha1 (ironclad:make-digest 'ironclad:sha1)) (bin-data (ironclad:ascii-string-to-byte-array data))) (ironclad:update-digest sha1 bin-data) (ironclad:byte-array-to-hex-string (ironclad:produce-digest sha1))))
#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; }
Write a version of this Common_Lisp function in C# with identical behavior.
(defun sha1-hash (data) (let ((sha1 (ironclad:make-digest 'ironclad:sha1)) (bin-data (ironclad:ascii-string-to-byte-array data))) (ironclad:update-digest sha1 bin-data) (ironclad:byte-array-to-hex-string (ironclad:produce-digest sha1))))
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)); } } }
Convert this Common_Lisp snippet to C# and keep its semantics consistent.
(defun sha1-hash (data) (let ((sha1 (ironclad:make-digest 'ironclad:sha1)) (bin-data (ironclad:ascii-string-to-byte-array data))) (ironclad:update-digest sha1 bin-data) (ironclad:byte-array-to-hex-string (ironclad:produce-digest sha1))))
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)); } } }
Convert this Common_Lisp snippet to C++ and keep its semantics consistent.
(defun sha1-hash (data) (let ((sha1 (ironclad:make-digest 'ironclad:sha1)) (bin-data (ironclad:ascii-string-to-byte-array data))) (ironclad:update-digest sha1 bin-data) (ironclad:byte-array-to-hex-string (ironclad:produce-digest sha1))))
#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 ; }
Produce a language-to-language conversion: from Common_Lisp to C++, same semantics.
(defun sha1-hash (data) (let ((sha1 (ironclad:make-digest 'ironclad:sha1)) (bin-data (ironclad:ascii-string-to-byte-array data))) (ironclad:update-digest sha1 bin-data) (ironclad:byte-array-to-hex-string (ironclad:produce-digest sha1))))
#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 ; }
Transform the following Common_Lisp implementation into Python, maintaining the same output and logic.
(defun sha1-hash (data) (let ((sha1 (ironclad:make-digest 'ironclad:sha1)) (bin-data (ironclad:ascii-string-to-byte-array data))) (ironclad:update-digest sha1 bin-data) (ironclad:byte-array-to-hex-string (ironclad:produce-digest sha1))))
import crypto { sha1 } let hash = sha1.hexdigest('Ars longa, vita brevis') print hash
Rewrite the snippet below in Python so it works the same as the original Common_Lisp code.
(defun sha1-hash (data) (let ((sha1 (ironclad:make-digest 'ironclad:sha1)) (bin-data (ironclad:ascii-string-to-byte-array data))) (ironclad:update-digest sha1 bin-data) (ironclad:byte-array-to-hex-string (ironclad:produce-digest sha1))))
import crypto { sha1 } let hash = sha1.hexdigest('Ars longa, vita brevis') print hash
Translate the given Common_Lisp code snippet into Go without altering its behavior.
(defun sha1-hash (data) (let ((sha1 (ironclad:make-digest 'ironclad:sha1)) (bin-data (ironclad:ascii-string-to-byte-array data))) (ironclad:update-digest sha1 bin-data) (ironclad:byte-array-to-hex-string (ironclad:produce-digest sha1))))
package main import ( "crypto/sha1" "fmt" ) func main() { h := sha1.New() h.Write([]byte("Rosetta Code")) fmt.Printf("%x\n", h.Sum(nil)) }
Port the following code from Common_Lisp to Go with equivalent syntax and logic.
(defun sha1-hash (data) (let ((sha1 (ironclad:make-digest 'ironclad:sha1)) (bin-data (ironclad:ascii-string-to-byte-array data))) (ironclad:update-digest sha1 bin-data) (ironclad:byte-array-to-hex-string (ironclad:produce-digest sha1))))
package main import ( "crypto/sha1" "fmt" ) func main() { h := sha1.New() h.Write([]byte("Rosetta Code")) fmt.Printf("%x\n", h.Sum(nil)) }
Write a version of this D function in C with identical behavior.
void main() { import std.stdio, std.digest.sha; writefln("%-(%02x%)", "Ars longa, vita brevis".sha1Of); }
#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; }
Rewrite this program in C while keeping its functionality equivalent to the D version.
void main() { import std.stdio, std.digest.sha; writefln("%-(%02x%)", "Ars longa, vita brevis".sha1Of); }
#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; }
Transform the following D implementation into C#, maintaining the same output and logic.
void main() { import std.stdio, std.digest.sha; writefln("%-(%02x%)", "Ars longa, vita brevis".sha1Of); }
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)); } } }
Change the following D code into C# without altering its purpose.
void main() { import std.stdio, std.digest.sha; writefln("%-(%02x%)", "Ars longa, vita brevis".sha1Of); }
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)); } } }
Ensure the translated C++ code behaves exactly like the original D snippet.
void main() { import std.stdio, std.digest.sha; writefln("%-(%02x%)", "Ars longa, vita brevis".sha1Of); }
#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 ; }
Generate an equivalent C++ version of this D code.
void main() { import std.stdio, std.digest.sha; writefln("%-(%02x%)", "Ars longa, vita brevis".sha1Of); }
#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 ; }
Can you help me rewrite this code in Python instead of D, keeping it the same logically?
void main() { import std.stdio, std.digest.sha; writefln("%-(%02x%)", "Ars longa, vita brevis".sha1Of); }
import crypto { sha1 } let hash = sha1.hexdigest('Ars longa, vita brevis') print hash
Write the same code in Python as shown below in D.
void main() { import std.stdio, std.digest.sha; writefln("%-(%02x%)", "Ars longa, vita brevis".sha1Of); }
import crypto { sha1 } let hash = sha1.hexdigest('Ars longa, vita brevis') print hash
Port the following code from D to Go with equivalent syntax and logic.
void main() { import std.stdio, std.digest.sha; writefln("%-(%02x%)", "Ars longa, vita brevis".sha1Of); }
package main import ( "crypto/sha1" "fmt" ) func main() { h := sha1.New() h.Write([]byte("Rosetta Code")) fmt.Printf("%x\n", h.Sum(nil)) }
Convert this D block to Go, preserving its control flow and logic.
void main() { import std.stdio, std.digest.sha; writefln("%-(%02x%)", "Ars longa, vita brevis".sha1Of); }
package main import ( "crypto/sha1" "fmt" ) func main() { h := sha1.New() h.Write([]byte("Rosetta Code")) fmt.Printf("%x\n", h.Sum(nil)) }
Write a version of this Delphi function in C with identical behavior.
program Sha_1; 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.
#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; }
Convert the following code from Delphi to C, ensuring the logic remains intact.
program Sha_1; 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.
#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; }
Port the following code from Delphi to C# with equivalent syntax and logic.
program Sha_1; 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.
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)); } } }
Port the following code from Delphi to C# with equivalent syntax and logic.
program Sha_1; 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.
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)); } } }
Write a version of this Delphi function in C++ with identical behavior.
program Sha_1; 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.
#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 ; }
Write the same code in C++ as shown below in Delphi.
program Sha_1; 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.
#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 ; }
Keep all operations the same but rewrite the snippet in Python.
program Sha_1; 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.
import crypto { sha1 } let hash = sha1.hexdigest('Ars longa, vita brevis') print hash
Maintain the same structure and functionality when rewriting this code in Python.
program Sha_1; 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.
import crypto { sha1 } let hash = sha1.hexdigest('Ars longa, vita brevis') print hash
Translate this program into Go but keep the logic exactly as in Delphi.
program Sha_1; 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.
package main import ( "crypto/sha1" "fmt" ) func main() { h := sha1.New() h.Write([]byte("Rosetta Code")) fmt.Printf("%x\n", h.Sum(nil)) }
Ensure the translated Go code behaves exactly like the original Delphi snippet.
program Sha_1; 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.
package main import ( "crypto/sha1" "fmt" ) func main() { h := sha1.New() h.Write([]byte("Rosetta Code")) fmt.Printf("%x\n", h.Sum(nil)) }
Translate this program into C but keep the logic exactly as in 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>>
#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; }
Write the same algorithm in C as shown in this Elixir implementation.
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>>
#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; }
Ensure the translated C# code behaves exactly like the original Elixir snippet.
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>>
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)); } } }
Write the same algorithm in C# as shown in this Elixir implementation.
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>>
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)); } } }
Write a version of this Elixir function in C++ with identical behavior.
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>>
#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 ; }
Convert this Elixir block to C++, preserving its control flow and logic.
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>>
#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 ; }
Convert this Elixir block to Python, preserving its control flow and logic.
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>>
import crypto { sha1 } let hash = sha1.hexdigest('Ars longa, vita brevis') print hash
Please provide an equivalent version of this Elixir code in Python.
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>>
import crypto { sha1 } let hash = sha1.hexdigest('Ars longa, vita brevis') print hash
Change the programming language of this snippet from Elixir to Go without modifying what it does.
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>>
package main import ( "crypto/sha1" "fmt" ) func main() { h := sha1.New() h.Write([]byte("Rosetta Code")) fmt.Printf("%x\n", h.Sum(nil)) }
Port the provided Elixir code into Go while preserving the original functionality.
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>>
package main import ( "crypto/sha1" "fmt" ) func main() { h := sha1.New() h.Write([]byte("Rosetta Code")) fmt.Printf("%x\n", h.Sum(nil)) }
Convert this F# snippet to C and keep its semantics consistent.
let n = System.Security.Cryptography.SHA1.Create() Array.iter (printf "%x ") (n.ComputeHash "Rosetta Code"B)
#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; }
Write the same code in C as shown below in F#.
let n = System.Security.Cryptography.SHA1.Create() Array.iter (printf "%x ") (n.ComputeHash "Rosetta Code"B)
#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; }
Write the same code in C# as shown below in F#.
let n = System.Security.Cryptography.SHA1.Create() Array.iter (printf "%x ") (n.ComputeHash "Rosetta Code"B)
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)); } } }
Produce a functionally identical C# code for the snippet given in F#.
let n = System.Security.Cryptography.SHA1.Create() Array.iter (printf "%x ") (n.ComputeHash "Rosetta Code"B)
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)); } } }
Convert this F# block to C++, preserving its control flow and logic.
let n = System.Security.Cryptography.SHA1.Create() Array.iter (printf "%x ") (n.ComputeHash "Rosetta Code"B)
#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 ; }
Convert this F# snippet to C++ and keep its semantics consistent.
let n = System.Security.Cryptography.SHA1.Create() Array.iter (printf "%x ") (n.ComputeHash "Rosetta Code"B)
#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 ; }
Port the provided F# code into Python while preserving the original functionality.
let n = System.Security.Cryptography.SHA1.Create() Array.iter (printf "%x ") (n.ComputeHash "Rosetta Code"B)
import crypto { sha1 } let hash = sha1.hexdigest('Ars longa, vita brevis') print hash
Can you help me rewrite this code in Python instead of F#, keeping it the same logically?
let n = System.Security.Cryptography.SHA1.Create() Array.iter (printf "%x ") (n.ComputeHash "Rosetta Code"B)
import crypto { sha1 } let hash = sha1.hexdigest('Ars longa, vita brevis') print hash
Write a version of this F# function in Go with identical behavior.
let n = System.Security.Cryptography.SHA1.Create() Array.iter (printf "%x ") (n.ComputeHash "Rosetta Code"B)
package main import ( "crypto/sha1" "fmt" ) func main() { h := sha1.New() h.Write([]byte("Rosetta Code")) fmt.Printf("%x\n", h.Sum(nil)) }
Write the same algorithm in Go as shown in this F# implementation.
let n = System.Security.Cryptography.SHA1.Create() Array.iter (printf "%x ") (n.ComputeHash "Rosetta Code"B)
package main import ( "crypto/sha1" "fmt" ) func main() { h := sha1.New() h.Write([]byte("Rosetta Code")) fmt.Printf("%x\n", h.Sum(nil)) }
Preserve the algorithm and functionality while converting the code from Fortran to C#.
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
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)); } } }
Write the same algorithm in C# as shown in this Fortran implementation.
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
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)); } } }
Please provide an equivalent version of this Fortran code in C++.
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
#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 ; }
Translate the given Fortran code snippet into C++ without altering its behavior.
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
#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 ; }
Write the same algorithm in C as shown in this Fortran implementation.
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
#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; }
Change the programming language of this snippet from Fortran to C without modifying what it does.
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
#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; }
Can you help me rewrite this code in Go instead of Fortran, keeping it the same logically?
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
package main import ( "crypto/sha1" "fmt" ) func main() { h := sha1.New() h.Write([]byte("Rosetta Code")) fmt.Printf("%x\n", h.Sum(nil)) }
Write the same code in Python as shown below in 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
import crypto { sha1 } let hash = sha1.hexdigest('Ars longa, vita brevis') print hash
Generate an equivalent Python version of this Fortran code.
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
import crypto { sha1 } let hash = sha1.hexdigest('Ars longa, vita brevis') print hash
Port the provided Fortran code into PHP while preserving the original functionality.
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
<?php $string = 'Rosetta Code'; echo sha1( $string ), "\n"; ?>
Translate the given Fortran code snippet into PHP without altering its behavior.
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
<?php $string = 'Rosetta Code'; echo sha1( $string ), "\n"; ?>
Change the following Haskell code into C without altering its purpose.
module Digestor where import Data.Digest.Pure.SHA import qualified Data.ByteString.Lazy as B convertString :: String -> B.ByteString convertString phrase = B.pack $ map ( fromIntegral . fromEnum ) phrase convertToSHA1 :: String -> String convertToSHA1 word = showDigest $ sha1 $ convertString word main = do putStr "Rosetta Code SHA1-codiert: " putStrLn $ convertToSHA1 "Rosetta Code"
#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; }
Convert this Haskell block to C, preserving its control flow and logic.
module Digestor where import Data.Digest.Pure.SHA import qualified Data.ByteString.Lazy as B convertString :: String -> B.ByteString convertString phrase = B.pack $ map ( fromIntegral . fromEnum ) phrase convertToSHA1 :: String -> String convertToSHA1 word = showDigest $ sha1 $ convertString word main = do putStr "Rosetta Code SHA1-codiert: " putStrLn $ convertToSHA1 "Rosetta Code"
#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; }
Maintain the same structure and functionality when rewriting this code in C#.
module Digestor where import Data.Digest.Pure.SHA import qualified Data.ByteString.Lazy as B convertString :: String -> B.ByteString convertString phrase = B.pack $ map ( fromIntegral . fromEnum ) phrase convertToSHA1 :: String -> String convertToSHA1 word = showDigest $ sha1 $ convertString word main = do putStr "Rosetta Code SHA1-codiert: " putStrLn $ convertToSHA1 "Rosetta Code"
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)); } } }
Produce a language-to-language conversion: from Haskell to C#, same semantics.
module Digestor where import Data.Digest.Pure.SHA import qualified Data.ByteString.Lazy as B convertString :: String -> B.ByteString convertString phrase = B.pack $ map ( fromIntegral . fromEnum ) phrase convertToSHA1 :: String -> String convertToSHA1 word = showDigest $ sha1 $ convertString word main = do putStr "Rosetta Code SHA1-codiert: " putStrLn $ convertToSHA1 "Rosetta Code"
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)); } } }
Convert this Haskell block to C++, preserving its control flow and logic.
module Digestor where import Data.Digest.Pure.SHA import qualified Data.ByteString.Lazy as B convertString :: String -> B.ByteString convertString phrase = B.pack $ map ( fromIntegral . fromEnum ) phrase convertToSHA1 :: String -> String convertToSHA1 word = showDigest $ sha1 $ convertString word main = do putStr "Rosetta Code SHA1-codiert: " putStrLn $ convertToSHA1 "Rosetta Code"
#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 ; }
Write the same algorithm in C++ as shown in this Haskell implementation.
module Digestor where import Data.Digest.Pure.SHA import qualified Data.ByteString.Lazy as B convertString :: String -> B.ByteString convertString phrase = B.pack $ map ( fromIntegral . fromEnum ) phrase convertToSHA1 :: String -> String convertToSHA1 word = showDigest $ sha1 $ convertString word main = do putStr "Rosetta Code SHA1-codiert: " putStrLn $ convertToSHA1 "Rosetta Code"
#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 ; }
Write the same code in Python as shown below in Haskell.
module Digestor where import Data.Digest.Pure.SHA import qualified Data.ByteString.Lazy as B convertString :: String -> B.ByteString convertString phrase = B.pack $ map ( fromIntegral . fromEnum ) phrase convertToSHA1 :: String -> String convertToSHA1 word = showDigest $ sha1 $ convertString word main = do putStr "Rosetta Code SHA1-codiert: " putStrLn $ convertToSHA1 "Rosetta Code"
import crypto { sha1 } let hash = sha1.hexdigest('Ars longa, vita brevis') print hash
Write the same code in Python as shown below in Haskell.
module Digestor where import Data.Digest.Pure.SHA import qualified Data.ByteString.Lazy as B convertString :: String -> B.ByteString convertString phrase = B.pack $ map ( fromIntegral . fromEnum ) phrase convertToSHA1 :: String -> String convertToSHA1 word = showDigest $ sha1 $ convertString word main = do putStr "Rosetta Code SHA1-codiert: " putStrLn $ convertToSHA1 "Rosetta Code"
import crypto { sha1 } let hash = sha1.hexdigest('Ars longa, vita brevis') print hash
Translate the given Haskell code snippet into Go without altering its behavior.
module Digestor where import Data.Digest.Pure.SHA import qualified Data.ByteString.Lazy as B convertString :: String -> B.ByteString convertString phrase = B.pack $ map ( fromIntegral . fromEnum ) phrase convertToSHA1 :: String -> String convertToSHA1 word = showDigest $ sha1 $ convertString word main = do putStr "Rosetta Code SHA1-codiert: " putStrLn $ convertToSHA1 "Rosetta Code"
package main import ( "crypto/sha1" "fmt" ) func main() { h := sha1.New() h.Write([]byte("Rosetta Code")) fmt.Printf("%x\n", h.Sum(nil)) }
Ensure the translated Go code behaves exactly like the original Haskell snippet.
module Digestor where import Data.Digest.Pure.SHA import qualified Data.ByteString.Lazy as B convertString :: String -> B.ByteString convertString phrase = B.pack $ map ( fromIntegral . fromEnum ) phrase convertToSHA1 :: String -> String convertToSHA1 word = showDigest $ sha1 $ convertString word main = do putStr "Rosetta Code SHA1-codiert: " putStrLn $ convertToSHA1 "Rosetta Code"
package main import ( "crypto/sha1" "fmt" ) func main() { h := sha1.New() h.Write([]byte("Rosetta Code")) fmt.Printf("%x\n", h.Sum(nil)) }
Translate this program into C but keep the logic exactly as in J.
require '~addons/ide/qt/qt.ijs' getsha1=: 'sha1'&gethash_jqtide_ getsha1 'Rosetta Code' 48c98f7e5a6e736d790ab740dfc3f51a61abe2b5
#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; }
Change the following J code into C without altering its purpose.
require '~addons/ide/qt/qt.ijs' getsha1=: 'sha1'&gethash_jqtide_ getsha1 'Rosetta Code' 48c98f7e5a6e736d790ab740dfc3f51a61abe2b5
#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; }
Maintain the same structure and functionality when rewriting this code in C#.
require '~addons/ide/qt/qt.ijs' getsha1=: 'sha1'&gethash_jqtide_ getsha1 'Rosetta Code' 48c98f7e5a6e736d790ab740dfc3f51a61abe2b5
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)); } } }
Convert this J block to C#, preserving its control flow and logic.
require '~addons/ide/qt/qt.ijs' getsha1=: 'sha1'&gethash_jqtide_ getsha1 'Rosetta Code' 48c98f7e5a6e736d790ab740dfc3f51a61abe2b5
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)); } } }
Produce a functionally identical C++ code for the snippet given in J.
require '~addons/ide/qt/qt.ijs' getsha1=: 'sha1'&gethash_jqtide_ getsha1 'Rosetta Code' 48c98f7e5a6e736d790ab740dfc3f51a61abe2b5
#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 ; }
Write the same algorithm in C++ as shown in this J implementation.
require '~addons/ide/qt/qt.ijs' getsha1=: 'sha1'&gethash_jqtide_ getsha1 'Rosetta Code' 48c98f7e5a6e736d790ab740dfc3f51a61abe2b5
#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 ; }
Rewrite this program in Python while keeping its functionality equivalent to the J version.
require '~addons/ide/qt/qt.ijs' getsha1=: 'sha1'&gethash_jqtide_ getsha1 'Rosetta Code' 48c98f7e5a6e736d790ab740dfc3f51a61abe2b5
import crypto { sha1 } let hash = sha1.hexdigest('Ars longa, vita brevis') print hash
Preserve the algorithm and functionality while converting the code from J to Python.
require '~addons/ide/qt/qt.ijs' getsha1=: 'sha1'&gethash_jqtide_ getsha1 'Rosetta Code' 48c98f7e5a6e736d790ab740dfc3f51a61abe2b5
import crypto { sha1 } let hash = sha1.hexdigest('Ars longa, vita brevis') print hash
Change the following J code into Go without altering its purpose.
require '~addons/ide/qt/qt.ijs' getsha1=: 'sha1'&gethash_jqtide_ getsha1 'Rosetta Code' 48c98f7e5a6e736d790ab740dfc3f51a61abe2b5
package main import ( "crypto/sha1" "fmt" ) func main() { h := sha1.New() h.Write([]byte("Rosetta Code")) fmt.Printf("%x\n", h.Sum(nil)) }
Change the following J code into Go without altering its purpose.
require '~addons/ide/qt/qt.ijs' getsha1=: 'sha1'&gethash_jqtide_ getsha1 'Rosetta Code' 48c98f7e5a6e736d790ab740dfc3f51a61abe2b5
package main import ( "crypto/sha1" "fmt" ) func main() { h := sha1.New() h.Write([]byte("Rosetta Code")) fmt.Printf("%x\n", h.Sum(nil)) }
Rewrite this program in C while keeping its functionality equivalent to the Julia version.
using Nettle testdict = Dict("abc" => "a9993e364706816aba3e25717850c26c9cd0d89d", "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" => "84983e441c3bd26ebaae4aa1f95129e5e54670f1", "a" ^ 1_000_000 => "34aa973cd4c4daa4f61eeb2bdbad27316534016f",) for (text, expect) in testdict digest = hexdigest("sha1", text) if length(text) > 50 text = text[1:50] * "..." end println(" end
#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; }
Please provide an equivalent version of this Julia code in C.
using Nettle testdict = Dict("abc" => "a9993e364706816aba3e25717850c26c9cd0d89d", "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" => "84983e441c3bd26ebaae4aa1f95129e5e54670f1", "a" ^ 1_000_000 => "34aa973cd4c4daa4f61eeb2bdbad27316534016f",) for (text, expect) in testdict digest = hexdigest("sha1", text) if length(text) > 50 text = text[1:50] * "..." end println(" end
#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; }
Convert the following code from Julia to C#, ensuring the logic remains intact.
using Nettle testdict = Dict("abc" => "a9993e364706816aba3e25717850c26c9cd0d89d", "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" => "84983e441c3bd26ebaae4aa1f95129e5e54670f1", "a" ^ 1_000_000 => "34aa973cd4c4daa4f61eeb2bdbad27316534016f",) for (text, expect) in testdict digest = hexdigest("sha1", text) if length(text) > 50 text = text[1:50] * "..." end println(" end
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)); } } }
Rewrite the snippet below in C# so it works the same as the original Julia code.
using Nettle testdict = Dict("abc" => "a9993e364706816aba3e25717850c26c9cd0d89d", "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" => "84983e441c3bd26ebaae4aa1f95129e5e54670f1", "a" ^ 1_000_000 => "34aa973cd4c4daa4f61eeb2bdbad27316534016f",) for (text, expect) in testdict digest = hexdigest("sha1", text) if length(text) > 50 text = text[1:50] * "..." end println(" end
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)); } } }
Change the following Julia code into C++ without altering its purpose.
using Nettle testdict = Dict("abc" => "a9993e364706816aba3e25717850c26c9cd0d89d", "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" => "84983e441c3bd26ebaae4aa1f95129e5e54670f1", "a" ^ 1_000_000 => "34aa973cd4c4daa4f61eeb2bdbad27316534016f",) for (text, expect) in testdict digest = hexdigest("sha1", text) if length(text) > 50 text = text[1:50] * "..." end println(" end
#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 ; }
Change the following Julia code into C++ without altering its purpose.
using Nettle testdict = Dict("abc" => "a9993e364706816aba3e25717850c26c9cd0d89d", "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" => "84983e441c3bd26ebaae4aa1f95129e5e54670f1", "a" ^ 1_000_000 => "34aa973cd4c4daa4f61eeb2bdbad27316534016f",) for (text, expect) in testdict digest = hexdigest("sha1", text) if length(text) > 50 text = text[1:50] * "..." end println(" end
#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 ; }
Change the following Julia code into Python without altering its purpose.
using Nettle testdict = Dict("abc" => "a9993e364706816aba3e25717850c26c9cd0d89d", "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" => "84983e441c3bd26ebaae4aa1f95129e5e54670f1", "a" ^ 1_000_000 => "34aa973cd4c4daa4f61eeb2bdbad27316534016f",) for (text, expect) in testdict digest = hexdigest("sha1", text) if length(text) > 50 text = text[1:50] * "..." end println(" end
import crypto { sha1 } let hash = sha1.hexdigest('Ars longa, vita brevis') print hash
Write the same code in Python as shown below in Julia.
using Nettle testdict = Dict("abc" => "a9993e364706816aba3e25717850c26c9cd0d89d", "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" => "84983e441c3bd26ebaae4aa1f95129e5e54670f1", "a" ^ 1_000_000 => "34aa973cd4c4daa4f61eeb2bdbad27316534016f",) for (text, expect) in testdict digest = hexdigest("sha1", text) if length(text) > 50 text = text[1:50] * "..." end println(" end
import crypto { sha1 } let hash = sha1.hexdigest('Ars longa, vita brevis') print hash
Port the provided Julia code into Go while preserving the original functionality.
using Nettle testdict = Dict("abc" => "a9993e364706816aba3e25717850c26c9cd0d89d", "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" => "84983e441c3bd26ebaae4aa1f95129e5e54670f1", "a" ^ 1_000_000 => "34aa973cd4c4daa4f61eeb2bdbad27316534016f",) for (text, expect) in testdict digest = hexdigest("sha1", text) if length(text) > 50 text = text[1:50] * "..." end println(" end
package main import ( "crypto/sha1" "fmt" ) func main() { h := sha1.New() h.Write([]byte("Rosetta Code")) fmt.Printf("%x\n", h.Sum(nil)) }
Write the same algorithm in Go as shown in this Julia implementation.
using Nettle testdict = Dict("abc" => "a9993e364706816aba3e25717850c26c9cd0d89d", "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" => "84983e441c3bd26ebaae4aa1f95129e5e54670f1", "a" ^ 1_000_000 => "34aa973cd4c4daa4f61eeb2bdbad27316534016f",) for (text, expect) in testdict digest = hexdigest("sha1", text) if length(text) > 50 text = text[1:50] * "..." end println(" end
package main import ( "crypto/sha1" "fmt" ) func main() { h := sha1.New() h.Write([]byte("Rosetta Code")) fmt.Printf("%x\n", h.Sum(nil)) }
Port the following code from Lua to C with equivalent syntax and logic.
#!/usr/bin/lua local sha1 = require "sha1" for i, str in ipairs{"Rosetta code", "Rosetta Code"} do print(string.format("SHA-1(%q) = %s", str, sha1(str))) end
#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; }
Write the same code in C as shown below in Lua.
#!/usr/bin/lua local sha1 = require "sha1" for i, str in ipairs{"Rosetta code", "Rosetta Code"} do print(string.format("SHA-1(%q) = %s", str, sha1(str))) end
#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; }
Produce a language-to-language conversion: from Lua to C#, same semantics.
#!/usr/bin/lua local sha1 = require "sha1" for i, str in ipairs{"Rosetta code", "Rosetta Code"} do print(string.format("SHA-1(%q) = %s", str, sha1(str))) end
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)); } } }
Convert the following code from Lua to C#, ensuring the logic remains intact.
#!/usr/bin/lua local sha1 = require "sha1" for i, str in ipairs{"Rosetta code", "Rosetta Code"} do print(string.format("SHA-1(%q) = %s", str, sha1(str))) end
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)); } } }