
///     -  MS CRYPTO API Class iscapi.Signer Extends %RegisteredObject { ///  DLL /// dllPath -     ClassMethod LoadDLL(dllPath As %String) As %Status { s result = $$$OK if (dllPath = "") { w "Please set dllPath equal to path to the ISCAPI.dll" q $$$ERROR($$$GeneralError, "No path to iscapi.dll is provided") } try { d $zf(-3, dllPath) } catch (ex) { s result = ex.AsStatus() } if (result=1) {w "DLL from "_dllPath_" was loaded"} else {w "Cannot load DLL from "_dllPath} q result } ///  DLL ClassMethod UnloadDLL() { d $zf(-3, "") } ///  -. /// provType -   (VipNet=2, CryptoPro=75) /// algId -   (32798) /// containerName -      /// pin -    (  , CSP      ,    ) /// providerName -  - ClassMethod Init(provType = 75, algId = 32798, containerName As %String, pin As %String = "111111", providerName As %String = "") As %Status { s result = $$$OK try { d $zf(-3, "", "Init", provType, algId, containerName, pin, providerName) } catch (ex) { s result = ex.AsStatus() } if (result=1) {w "CSP was successfully initialized"} else {w "Error during CSP initialization"} q result } ///  . /// logFileName -   .     . /// logLevel -   /// 0 -  /// 1 -   /// 2 -   /// logTargets - ,      /// 0 -    /// 1 -    /// 2 -    /// 3 -    ClassMethod InitLogger(logFileName As %String = "c:\iscapi.log", logLevel As %Integer = 2, logTargets As %Integer = 3) As %Status { s result = $$$OK try { d $zf(-3, "", "InitLogger", logFileName, logLevel, logTargets) } catch ex { s result = ex.AsStatus() } if (result=1) {w "Logger was successfully initialized"} else {w "Error during Logger initialization"} q result } ///   . ///         ,       . ClassMethod HashData(dataPortion As %String) As %Status { s result = $$$OK try { d $zf(-3, "", "HashData", dataPortion) } catch ex { s result = ex.AsStatus() } q result } ///  . ClassMethod HashFile(fileName As %String) As %Status { s result = $$$OK try { d $zf(-3, "", "HashFile", fileName) } catch ex { s result = ex.AsStatus() } q result } ///   . ///     ClassMethod GetHashValue() As %String { s result = "" try { s result = $zf(-3, "", "GetHashValue", "") } catch ex { w "GHV exception", ! zw ex s result = "" } w "GHV result is:", result, ! q result } ClassMethod ExportUserKey() As %String { s result = "" try { s result = $zf(-3, "", "ExportUserKey", "") } catch ex { s result = "" } q result } ///   . ///  . ClassMethod SignNewHash(dataPortion As %String) As %String { s result = "" try { s result = $zf(-3, "", "SignNewHash", dataPortion, "") } catch ex { s result = "" } q result } ///    . ClassMethod SignCurrentHash() As %String { s result = "" try { s result = $zf(-3, "", "SignCurrentHash", "") } catch ex { s result = "" } w "Signature recieved: ",result,! q result } ///   . ClassMethod VerifyHash(hash As %String, sign As %String) As %Boolean { s result = 0 try { s result = $zf(-3, "", "VerifyHash", hash, sign, 0) } catch ex { s result = 0 } q result } ClassMethod VerifyHashByKey(hash As %String, sign As %String, pubKey As %String) As %Boolean { s result = 0 try { s result = $zf(-3, "", "VerifyHashByKey", hash, sign, pubKey, 0) } catch ex { s result = 0 } q result } ///      . ///    ClassMethod VerifySignature(dataPortion As %String, sign As %String) As %Boolean { s result = 0 try { s result = $zf(-3, "", "VerifySignature", dataPortion, sign, 0) } catch ex { s result = 0 } q result } ///     DLL ClassMethod ReleaseAll() As %Status { s result = $$$OK try { d $zf(-3, "", "ReleaseAll") d ..UnloadDLL() } catch ex { s result = ex.AsStatus() } q result } ///     HEX /// TODO: rewrite ClassMethod ByteToHex(bString As %String) As %String { s str = "" for i=1:1:$l(bString) { s hex = $zhex($ascii($e(bString, i))) if ($l(hex) = 1) s hex = "0" _ hex s str = str _ hex } q str } ClassMethod HexToString(value As %String) As %String { s str = "" for i=1:2:$l(value) { s hex = $e(value, i, i + 1) s str = str _ $c($zhex(hex)) } q str } ///  DLL ClassMethod PrintProviders() As %Status { s result = $$$OK try { d $zf(-3, "", "PrintProviders") } catch ex { s result = ex.AsStatus() } q result } ///   ClassMethod Test() { s data = "123!" d ..LoadDLL("C:\ISCAPI.dll") w "DLL loaded", ! d ..InitLogger("c:\iscapiL.txt", 2, 1) w "Logger initialized", ! d ..PrintProviders() d ..Init(75, 32798, "CacheCrypt", "", "Crypto-Pro GOST R 34.10-2001 Cryptographic Service Provider") w "CSP initialized", ! d ..HashData(data) w "Hash created on: ", data, ! s hash = ..GetHashValue() w "Hash received, hash length=", $l(hash), ! w "Hash to base64:", !, $system.Encryption.Base64Encode(hash), ! w "Hash to HEX:", !, ..ByteToHex(hash), ! w "Hash value:", hash, ! s sign = ..SignCurrentHash() w "Hash signed, sign length=", $l(sign), ! w "Sign to base64:", !, $system.Encryption.Base64Encode(sign), ! w "Sign to HEX:", !, ..ByteToHex(sign), ! s vfy = ..VerifyHash(hash, sign) w "Verifying Hash signature result = ", vfy, ! s vfy = ..VerifySignature(data, sign) w "Verifying Signature by input text result = ", vfy, ! w "Exporting User Key...",! s userKey = ..ExportUserKey() w "Size: ", $l(userKey), ! w "UserKeyBytes: ", ..ByteToHex(userKey), ! s vfy = ..VerifyHashByKey(hash, sign, userKey) w "Verifying Hash signature ByKey result = ", vfy, ! d ..ReleaseAll() } } Source: https://habr.com/ru/post/205952/
All Articles