dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Rsa-sha1

  1. #1
    Join Date
    Feb 2007
    Posts
    65

    Question Rsa-sha1

    Hi

    I want to implement RSA-SHA1 algorithm in C++, which has to basically a file content using private key, here i have Perl code and looking for a corresponding C++ code, can any one help me?

    PERL Code

    Code:
    # Sign a document with given private key file.
    # The first argument is the document to sign
    # The second argument is the name of the private key file
    sub rsa_sha1_sign {
        my ($to_sign, $pvkFile) = @_;
    
        return write_to_program("openssl sha1 -sign $pvkFile", $to_sign);
    }
    
    # Helper function to write data to a program
    sub write_to_program {
        my ($prog, $data) = @_;
    
        my $pid = open2(*README, *WRITEME, $prog);
        print WRITEME $data;
        close WRITEME;
    
        # slurp entire contents of output into scalar
        my $output;
        local $/ = undef;
        $output = <README>;
        close README;
    
        waitpid($pid, 0);
    
        return $output;
    }
    Last edited by Satsumatable; December 10th, 2009 at 12:05 AM.

  2. #2
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: Rsa-sha1

    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  3. #3
    Join Date
    Feb 2005
    Posts
    2,160

    Re: Rsa-sha1

    Or just use the crypto API.

    Code:
    #include <wincrypt.h>
    
    int CalcSHAHash(void)
    {
      HCRYPTPROV hProv;
      HCRYPTHASH hHash;
      char mtest[]="String To Hash";
      unsigned char hsh[20];  //SHA hash is 20 bytes
      char hexbuf[41];  //string representation of hash (2 hex chars for each byte of hash)
      char tmp[3];
      unsigned long sz;
      int rc;
    
      rc=CryptAcquireContext(&hProv,NULL,MS_STRONG_PROV,PROV_RSA_FULL,0);
      if(!rc){  //maybe no keyset (first time use of crypto API)
        rc=CryptAcquireContext(&hProv,NULL,MS_STRONG_PROV,PROV_RSA_FULL,CRYPT_NEWKEYSET);
        if(!rc){
          return 0;
        }
      }
      CryptCreateHash(hProv,CALG_SHA,0,0,&hHash);
      CryptHashData(hHash,(unsigned char *)mtest,strlen(mtest),0);
    
      sz=20;
      CryptGetHashParam(hHash,HP_HASHVAL,hsh,&sz,0);
      hexbuf[0]=0;
      for(int i=0;i<sz;i++){
        sprintf(tmp,"&#37;.2X",hsh[i]);
        strncat(hexbuf,tmp,2);
      }
      printf("%s\n",hexbuf);
      CryptDestroyHash(hHash);
      CryptReleaseContext(hProv,0);
      return 1;
    }

  4. #4
    Join Date
    Mar 2015
    Posts
    1

    Re: Rsa-sha1

    I've been searching for a working algorithm for sha1 and finally the only working solution is by hoxsiew
    Thank you!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)