### Initializing GOST34132015ofb Source: https://gostcrypto.readthedocs.io/en/latest/_sources/api/gostcipher/gostcipher.rst.txt Setup example for the OFB mode encryption class. ```python import gostcrypto key = bytearray([ 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10, 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, ]) init_vect = bytearray([ 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xce, 0xf0, 0xa1, 0xb2, 0xc3, 0xd4, 0xe5, 0xf0, 0x01, 0x12, 0x23, 0x34, 0x45, 0x56, 0x67, 0x78, 0x89, 0x90, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, ]) ``` -------------------------------- ### Example of use Source: https://gostcrypto.readthedocs.io/en/latest/api/gostpbkdf/gostpbkdf.html A complete example showing initialization and key derivation. ```python import gostcrypto password = b'password' salt = b'salt' pbkdf_obj = gostcrypto.gostpbkdf.new(password, salt=salt, counter=4096) pbkdf_result = pbkdf_obj.derive(32) ``` -------------------------------- ### Hexdigest Initialization Source: https://gostcrypto.readthedocs.io/en/latest/_sources/api/gostcipher/gostcipher.rst.txt Example showing the setup for calculating a hexadecimal MAC digest. ```python import gostcrypto key = bytearray([ 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10, 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, ]) ``` -------------------------------- ### Install goscrypto package Source: https://gostcrypto.readthedocs.io/en/latest/_sources/intro.rst.txt Use pip to install the library. ```bash $ pip install gostcrypto ``` -------------------------------- ### Digest method stub Source: https://gostcrypto.readthedocs.io/en/latest/_sources/api/gosthash/gosthash.rst.txt Example showing the import required for using the digest method. ```python import gostcrypto ``` -------------------------------- ### Generate MAC with Kuznechik Source: https://gostcrypto.readthedocs.io/en/latest/_sources/api/gostcipher/gostcipher.rst.txt Example of creating a message authentication code using the Kuznechik algorithm in MAC mode. ```python plain_text = = bytearray([ 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x00, 0xff, 0xee, 0xdd, 0xcc, 0xbb, 0xaa, 0x99, 0x88, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xee, 0xff, 0x0a, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xee, 0xff, 0x0a, 0x00, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xee, 0xff, 0x0a, 0x00, 0x11, ]) cipher_obj = gostcrypto.gostcipher.new('kuznechik', key, gostcrypto.gostcipher.MODE_MAC) cipher_obj.update(plain_text) mac_result = cipher_obj.hexdigest(8) ``` -------------------------------- ### Access hash object attributes Source: https://gostcrypto.readthedocs.io/en/latest/api/gosthash/gosthash.html Examples of accessing digest_size, block_size, and name attributes. ```python import gostcrypto hash_obj = gostcrypto.gosthash.new('streebog256') hash_obj_digest_size = hash_obj.digest_size ``` ```python import gostcrypto hash_obj = gostcrypto.gosthash.new('streebog256') hash_obj_block_size = hash_obj.block_size ``` ```python import gostcrypto hash_obj = gostcrypto.gosthash.new('streebog256') hash_obj_name = hash_obj.name ``` -------------------------------- ### Define twisted Edwards curve parameters Source: https://gostcrypto.readthedocs.io/en/latest/api/gostsignature/gostsignature.html Example configuration for setting an elliptic curve as a twisted Edwards curve using the required dictionary structure. ```python 'id-gost-3410-2012-256-twisted-Edwards-param': dict( p=bytearray_to_int(bytearray([ 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfd, 0x97 ])), e=0x01, d=bytearray_to_int(bytearray([ 0x06, 0x05, 0xf6, 0xb7, 0xc1, 0x83, 0xfa, 0x81, 0x57, 0x8b, 0xc3, 0x9c, 0xfa, 0xd5, 0x18, 0x13, 0x2b, 0x9d, 0xf6, 0x28, 0x97, 0x00, 0x9a, 0xf7, 0xe5, 0x22, 0xc3, 0x2d, 0x6d, 0xc7, 0xbf, 0xfb ])), m=bytearray_to_int(bytearray([ 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0x63, 0x37, 0x7f, 0x21, 0xed, 0x98, 0xd7, 0x04, 0x56, 0xbd, 0x55, 0xb0, 0xd8, 0x31, 0x9c ])), q=bytearray_to_int(bytearray([ 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0xd8, 0xcd, 0xdf, 0xc8, 0x7b, 0x66, 0x35, 0xc1, 0x15, 0xaf, 0x55, 0x6c, 0x36, 0x0c, 0x67 ])), u=0x0d, v=bytearray_to_int(bytearray([ 0x60, 0xca, 0x1e, 0x32, 0xaa, 0x47, 0x5b, 0x34, 0x84, 0x88, 0xc3, 0x8f, 0xab, 0x07, 0x64, 0x9c, 0xe7, 0xef, 0x8d, 0xbe, 0x87, 0xf2, 0x2e, 0x81, 0xf9, 0x2b, 0x25, 0x92, 0xdb, 0xa3, 0x00, 0xe7 ])), ) ``` -------------------------------- ### Define GOST 34.10-2012 Curve Parameters Source: https://gostcrypto.readthedocs.io/en/latest/_sources/api/gostsignature/gostsignature.rst.txt Example of defining elliptic curve parameters in canonical form for GOST 34.10-2012. ```python 'id-tc26-gost-3410-2012-256-paramSetB': dict( p=bytearray_to_int(bytearray([ 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfd, 0x97 ])), a=bytearray_to_int(bytearray([ 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfd, 0x94 ])), b=0xa6, m=bytearray_to_int(bytearray([ 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x6c, 0x61, 0x10, 0x70, 0x99, 0x5a, 0xd1, 0x00, 0x45, 0x84, 0x1b, 0x09, 0xb7, 0x61, 0xb8, 0x93 ])), q=bytearray_to_int(bytearray([ 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x6c, 0x61, 0x10, 0x70, 0x99, 0x5a, 0xd1, 0x00, 0x45, 0x84, 0x1b, 0x09, 0xb7, 0x61, 0xb8, 0x93 ])), x=0x01, y=bytearray_to_int(bytearray([ 0x00, 0x8d, 0x91, 0xe4, 0x71, 0xe0, 0x98, 0x9c, 0xda, 0x27, 0xdf, 0x50, 0x5a, 0x45, 0x3f, 0x2b, 0x76, 0x35, 0x29, 0x4f, 0x2d, 0xdf, 0x23, 0xe3, 0xb1, 0x22, 0xac, 0xc9, 0x9c, 0x9e, 0x9f, 0x1e, 0x14 ])) ) ``` -------------------------------- ### Initialize and use GOSTRandom Source: https://gostcrypto.readthedocs.io/en/latest/api/gostrandom/gostrandom.html Demonstrates creating a new random object with a 32-byte key and clearing it after use. ```python import gostcrypto rand_k = bytearray([ 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10, 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, ]) random_obj = gostcrypto.gostrandom.new(32, rand_k=rand_k, size_s=gostcrypto.gostrandom.SIZE_S_256) random_result = random_obj.random() random_obj.clear() ``` -------------------------------- ### Initialize PBKDF object Source: https://gostcrypto.readthedocs.io/en/latest/_sources/api/gostpbkdf/gostpbkdf.rst.txt Creates a new PBKDF object using a password, optional salt, and iteration counter. ```python import gostcrypto password = b'password' salt = b'salt' pbkdf_obj = gostcrypto.gostpbkdf.new(password, salt=salt, counter=2000) ``` -------------------------------- ### Get HMAC algorithm name identifier Source: https://gostcrypto.readthedocs.io/en/latest/_sources/intro.rst.txt Retrieves the OID name from an HMAC object. ```python import gostcrypto key = bytearray.fromhex('000102030405060708090a0b0c0d0e0f1011121315161718191a1b1c1d1e1f') hmac_obj = gostcrypto.gosthmac.new('HMAC_GOSTR3411_2012_256', key) oid_name = hmac_obj.oid.name ``` -------------------------------- ### Create a new PBKDF object Source: https://gostcrypto.readthedocs.io/en/latest/api/gostpbkdf/gostpbkdf.html Initializes a new key derivation object using the gostcrypto.gostpbkdf.new function. ```python import gostcrypto password = b'password' salt = b'salt' pbkdf_obj = gostcrypto.gostpbkdf.new(password, salt=salt, counter=2000) ``` -------------------------------- ### Generating a public key Source: https://gostcrypto.readthedocs.io/en/latest/_sources/api/gostsignature/gostsignature.rst.txt Shows how to derive a public key from a private key using the signature object. ```python private_key = bytearray([ 0x7a, 0x92, 0x9a, 0xde, 0x78, 0x9b, 0xb9, 0xbe, 0x10, 0xed, 0x35, 0x9d, 0xd3, 0x9a, 0x72, 0xc1, 0x1b, 0x60, 0x96, 0x1f, 0x49, 0x39, 0x7e, 0xee, 0x1d, 0x19, 0xce, 0x98, 0x91, 0xec, 0x3b, 0x28, ]) sign_obj = gostcrypto.gostsignature.new(gostcrypto.gostsignature.MODE_256, gostcrypto.gostsignature.CURVES_R_1323565_1_024_2019['id-tc26-gost-3410-2012-256-paramSetB']) public_key = sign_obj.public_key_generate(private_key) ``` -------------------------------- ### Get hashing algorithm identifier Source: https://gostcrypto.readthedocs.io/en/latest/_sources/intro.rst.txt Retrieve the name of the OID associated with the hash object. ```python import gostcrypto hash_obj = gostcrypto.gosthash.new('streebog512') oid_name = hash_obj.oid.name ``` -------------------------------- ### MAC Calculation with Initial Data Source: https://gostcrypto.readthedocs.io/en/latest/_sources/api/gostcipher/gostcipher.rst.txt Demonstrates initializing a MAC object with the first message fragment passed directly to the new() function. ```python import gostcrypto key = bytearray([ 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10, 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, ]) mac_obj = gostcrypto.gostcipher.new('kuznechik', key, gostcrypto.gostcipher.MODE_MAC, data=b'first part message) mac_obj.update(b'second part message') ``` -------------------------------- ### Get signature mode object name Source: https://gostcrypto.readthedocs.io/en/latest/_sources/intro.rst.txt Retrieves the OID name from a newly created signature object. ```python import gostcrypto sign_obj = gostcrypto.gostsignature.new(gostcrypto.gostsignature.MODE_256, gostcrypto.gostsignature.CURVES_R_1323565_1_024_2019['id-tc26-gost-3410-2012-256-paramSetB']) oid_name = sign_obj.oid.name ``` -------------------------------- ### Initialize hash object with data Source: https://gostcrypto.readthedocs.io/en/latest/_sources/api/gosthash/gosthash.rst.txt Create a new hash object by passing the algorithm name and initial data directly to the new() function. ```python import gostcrypto hash_string = u'Се ветри, Стрибожи внуци, веютъ Ñ Ð¼Ð¾Ñ€Ñ Ñ Ñ‚Ñ€ÐµÐ»Ð°Ð¼Ð¸ на Ñ…Ñ€Ð°Ð±Ñ€Ñ‹Ñ Ð¿Ð»ÑŠÐºÑ‹ Игоревы'.encode('cp1251') hash_obj = gostrypto.gosthash.new('streebog256', data=hash_string) ``` -------------------------------- ### gostpbkdf.new Source: https://gostcrypto.readthedocs.io/en/latest/_sources/intro.rst.txt Creates a new PBKDF object for key derivation. ```APIDOC ## gostpbkdf.new ### Description Initializes a PBKDF object to derive keys from a password and salt. ### Usage ```python import gostcrypto pbkdf_obj = gostcrypto.gostpbkdf.new(password, salt=salt, counter=4096) pbkdf_result = pbkdf_obj.derive(32) ``` ``` -------------------------------- ### Retrieve OID Digits Source: https://gostcrypto.readthedocs.io/en/latest/_sources/api/gostoid/gostoid.rst.txt Access the digit attribute to get the OID as a tuple of integers. Throws GOSTOIDError if the OID is invalid. ```python oid_obj = ObjectIdentifier('1.2.643.7.1.1.2.3') print(oid_obj.digit) ``` -------------------------------- ### Define GOST Curve as Twisted Edwards Source: https://gostcrypto.readthedocs.io/en/latest/_sources/api/gostsignature/gostsignature.rst.txt Example of setting an elliptic curve simultaneously in canonical form and as twisted Edwards curves. ```python 'id-tc26-gost-3410-2012-256-paramSetA': dict( p=bytearray_to_int(bytearray([ 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfd, 0x97 ])), a=bytearray_to_int(bytearray([ 0x00, 0xc2, 0x17, 0x3f, 0x15, 0x13, 0x98, 0x16, 0x73, 0xaf, 0x48, 0x92, 0xc2, 0x30, 0x35, 0xa2, 0x7c, 0xe2, 0x5e, 0x20, 0x13, 0xbf, 0x95, 0xaa, 0x33, 0xb2, 0x2c, 0x65, 0x6f, 0x27, 0x7e, 0x73, 0x35 ])), b=bytearray_to_int(bytearray([ 0x29, 0x5f, 0x9b, 0xae, 0x74, 0x28, 0xed, 0x9c, 0xcc, 0x20, 0xe7, 0xc3, 0x59, 0xa9, 0xd4, 0x1a, 0x22, 0xfc, 0xcd, 0x91, 0x08, 0xe1, 0x7b, 0xf7, 0xba, 0x93, 0x37, 0xa6, 0xf8, 0xae, 0x95, 0x13 ])), e=0x01, d=bytearray_to_int(bytearray([ 0x06, 0x05, 0xf6, 0xb7, 0xc1, 0x83, 0xfa, 0x81, 0x57, 0x8b, 0xc3, 0x9c, 0xfa, 0xd5, 0x18, 0x13, 0x2b, 0x9d, 0xf6, 0x28, 0x97, 0x00, 0x9a, 0xf7, 0xe5, 0x22, 0xc3, 0x2d, 0x6d, 0xc7, 0xbf, 0xfb ])), m=bytearray_to_int(bytearray([ 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0x63, 0x37, 0x7f, 0x21, 0xed, 0x98, 0xd7, 0x04, 0x56, 0xbd, 0x55, 0xb0, 0xd8, 0x31, 0x9c ])), q=bytearray_to_int(bytearray([ 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0xd8, 0xcd, 0xdf, 0xc8, 0x7b, 0x66, 0x35, 0xc1, 0x15, 0xaf, 0x55, 0x6c, 0x36, 0x0c, 0x67 ])), x=bytearray_to_int(bytearray([ 0x00, 0x91, 0xe3, 0x84, 0x43, 0xa5, 0xe8, 0x2c, 0x0d, 0x88, 0x09, 0x23, 0x42, 0x57, 0x12, 0xb2, 0xbb, 0x65, 0x8b, 0x91, 0x96, 0x93, 0x2e, 0x02, 0xc7, 0x8b, 0x25, 0x82, 0xfe, 0x74, 0x2d, 0xaa, 0x28 ])), y=bytearray_to_int(bytearray([ 0x32, 0x87, 0x94, 0x23, 0xab, 0x1a, 0x03, 0x75, 0x89, 0x57, 0x86, 0xc4, 0xbb, 0x46, 0xe9, 0x56, 0x5f, 0xde, 0x0b, 0x53, 0x44, 0x76, 0x67, 0x40, 0xaf, 0x26, 0x8a, 0xdb, 0x32, 0x32, 0x2e, 0x5c ])), u=0x0d, v=bytearray_to_int(bytearray([ 0x60, 0xca, 0x1e, 0x32, 0xaa, 0x47, 0x5b, 0x34, 0x84, 0x88, 0xc3, 0x8f, 0xab, 0x07, 0x64, 0x9c, 0xe7, 0xef, 0x8d, 0xbe, 0x87, 0xf2, 0x2e, 0x81, 0xf9, 0x2b, 0x25, 0x92, 0xdb, 0xa3, 0x00, 0xe7 ])), ) ``` -------------------------------- ### Initialize hash and update later Source: https://gostcrypto.readthedocs.io/en/latest/api/gosthash/gosthash.html Initialize the hash object without data and provide the message fragment via the update method. ```python import gostcrypto hash_string = u'Се ветри, Стрибожи внуци, веютъ с моря стрелами на храбрыя плъкы Игоревы'.encode('cp1251') hash_obj = gostrypto.gosthash.new('streebog256') hash_obj.update(hash_string) ``` -------------------------------- ### Create a new pseudo-random sequence generator Source: https://gostcrypto.readthedocs.io/en/latest/api/gostrandom/gostrandom.html Initializes a generator using a specific seed and size. If random_k is omitted, os.urandom is used by default. ```python import gostcrypto random_k = bytearray([ 0xa8, 0xe2, 0xf9, 0x00, 0xdd, 0x4d, 0x7e, 0x24, 0x5f, 0x09, 0x75, 0x3d, 0x01, 0xe8, 0x75, 0xfc, 0x38, 0xf1, 0x4f, 0xf5, 0x25, 0x4c, 0x94, 0xea, 0xdb, 0x45, 0x1e, 0x4a, 0xb6, 0x03, 0xb1, 0x47, ]) random_obj = gostcrypto.gostrandom.new(64, random_k=random_k, size_s=gostcrypto.gostrandom.SIZE_S_256) ``` -------------------------------- ### Initialize HMAC with data Source: https://gostcrypto.readthedocs.io/en/latest/_sources/api/gosthmac/gosthmac.rst.txt Initialize an HMAC object by passing the algorithm name, key, and initial data directly to the new() function. ```python import gostcrypto key = bytearray.fromhex('000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f') hmac_string = bytearray.fromhex('0126bdb87800af214341456563780100') hmac_obj = gostrypto.gosthmac.new('HMAC_GOSTR3411_2012_256', key, data=hmac_string) ``` -------------------------------- ### Handle GOSTPBKDFError Source: https://gostcrypto.readthedocs.io/en/latest/_sources/api/gostpbkdf/gostpbkdf.rst.txt Demonstrates exception handling when performing key derivation. ```python import gostcrypto password = b'password' salt = b'salt' try: pbkdf_obj = gostcrypto.gostpbkdf.new(password, salt=salt, counter=2000) pbkdf_result = pbkdf_obj.hexderive(32) except gostcrypto.gostpbkdf.GOSTPBKDFError as err: print(err) else: print(pbkdf_result) ``` -------------------------------- ### Encrypting data with GOST34132015ofb Source: https://gostcrypto.readthedocs.io/en/latest/api/gostcipher/gostcipher.html Demonstrates initializing a cipher object with the Kuznechik algorithm in OFB mode and encrypting plaintext data. ```python import gostcrypto key = bytearray([ 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10, 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, ]) init_vect = bytearray([ 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xce, 0xf0, 0xa1, 0xb2, 0xc3, 0xd4, 0xe5, 0xf0, 0x01, 0x12, 0x23, 0x34, 0x45, 0x56, 0x67, 0x78, 0x89, 0x90, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, ]) plain_text = = bytearray([ 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x00, 0xff, 0xee, 0xdd, 0xcc, 0xbb, 0xaa, 0x99, 0x88, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xee, 0xff, 0x0a, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xee, 0xff, 0x0a, 0x00, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xee, 0xff, 0x0a, 0x00, 0x11, ]) cipher_obj = gostcrypto.gostcipher.new('kuznechik', key, gostcrypto.gostcipher.MODE_OFB, init_vect=init_vect) cipher_text = cipher_obj.encrypt(plain_text) ``` -------------------------------- ### Handle PBKDF errors Source: https://gostcrypto.readthedocs.io/en/latest/api/gostpbkdf/gostpbkdf.html Demonstrates catching GOSTPBKDFError exceptions during key derivation. ```python import gostcrypto password = b'password' salt = b'salt' try: pbkdf_obj = gostcrypto.gostpbkdf.new(password, salt=salt, counter=2000) pbkdf_result = pbkdf_obj.hexderive(32) except gostcrypto.gostpbkdf.GOSTPBKDFError as err: print(err) else: print(pbkdf_result) ``` -------------------------------- ### Encrypting data with GOST 34.13-2015 CBC Source: https://gostcrypto.readthedocs.io/en/latest/_sources/api/gostcipher/gostcipher.rst.txt Demonstrates initializing a cipher object with a key and initialization vector to encrypt plaintext. ```python import gostcrypto key = bytearray([ 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10, 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, ]) init_vect = bytearray([ 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xce, 0xf0, 0xa1, 0xb2, 0xc3, 0xd4, 0xe5, 0xf0, 0x01, 0x12, 0x23, 0x34, 0x45, 0x56, 0x67, 0x78, 0x89, 0x90, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, ]) plain_text = = bytearray([ 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x00, 0xff, 0xee, 0xdd, 0xcc, 0xbb, 0xaa, 0x99, 0x88, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xee, 0xff, 0x0a, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xee, 0xff, 0x0a, 0x00, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xee, 0xff, 0x0a, 0x00, 0x11, ]) cipher_obj = gostcrypto.gostcipher.new('kuznechik', key, gostcrypto.gostcipher.MODE_CBC, init_vect=init_vect, pad_mode=gostcrypto.gostcipher.PAD_MODE_2) cipher_text = cipher_obj.encrypt(plain_text) ``` -------------------------------- ### Encrypting data with GOST34132015ecb Source: https://gostcrypto.readthedocs.io/en/latest/_sources/api/gostcipher/gostcipher.rst.txt Demonstrates initializing a cipher object with the Kuznechik algorithm in ECB mode and performing encryption. ```python import gostcrypto key = bytearray([ 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10, 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, ]) plain_text = = bytearray([ 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x00, 0xff, 0xee, 0xdd, 0xcc, 0xbb, 0xaa, 0x99, 0x88, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xee, 0xff, 0x0a, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xee, 0xff, 0x0a, 0x00, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xee, 0xff, 0x0a, 0x00, 0x11, ]) cipher_obj = gostcrypto.gostcipher.new('kuznechik', key, gostcrypto.gostcipher.MODE_ECB, pad_mode=gostcrypto.gostcipher.PAD_MODE_2) cipher_text = cipher_obj.encrypt(plain_text) ``` -------------------------------- ### gostrandom.new Source: https://gostcrypto.readthedocs.io/en/latest/_sources/intro.rst.txt Initializes a random number generator object. ```APIDOC ## gostrandom.new ### Description Creates a new random object for generating random sequences. ### Usage ```python import gostcrypto random_obj = gostcrypto.gostrandom.new(32, rand_k=rand_k, size_s=gostcrypto.gostrandom.SIZE_S_256) random_result = random_obj.random() random_obj.clear() ``` ``` -------------------------------- ### Initialize hash with data Source: https://gostcrypto.readthedocs.io/en/latest/api/gosthash/gosthash.html Pass the initial message fragment directly to the new function using the data parameter. ```python import gostcrypto hash_string = u'Се ветри, Стрибожи внуци, веютъ с моря стрелами на храбрыя плъкы Игоревы'.encode('cp1251') hash_obj = gostrypto.gosthash.new('streebog256', data=hash_string) ``` -------------------------------- ### gostcrypto.gostsignature.new Source: https://gostcrypto.readthedocs.io/en/latest/_sources/api/gostsignature/gostsignature.rst.txt Initializes a new signature object with a specified mode and curve parameters. ```APIDOC ## gostcrypto.gostsignature.new(mode, curve) ### Description Creates a new signature object instance configured with the provided mode and elliptic curve parameters. ### Parameters - **mode** (int) - The signature mode (e.g., gostcrypto.gostsignature.MODE_256). - **curve** (dict) - The elliptic curve parameters (e.g., from gostcrypto.gostsignature.CURVES_R_1323565_1_024_2019). ``` -------------------------------- ### Initialize a new cipher object Source: https://gostcrypto.readthedocs.io/en/latest/_sources/api/gostcipher/gostcipher.rst.txt Creates a new cipher instance using the specified algorithm, key, and mode. Padding mode can be configured for ECB and CBC modes. ```python import gostcrypto key = bytearray([ 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10, 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, ]) cipher_obj = gostcrypto.gostcipher.new('kuznechik', key, gostcrypto.gostcipher.MODE_ECB, pad_mode=PAD_MODE_2) ``` -------------------------------- ### MAC Calculation via Update Method Source: https://gostcrypto.readthedocs.io/en/latest/_sources/api/gostcipher/gostcipher.rst.txt Demonstrates passing all message fragments sequentially to the update() method after initializing the MAC object. ```python import gostcrypto key = bytearray([ 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10, 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, ]) mac_obj = gostcrypto.gostcipher.new('kuznechik', key, gostcrypto.gostcipher.MODE_MAC) mac_obj.update(b'first part message') mac_obj.update(b'second part message') ``` -------------------------------- ### Initialize a new signature object Source: https://gostcrypto.readthedocs.io/en/latest/_sources/api/gostsignature/gostsignature.rst.txt Creates a new signature object using a specified mode and elliptic curve parameters. ```python import gostcrypto sign_obj = gostcrypto.gostsignature.new(gostcrypto.gostsignature.MODE_256, gostcrypto.gostsignature.CURVES_R_1323565_1_024_2019['id-tc26-gost-3410-2012-256-paramSetB']) ``` -------------------------------- ### new(rand_size, **kwargs) Source: https://gostcrypto.readthedocs.io/en/latest/api/gostrandom/gostrandom.html Creates and returns a new pseudo-random sequence generation object. ```APIDOC ## new(rand_size, **kwargs) ### Description Creates a new pseudo-random sequence generation object and returns it as an instance of the R132356510062017 class. ### Parameters #### Arguments - **rand_size** (int) - Required - Size of the generated random variable in bytes. #### Keyword Arguments - **random_k** (bytearray) - Optional - Initial filling (seed). If not provided, os.urandom is used. - **size_s** (int) - Optional - Size of the initial filling in bytes. Defaults to SIZE_S_384. ### Exceptions - **GOSTRandomError** - Raised if the provided seed value size is invalid. ```