CryptoSys Home > Using Visual FoxPro

Using Visual FoxPro with CryptoSys API, CryptoSys PKI and FirmaSAT


We don't use FoxPro ourselves, but it is possible to call all the functions in our cryptographic products CryptoSys API, CryptoSys PKI and FirmaSAT from FoxPro. This page explains what we know. If anyone has any corrections or would like to send in examples of working FoxPro declarations, we'd be delighted to hear from you. Please send us a message. Thanks to Hugo Chavez and Hugo Sols for their FoxPro code examples for FirmaSAT and New Bálint Korpa for his SHA-3-512 example for CryptoSys API.

For more info on creating interfaces see Writing an interface in another programming language.

As far as we know, all the DECLARE statements can be done using only [@]STRING and INTEGER types.

Where a "long" is used in CryptoSys PKI, use a FoxPro INTEGER. The "long" type in VB6 and C is a 32-bit signed integer.

As far as FoxPro is concerned, CryptoSys API, CryptoSys PKI and FirmaSAT treat both a string and a byte array identically, except a string must have an extra zero character at the end and a byte array does not, but the length of the byte array must be specified separately. Any extra bytes passed by the FoxPro function will be ignored. Only ANSI strings with single 8-bit characters are used in CryptoSys, not Unicode.

We believe that FoxPro may automatically add an extra CHR(0) to the end of a STRING when passing to a DLL and so you may need to dimension your variable with an extra character at the end (see the second web site below). It certainly does not hurt to have your STRING types dimensioned to a greater length than required, provided either (a) a string is terminated with a zero byte or (b) the correct length for the byte array is specified in the length parameter.

We recommend that you use the C interfaces in the diCryptoSys.h, diCrPKI.h and diFirmaSat2.h include files rather than the VB6 ones to derive the FoxPro declare statements. You should note that the only difference in type between a "string" and a "byte array" is that the former is char *str and the latter is unsigned char *b. These should both be STRING types as far as FoxPro is concerned. The strings and byte arrays marked "const" in the C declaration are not changed, but the ones without const are meant to accept output so presumably need the @ prefix in FoxPro.

These two web sites should be helpful:

Using the first web site (now defunct, sorry) and the .h include files, should enable you to write the correct DECLARE statements to use CryptoSys API, CryptoSys PKI and FirmaSAT with FoxPro.

Example

C declaration:
long _stdcall HASH_HexFromBytes(
  char *szOutput, 
  long nMaxChars, 
  const void *lpMessage, 
  long nMsgLen, 
  long nOptions);
FoxPro declaration:
DECLARE INTEGER HASH_HexFromBytes IN diCrPKI.dll;
  STRING  @szOutput,;
  INTEGER nMaxChars,;
  STRING  lpMessage,;
  INTEGER nMsgLen,;
  INTEGER nOptions

It is possible that the reference to a byte array (unsigned char* or void *) may be passed as an INTEGER type instead of STRING.

Disclaimer

Our CryptoSys products are offered primarily with interfaces using the C/C++, C#, VB6/VBA and VB.NET languages. We don't use or offer to support any other interfaces. We will try and help if we can. We haven't checked anything on this page ourselves. Please make whatever checks you need to before purchase.

Comments by Users

SHA-3-512

************************************************************
*  FUNCTION sha3_512
************************************************************
*  Author...................:  Balint Korpa
*  Calling sample...........:  sha3_512("yourstring")

