How to encrypt with sha256 given a public key ?

245 views
Skip to first unread message

bedipr...@gmail.com

unread,
Sep 12, 2025, 2:55:49 PM (8 days ago) Sep 12
to Harbour Users
Hi All

I need to encrypt some information, mostly 13 charatcter strings through Sha_256 algorithm supplying its public key.

Any help is apppreciated.


Pritpal Bedi

marcos...@gmail.com

unread,
Sep 12, 2025, 4:45:45 PM (8 days ago) Sep 12
to Harbour Users
Hi


"I need to encrypt some information, mostly 13 charatcter strings through Sha_256 algorithm supplying its public key."

What you are asking for cannot be done; they are two types of encryption for different needs.

Harbour has SHA, but it does not have RSA, which is the standard for asymmetric encryption.

These are the SHA algorithm functions that Harbour has:

PROCEDURE Main()

   ? "hb_SHA1"
   ?  hb_SHA1("The world is unique and colorful")
   ?
   ? "hb_SHA224"
   ? hb_SHA224("The world is unique and colorful")
   ?
   ? "hb_SHA256"
   ? hb_SHA256("The world is unique and colorful")
   ?
   ? "hb_SHA384"
   ? hb_SHA384("The world is unique and colorful")
   ?
   ? "hb_SHA512"
   ? hb_SHA512("The world is unique and colorful")
   ?
    wait

RETURN

Output:

hb_SHA1
9990a8e1901bc063cdabac5f6307dd32ecf5b1ec

hb_SHA224
29e549fa1d3fff8dd7d2a11866b648f62d81f9ae6bb9349ef6f7a344

hb_SHA256
fec0ed8762f3cad9b2206cc8befad0b1d9002b48191c2f929e7461ae711d52a5

hb_SHA384
4761b630c1e128fabf30a9675624a13439d8cb1174b12c3fa9b583101d833a2ea81753501e57370f990a75febb7cc148

hb_SHA512
6e75d9444dbe323bf618482ff2a0fec255143b649d3930b3dc87f0dd00aa90bc937e8dfe1a131d21290cf255639ee8867433ab70c69023ffc7afb137fe71432a

Press any key to continue...


hb_SHA1(<cBuffer> [, <lBin>]) ➜ cDigest
hb_SHA224(<cBuffer> [, <lBin>]) ➜ cDigest
hb_SHA256(<cBuffer> [, <lBin>]) ➜ cDigest
hb_SHA384(<cBuffer> [, <lBin>]) ➜ cDigest
hb_SHA512(<cBuffer> [, <lBin>]) ➜ cDigest

Above functions are used to calculate hash value (digest) of given <cBuffer> according to SHA-1, SHA-2, SHA-3 Secure Hash Algorithms.

<lBin> flag controls whether to return binary data or hexadecimal string, default .F., that is, return hexadecimal string.

https://en.wikipedia.org/wiki/Secure_Hash_Algorithms
https://www.cohesity.com/glossary/rsa-encryption/

The RSA algorithm would have to be implemented in C so that Harbour can handle public and private keys.

Sincerely,

Marcos Jarrin

bedipr...@gmail.com

unread,
Sep 12, 2025, 8:41:21 PM (8 days ago) Sep 12
to Harbour Users
I know hb_sha??? usage and employ them signing AWS cloud signature version 4.

My question was different and now seems that can't be materialized as you already mentioned. I probably used wrong terminology.  My requirement belongs to RSA encryption applying sha protocol.

Thanks

Pritpal Bedi
a student of software analysis & concepts

Przemyslaw Czerpak

unread,
Sep 13, 2025, 7:47:07 AM (7 days ago) Sep 13
to harbou...@googlegroups.com
Hi Pritpal,

   /* general version */
   function encryptAsync( cPubKeyFile, cData, /*@*/cDataEnc )
   local pPubKey, pPubKeyCTX
      if empty( pPubKey := PEM_READ_PUBKEY( cPubKeyFile ) )
         outErr( "Cannot read public key: " + cPubKeyFile + hb_eol() )
      elseif empty( pPubKeyCTX := EVP_PKEY_CTX_new( pPubKey ) )
         outErr( "Public key context initialization error." + hb_eol() )
      elseif EVP_PKEY_encrypt_init( pPubKeyCTX ) <= 0
         outErr( "Public key encryption initialization error." + hb_eol() )
      elseif EVP_PKEY_encrypt( pPubKeyCTX, @cDataEnc, cData ) <= 0
         outErr( "Encryption with publiv key error." + hb_eol() )
      else
         return .t.
      endif
   return .f.

   /* RSA only version */
   function encryptRSA( cPubKeyFile, cData, /*@*/cDataEnc )
   local pRSAPubKey
      if empty( pRSAPubKey := PEM_READ_BIO_RSA_PUBKEY( cPubKeyFile ) )
         outErr( "Cannot read RSA public key: " + cPubKeyFile + hb_eol() )
      elseif RSA_public_encrypt( pRSAPubKey, cData, @cDataEnc ) <= 0
         outErr( "Encryption with RSA publiv key error." + hb_eol() )
      else
         return .t.
      endif
   return .f.

best regards,
Przemek


W dniu 13.09.2025 o 02:41, bedipr...@gmail.com pisze:
--
You received this message because you are subscribed to the Google Groups "Harbour Users" group.
Unsubscribe: harbour-user...@googlegroups.com
Web: https://groups.google.com/group/harbour-users
---
You received this message because you are subscribed to the Google Groups "Harbour Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to harbour-user...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/harbour-users/ce7c7fcc-ba3a-4abf-8030-de1702f5c2e0n%40googlegroups.com.

marcos...@gmail.com

unread,
Sep 13, 2025, 2:57:43 PM (7 days ago) Sep 13
to Harbour Users
Hello

You can consult with this AI about RSA in Harbour

https://deepwiki.com/harbour/core

Sincerely

Marcos Jarrin

bedipr...@gmail.com

unread,
Sep 14, 2025, 10:27:54 PM (6 days ago) Sep 14
to Harbour Users
Thank you very much Przemek

Another question, can we provide publickey content as a buffer also instead of a file ?

Thanks again.

Pritpal Bedi
a student of software analysis & concepts

Przemyslaw Czerpak

unread,
Sep 15, 2025, 7:30:39 AM (5 days ago) Sep 15
to harbou...@googlegroups.com
Hi Pritpal,

cPubKeyData := hb_memoRead( cPubKeyFile )
cPubKeyBIO := BIO_new_mem_buf( cPubKeyData )

and then use cPubKeyBIO just like cPubKeyFile, i.e.

    encryptAsync( cPubKeyBIO, cData, @cDataEnc )
    encryptRSA( cPubKeyBIO, cData, @cDataEnc )


best regards,
Przemek


W dniu 15.09.2025 o 04:27, bedipr...@gmail.com pisze:

bedipr...@gmail.com

unread,
Sep 15, 2025, 1:08:24 PM (5 days ago) Sep 15
to Harbour Users
Perfect !!!

Thanks Przemek.

Pritpal Bedi
a student of software analysis & concepts

Reply all
Reply to author
Forward
0 new messages