<commit_before>package io.subutai.core.security.broker;


import org.bouncycastle.openpgp.PGPPublicKey;
import org.bouncycastle.openpgp.PGPSecretKey;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.subutai.common.security.crypto.pgp.PgpUtil;
import io.subutai.core.broker.api.ByteMessagePreProcessor;
import io.subutai.core.broker.api.Topic;


/**
 * This class decrypts incoming messages
 */
public class MessageDecryptor implements ByteMessagePreProcessor
{
    private static final Logger LOG = LoggerFactory.getLogger( MessageDecryptor.class.getName() );


    @Override
    public byte[] process( final String topic, final byte[] message )
    {
        LOG.info( String.format( "INCOMING:%s", new String( message ) ) );

        try
        {
            //process incoming heartbeats and responses
            if ( Topic.RESPONSE_TOPIC.name().equalsIgnoreCase( topic ) || Topic.HEARTBEAT_TOPIC.name().equalsIgnoreCase(
                    topic ) )
            {
                //imitate obtaining MH private key and sender host pub key
                PGPSecretKey signingKey =
                        PgpUtil.findSecretKeyById( MessageEncryptor.findFile( MessageEncryptor.SECRET_KEYRING ),
                                MessageEncryptor.SECRET_KEY_ID );
                PGPPublicKey encryptingKey =
                        PgpUtil.findPublicKeyById( MessageEncryptor.findFile( MessageEncryptor.PUBLIC_KEYRING ),
                                MessageEncryptor.PUBLIC_KEY_ID );

                //todo obtain MH private key
                PGPSecretKey peerKeyForDecrypting = PgpUtil.findSecretKeyByFingerprint(
                        MessageEncryptor.findFile( MessageEncryptor.SECRET_KEYRING ),
                        PgpUtil.BytesToHex( encryptingKey.getFingerprint() ) );
                //todo obtain target host pub key
                PGPPublicKey hostKeyForVerifying = signingKey.getPublicKey();

                PgpUtil.ContentAndSignatures contentAndSignatures =
                        PgpUtil.decryptAndReturnSignatures( message, peerKeyForDecrypting,
                                MessageEncryptor.SECRET_PWD );

                if ( PgpUtil.verifySignature( contentAndSignatures, hostKeyForVerifying ) )
                {
                    LOG.info( String.format( "Verification succeeded%nDecrypted Message: %s",
                            new String( contentAndSignatures.getDecryptedContent() ) ) );
                }
                else
                {
                    LOG.info( String.format( "Verification failed%nDecrypted Message: %s",
                            new String( contentAndSignatures.getDecryptedContent() ) ) );
                }

                return PgpUtil.decryptAndVerify( message, peerKeyForDecrypting, MessageEncryptor.SECRET_PWD,
                        hostKeyForVerifying );
            }
        }
        catch ( Exception e )
        {
            LOG.error( "Error in process", e );
        }

        return message;
    }
}
<commit_msg>Updated comments SS-3236<commit_after>package io.subutai.core.security.broker;


import org.bouncycastle.openpgp.PGPPublicKey;
import org.bouncycastle.openpgp.PGPSecretKey;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.subutai.common.security.crypto.pgp.PgpUtil;
import io.subutai.core.broker.api.ByteMessagePreProcessor;
import io.subutai.core.broker.api.Topic;


/**
 * This class decrypts incoming messages
 */
public class MessageDecryptor implements ByteMessagePreProcessor
{
    private static final Logger LOG = LoggerFactory.getLogger( MessageDecryptor.class.getName() );


    @Override
    public byte[] process( final String topic, final byte[] message )
    {
        LOG.info( String.format( "INCOMING:%s", new String( message ) ) );

        try
        {
            //process incoming heartbeats and responses
            if ( Topic.RESPONSE_TOPIC.name().equalsIgnoreCase( topic ) || Topic.HEARTBEAT_TOPIC.name().equalsIgnoreCase(
                    topic ) )
            {
                //imitate obtaining MH private key and sender host pub key
                PGPSecretKey signingKey =
                        PgpUtil.findSecretKeyById( MessageEncryptor.findFile( MessageEncryptor.SECRET_KEYRING ),
                                MessageEncryptor.SECRET_KEY_ID );
                PGPPublicKey encryptingKey =
                        PgpUtil.findPublicKeyById( MessageEncryptor.findFile( MessageEncryptor.PUBLIC_KEYRING ),
                                MessageEncryptor.PUBLIC_KEY_ID );

                //todo obtain MH private key
                PGPSecretKey peerKeyForDecrypting = PgpUtil.findSecretKeyByFingerprint(
                        MessageEncryptor.findFile( MessageEncryptor.SECRET_KEYRING ),
                        PgpUtil.BytesToHex( encryptingKey.getFingerprint() ) );

                PgpUtil.ContentAndSignatures contentAndSignatures =
                        PgpUtil.decryptAndReturnSignatures( message, peerKeyForDecrypting,
                                MessageEncryptor.SECRET_PWD );

                //todo obtain target host pub key by id from content
                PGPPublicKey hostKeyForVerifying = signingKey.getPublicKey();

                if ( PgpUtil.verifySignature( contentAndSignatures, hostKeyForVerifying ) )
                {
                    LOG.info( String.format( "Verification succeeded%nDecrypted Message: %s",
                            new String( contentAndSignatures.getDecryptedContent() ) ) );
                }
                else
                {
                    LOG.info( String.format( "Verification failed%nDecrypted Message: %s",
                            new String( contentAndSignatures.getDecryptedContent() ) ) );
                }

                return PgpUtil.decryptAndVerify( message, peerKeyForDecrypting, MessageEncryptor.SECRET_PWD,
                        hostKeyForVerifying );
            }
        }
        catch ( Exception e )
        {
            LOG.error( "Error in process", e );
        }

        return message;
    }
}

{'fraction_non_alphanumeric': 0.07553662133377027, 'fraction_numerical': 0.001310830738980829, 'mean_word_length': 1.936026936026936, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7397413', 'n_tokens_mistral': 1699, 'n_tokens_neox': 1605, 'n_words': 302}