FUNCTION sha3_512

  LPARAMETERS tcString


    declare LONG SHA3_Init IN diCryptoSys ;
        LONG    nHashBitLen
    declare LONG SHA3_AddString IN diCryptoSys ; 
         long hContext, STRING @szMessage
    declare INTEGER SHA3_LengthInBytes IN diCryptoSys ;
        LONG    hContext
    DECLARE INTEGER SHA3_HexDigest IN diCryptoSys ;
            STRING  @szOutput, LONG nMaxChars, LONG hContext


    LOCAL hContext AS Long ,;
          lcstring AS String ,;
          nRet AS Integer, strDigest as String, nBytes AS Integer,lnRetBytes AS Integer 
          
    hContext=SHA3_Init(512) && You can choose another parameter also
    IF hContext = 0
        ErrorMsg("SHA-3 initialization error!")
        RETURN .F.
    ENDIF
    lcstring = tcString 
    nRet = SHA3_AddString(hContext, lcstring)
    *WAIT WINDOW nRet

    nBytes = SHA3_LengthInBytes(hContext)
    nLen = (nBytes*2)
    strDigest = Replicate(" ", nLen)
    *WAIT WINDOW LEN(strDigest)
    lnRetBytes = SHA3_HexDigest(@strDigest, nLen, hContext) 

    RETURN strDigest

ENDFUNC
Bálint Korpa | - Mon, Jun 1 2020 00:45 GMT

En modo de cmd

*::_________________

? LIBINFO() 

PROCEDURE LIBINFO() 
      LOCAL lcComando, lcArchivo, lcLibInfo, oShell       
      STORE "" TO lcComando, lcLibInfo, lcArchivo
      lcComando = ""
      lcComando = lcComando + "C:\FIRMASAT\FirmaSAT "
      lcComando = lcComando + "LIBINFO > Resultado.Tmp"
      
      lcArchivo = GetEnv("TEMP") + "" + Sys(2015) + ".bat"
      StrToFile(lcComando, lcArchivo)
      _CLIPTEXT = lcComando 

      ERASE Resultado.Tmp

      oShell = CREATEOBJEC("WScript.Shell")
      oShell.Run(lcArchivo, 0, 1)
      IF FILE("Resultado.Tmp")
         lcLibInfo = FILETOSTR("Resultado.Tmp")
         RETURN lcLibInfo 
      ELSE   
         RETURN "* Error *" + "LIBINFO"
      ENDIF
   ENDPROC
Hugo C. | - Fri, Jun 4 2010 22:06 GMT

Usando las DLLs

#DEFINE SAT_ENCODE_UTF8   0
#DEFINE SAT_ENCODE_LATIN1 1
CLEAR
LOCAL nc AS Integer
LOCAL strOut, strKeyFile, strPassword As String

strXmlFile = "C:\CFD\Facturas\A1006.xml"
strKeyFile = "C:\CFD\LLAVES\PRIVADA\MiLLave.KEY"  
strPassword = "MiPassWord"

DO DeclaraDlls  && declarar las DLLs
? " VERSION    : " + TRANSFORM(SAT_Version())
? " XML VALIDO : " + IIF(SAT_ValidateXml(strXmlFile, 0) == 0, "OK", "NO")
nc = SAT_MakeSignatureFromXml("", 0, strXmlFile, strKeyFile, strPassword)
IF nc <= 0 
   ? "ERROR"
   RETURN
ENDIF
strOut = SPACE(nc)    
nc = SAT_MakeSignatureFromXml(@strOut, nc, strXmlFile, strKeyFile, strPassword)
If nc > 0 Then
   ? "SELLO : " + strOut
EndIf
CLEAR DLLS    


PROCEDURE DeclaraDlls
* Declarar las dlls
DECLARE INTEGER SAT_Version IN diFirmaSAT2.dll
DECLARE INTEGER SAT_ValidateXml IN diFirmaSAT2.dll;
    STRING strXmlFile,;
    INTEGER nOptions 
DECLARE INTEGER SAT_MakeSignatureFromXml IN diFirmaSAT2.dll;
    STRING @strOut ,;
    INTEGER nOutChars ,;
    STRING strXmlFile ,;
    STRING strKeyFile ,; 
    STRING strPassword 
hugo C. | - Wed, Jun 9 2010 19:57 GMT

Next ...

LOCAL loFS AS OBJECT
ofS = CREATEOBJECT("FoxFirmaSat")
*CLEAR &&SET STEP ON 
? ofS.satModuleName()
? ofS.satCompileTime()
? ofS.satPKIModuleName()
? ofS.satPKICompileTime()
RETURN

