#1 2025-02-01 07:28:26

anouri
Member
Registered: 2024-02-11
Posts: 78

How can I sign file with mormot RSA?

Hi.
I can't find a way for signing a file with mormot.
I search forum and googled and can't find anything about that.

Offline

#2 2025-02-01 10:14:02

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,863
Website

Re: How can I sign file with mormot RSA?

Use StringFromFile() then ICryptPrivateKey.Sign().

Offline

#3 2025-02-02 09:29:06

anouri
Member
Registered: 2024-02-11
Posts: 78

Re: How can I sign file with mormot RSA?

Result is empty string:
key = '-----BEGIN RSA PRIVATE KEY-----    ...'

  var pub := TCryptPublicKeyEcc.Create;
  pub.Load(ckaEcc256, Memo2.Lines.Text);

  var key := TCryptPrivateKeyEcc.Create;
  key.Load(ckaEcc256,pub,Memo1.Lines.Text,'');
  var s := key.Sign(caaRS256,'1'); // s = ''

Offline

#4 2025-02-02 10:00:42

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,863
Website

Re: How can I sign file with mormot RSA?

You set ckaEcc256 with a RSA key.

Offline

#5 2025-02-02 10:17:20

anouri
Member
Registered: 2024-02-11
Posts: 78

Re: How can I sign file with mormot RSA?

when I set ckaRsa : key.Load(ckaRsa,pub,Memo1.Lines.Text,'');

erorr : TCryptPublicKeyEcc.Create: unsupported ckaNone

I debuged it little bit in TCryptPublicKeyEcc.Load function only ckaEcc256 supported.

Last edited by anouri (2025-02-02 10:18:38)

Offline

#6 2025-02-02 13:28:11

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,863
Website

Re: How can I sign file with mormot RSA?

You need to define mormot.core.rsa in your uses clause, otherwise the RSA algorithm is not registered.

Offline

#7 2025-02-02 13:39:02

anouri
Member
Registered: 2024-02-11
Posts: 78

Re: How can I sign file with mormot RSA?

There is no mormot.core.rsa but mormot.crypt.rsa was in uses clause.

Offline

#8 2025-02-02 13:51:56

anouri
Member
Registered: 2024-02-11
Posts: 78

Re: How can I sign file with mormot RSA?

I am trying to use

OpenSslSign('',pointer(msg),pointer(priv),Length(msg),Length(priv),sig)

raise Access violation in mormot.crypt.openssl
function :
function BIO_new_mem_buf(buf: pointer; len: integer): PBIO;

Last edited by anouri (2025-02-02 13:56:35)

Offline

#9 2025-02-02 16:18:41

anouri
Member
Registered: 2024-02-11
Posts: 78

Re: How can I sign file with mormot RSA?

This one works.
I don't know is it right or not.

  RegisterOpenSsl;

  var aa := CryptAsymOpenSsl[caaPS512].Create('secp256r1'); 
//secp192r1 ,secp224r1 ,secp256r1 = prime256v1 ,secp384r1 ,secp521r1 , brainpoolP256r1 ,brainpoolP384r1 ,brainpoolP512r1
  var prv,pub: RawUtf8;

  prv := Memo1.Lines.Text;  
  aa.Sign(msg,prv,sig);

  pub := Memo2.Lines.Text;
  if aa.Verify(msg,pub,sig) then
    ShowMessage('verified');

Last edited by anouri (2025-02-02 16:20:04)

Offline

#10 2025-02-04 15:45:16

anouri
Member
Registered: 2024-02-11
Posts: 78

Re: How can I sign file with mormot RSA?

Hi.
I got abstract error in Generate:

var
  LICryptPrivateKey: ICryptPrivateKey;
begin
  LICryptPrivateKey := TCryptPrivateKey.Create;
  TCryptPrivateKey(LICryptPrivateKey).Generate(caaRS256);

Offline

#11 2025-02-04 16:16:26

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,863
Website

Re: How can I sign file with mormot RSA?

Use the factory functions, not directly the classes.

Offline

#12 2025-02-04 16:26:53

anouri
Member
Registered: 2024-02-11
Posts: 78

Re: How can I sign file with mormot RSA?

Thank you so much.
solved ❤️

Offline

#13 2025-02-05 11:16:58

anouri
Member
Registered: 2024-02-11
Posts: 78

Re: How can I sign file with mormot RSA?

I have another problem. This code does not generate correct result for unicode characters. for ansi chars it is ok.
Seems I have problem converting string to rawbytestring?

  var CryptPrivateKeyRsa := TCryptPrivateKeyRsa.Create;
  CryptPrivateKeyRsa.Load(ckaRsa, nil,Memo1.Lines.Text,'');

  var msg: RawByteString;
  msg := Edit1.Text;
  //var sig := CryptPrivateKeyRsa.Sign(caaRS256,msg);
  var sig := CryptPrivateKeyRsa.Sign(caaRS256,pointer(msg),Length(msg));

  var sig64:= BinToBase64(sig);
  Memo3.Lines.Text := sig64;

Offline

#14 2025-02-05 11:22:46

anouri
Member
Registered: 2024-02-11
Posts: 78

Re: How can I sign file with mormot RSA?

Sorry for the stupid question.
Using StringToUtf8 solved the problem:

msg := StringToUtf8(Edit1.Text);

Last edited by anouri (2025-02-05 11:23:07)

Offline

Board footer

Powered by FluxBB