#DEFINE SAT_ENCODE_UTF8 0
#DEFINE SAT_ENCODE_LATIN1 1
DEFINE CLASS FoxFirmaSat AS Custom

   PROCEDURE INIT
      THIS.DeclaraDLLS()
   ENDPROC
   PROCEDURE DESTROY
      CLEAR DLLS
   ENDPROC
   
   Function satModuleName() AS STRING 
   
    LOCAL nc  AS INTEGER
    LOCAL strOut AS STRING 
    nc = SAT_ModuleName("", 0, 0)
    If nc <= 0 
       RETURN THIS.satLastError()
    ENDIF   
    strOut = SPACE(nc)
    nc = SAT_ModuleName(@strOut, nc, 0)
    If nc > 0 Then
        RETURN strOut
    Else  
        RETURN THIS.satLastError()
    EndIf
    
   ENDFUNC........
Hugo C. | - Thu, Jun 10 2010 04:01 GMT

*************************
FUNCTION _GENERA_PIPE      && FUNCION PARA GENERAR LA CADENA ORIGINAL A PARTIR DEL XML (VALIDADO)
LPARAMETER xmlFile         && C:\CFD\Factura.xml
*************************
LOCAL lnBytes
PRIVATE pcPipe
lnBytes = SAT_MakePipeStringFromXml('',0, xmlFile)
pcPipe  = SPACE( lnBytes)
lnBytes = SAT_MakePipeStringFromXml(@pcPipe, lnBytes, xmlFile)
??pcPipe
??lnBytes
RETURN
Hugo Sols | Chihuahua, Chih., Mex - Thu, Sep 2 2010 01:30 GMT
COMO OBTENER ATRIBUTOS DEL XML
HOW TO GET ATTRIBUTES FROM XML FILE

1.- DECLARATION 
DECLARE INTEGER SAT_GetXmlAttribute IN diFirmaSAT2.dll;
    STRING @strOut ,;
    INTEGER nOutChars , ;
    STRING xmlFile ,;
    STRING cAttributeName ,;
    STRING cElementName 

2.- Funcion
CREATE FUNCTION Procesa_Campo
PARAMETERS pcCampo,  ; && eJ: noAprobacionm, anoAprobacion, sello 
       pcElemento  && ej: Comprobante, Receptor, Emisor

PRIVATE pcValor
LOCAL lnBytes

lnBytes = SAT_GetXmlAttribute( '', 0, C:\CFD\Factura.xml , pcCampo,
pcElemento)
pcValor = SPACE( lnBytes)
lnBytes = SAT_GetXmlAttribute( @pcValor, lnBytes, C:\CFD\Factura.xml ,
pcCampo, pcElemento)

RETURN pcValor

HUGO SOLIS | CHIH, MEXICO - Mon, Mar 26 2012 21:23 GMT

In your example of SAT_MakePipeStringFromXml, there exist 3 parameters, but in the API manual there exists only 1 parameter. Where can I find the correct documentation of FirmaSat Functions?

Jess | - Tue, Sep 28 2010 08:38 GMT

SAT_MakePipeStringFromXml has four parameters! Any code in these comments is provided by others. We have not tested it.

The correct documentation for the functions for C and VB6 (and Foxpro) users is in the file diFirmaSat2.h

http://cryptosys.net/firmasat/diFirmaSat2.h.html

However, the .NET method for VB.NET and C# users Sat.MakePipeStringFromXml does have only one parameter (xmlFile), but that is a different interface and not relevant for FoxPro users.

Dave | Moderator - Sun, Oct 3 2010 13:17 GMT

See also the more readable HTML document "diFirmaSat2.h File Reference"

http://cryptosys.net/firmasat/diFirmaSat2_h_Ref.html
Dave | Moderator - Mon, Mar 26 2012 21:25 GMT

Contact

For more information or to comment on this page, please send us a message.

This page last updated 10 September 2025