beginner – Java refactoring – reduces cognitive complexity by extracting private methods

This is the code, written by our development team, and SonarQube tells me that the Cognitive competence It is too high: 21, and the current metric limit (default, ready to use) is 15. I tried to reduce complexity and would love to receive some comments.

Original code

/**
 * Gets the encrypted payload of this document,
 * or returns {@code null} if this document isn't an unencrypted wrapper document.
 *
 * @return encrypted payload of this document.
 */
public PdfEncryptedPayloadDocument getEncryptedPayloadDocument2() {
    if (getReader() != null && getReader().isEncrypted()) {
        return null;
    }
    PdfCollection collection = getCatalog().getCollection();
    if (collection != null && collection.isViewHidden()) {
        PdfString documentName = collection.getInitialDocument();
        PdfNameTree embeddedFiles = getCatalog().getNameTree(PdfName.EmbeddedFiles);
        String documentNameUnicode = documentName.toUnicodeString();
        PdfObject fileSpecObject = embeddedFiles.getNames().get(documentNameUnicode);
        if (fileSpecObject != null && fileSpecObject.isDictionary()) {
            try {
                PdfFileSpec fileSpec = PdfEncryptedPayloadFileSpecFactory.wrap((PdfDictionary) fileSpecObject);
                if (fileSpec != null) {
                    PdfDictionary embeddedDictionary = ((PdfDictionary) fileSpec.getPdfObject()).getAsDictionary(PdfName.EF);
                    PdfStream stream = embeddedDictionary.getAsStream(PdfName.UF);
                    if (stream == null) {
                        stream = embeddedDictionary.getAsStream(PdfName.F);
                    }
                    if (stream != null) {
                        return new PdfEncryptedPayloadDocument(stream, fileSpec, documentNameUnicode);
                    }
                }
            } catch (PdfException e) {
                LoggerFactory.getLogger(getClass()).error(e.getMessage());
            }
        }
    }
    return null;
}

Refactoring Code

/**
 * Gets the encrypted payload of this document,
 * or returns {@code null} if this document isn't an unencrypted wrapper document.
 *
 * @return encrypted payload of this document.
 */
public PdfEncryptedPayloadDocument getEncryptedPayloadDocument2() {
    if (readerIsEncrypted(getReader())) {
        return null;
    }
    PdfCollection collection = getCatalog().getCollection();
    if (collectionIsNotViewHidden(collection)) {
        return null;
    }
    PdfString documentName = collection.getInitialDocument();
    PdfNameTree embeddedFiles = getCatalog().getNameTree(PdfName.EmbeddedFiles);
    String documentNameUnicode = documentName.toUnicodeString();
    PdfObject fileSpecObject = embeddedFiles.getNames().get(documentNameUnicode);
    if (fileSpecObjectIsNoDictionary(fileSpecObject)) {
        return null;
    }
    try {
        PdfFileSpec fileSpec = PdfEncryptedPayloadFileSpecFactory.wrap((PdfDictionary) fileSpecObject);
        if (fileSpec != null) {
            PdfDictionary embeddedDictionary = ((PdfDictionary) fileSpec.getPdfObject()).getAsDictionary(PdfName.EF);
            PdfStream stream = getPdfStreamUf(embeddedDictionary);
            if (stream != null) {
                return new PdfEncryptedPayloadDocument(stream, fileSpec, documentNameUnicode);
            }
        }
    } catch (PdfException e) {
        LoggerFactory.getLogger(getClass()).error(e.getMessage());
    }
    return null;
}

private boolean readerIsEncrypted(PdfReader reader) {
    return reader != null && reader.isEncrypted();
}

private boolean collectionIsNotViewHidden(PdfCollection collection) {
    return collection == null || !collection.isViewHidden();
}

private boolean fileSpecObjectIsNoDictionary(PdfObject fileSpecObject) {
    return fileSpecObject == null || !fileSpecObject.isDictionary();
}

private PdfStream getPdfStreamUf(PdfDictionary dictionary) {
    PdfStream stream = dictionary.getAsStream(PdfName.UF);
    if (stream == null) {
        stream = dictionary.getAsStream(PdfName.F);
    }
    return stream;
}

Summary of Changes:

  • invest the second yes, to have the fund return null earlier.
  • invest the third yes, also to have a return null earlier.

    These two changes add an exit point to the method and are used as protection clauses, something I found on the Martin Fowler refactoring.com website.

  • extract to a private method: obtain the dictionary as a sequence and verify if it is null and then get it another way.
  • extract all conditions composed of private methods.

What I, from the point of view as a tester, see as an advantage in my refactoring code, is that I can see the private methods extracted in my IDE, see that they are only partially covered by unit tests (for example, 2 of 4 conditions ), and based on that information (white box test), design a new unit test that accesses the public API to cover the missing conditions.

I am also considering making private methods static, because they only act at the entrance and do not use anything from the class instance. But I am neutral at that point, it could be anyway.

The concern of the developer who reported the high cognitive complexity is that dividing the method into smaller methods, actually reduce readability Of code

I would really appreciate some comments based on facts about my refactoring of a wider developer community.

About me

I am a Stack Exchange veteran (see profile), but this is my first question on the Code Review site. I am a tester, not a developer, and it has been more than a decade since I last wrote a Java code. So please be kind to me. 🙂

How secure is NAT to keep my network private?

To expand How can a web page scan my local internal network from the Internet? How secure is my private network against possible threats such as DoS attacks, phishing attacks, disclosure of information about my private network services, etc. Have there been recent attacks?

I recently read (https://www.sans.org/reading-room/whitepapers/networkdevs/easy-steps-cisco-extended-access-list-231) that without an ACL it establishes the incoming ICMP echo request (ICMP type 8), ICMP exceeded in time (ICMP type 11) could reveal its intranet structure. That's right?

Provide 50 PBN private blog network links for $ 25

Provide 50 PBN (private blog network) backlinks

We will provide follow-up links to the homepage DA PA TF CF from a real website with a good theme, indexed and without a trace. We always update our PBN and link building method to ensure that it always works with the latest changes in Google's algorithm.

features:

Permanent links in high quality blog posts WITH unique content

Average DA 20 – 30+ and PA 20 – 30+ (all domains)

The medium PBN links of TF (Trust Flow) 20 – 30+ and CF 20 – 30+ offer great authority from Google's perspective and a lot of confidence. This is what really matters these days when it comes to Google. Quality links matter today and I provide the same service.

  • I will give you a full report!
  • Permanent rates for one time only!
  • No spam
  • OBL max 25
  • The post will be automatically indexed on Google without the use of the indexer
  • 100% customer service
  • We will provide the item too.
  • The link will be active for 1 year (no website downtime)
  • Unique article and more than 500 readable words
  • All domains are well indexed.
  • Improvement in the fastest ranking.
  • Detailed Report
  • All domains have exclusive protection from WHO WHO IS.

Order now
Or email us for more inquiries

. (tagsToTranslate) pbn (t) backlink (t) google (t) Homepage (t) seo

centos – don't let me enter the server with the private key

I launch my command from cmd:

start putty -load 192.168.1.200 -i C:UsersAdministradorDesktopid_rsa.ppk"

I can think of entering a private password in putty:

Using username "root".
Authenticating with public key "imported-openssh-key"
Passphrase for key "imported-openssh-key":

How can I do it to be able to enter alone and not enter the password?

python: pre-confirmation link to warn when a function or variable should be private

Looking to install a pre-confirmation link that warns when Python methods or variables should be underlined (marked as private), when they are only used from the module.

I guess this would require some static analysis skills. Could mypy / pytype do it somehow? Is there an existing tool?

How to unlock the bitcoin private key

People with 86 million dollars generally do not need to ask this.

People who have been duped by scammers are the type of people who generally ask this.

That looks like a blockchain wallet.


How to unlock the bitcoin private key

If you have forgotten a password necessary to "unlock" your wallet, you have lost all your bitcoins and will never recover them or spend many months and tens of thousands of dollars in the attempt.

If you have problems with your blockchain.com account, you should contact blockchain.com. They are a Luxembourg company. See https://support.blockchain.com/hc/en-us: if you have been cheated, they cannot help you.


I need help on how to get the private key

You can follow the guide on the support websites of blockchain.com

Exporting the private key. The example given is for Ethereum, but you must choose "Bitcoin" instead.

If your wallet does not have a private key for the bitcoin address that contains those 86 million dollars in Bitcoin, it means that it has a clock-only address imported into your wallet.

If someone tells you to pay him to release the private key, it means he is a scammer who tricks him into giving him more money.


Backups

If you were not deceived but did not make a backup copy of your wallet and a written backup copy of your password, private key or a written backup copy of your recovery phrase, and lost or forgot it, you have been very negligent.

Recovery can be very difficult, slow, expensive or most likely impossible.

Scammers scams

If a friend / miner / online consultant set this up for you, it means you are a victim of a scam.

If so, it is quite possible that the money you are seeing is money in the wallet of an innocent third party whose bitcoin address has been imported by a scammer in your wallet as a "watch only" address. In which case it was never their money and all the money they sent to the scammer was pocketed by them completely separately.


Helpful Links

Some other questions whose answers might Be useful to understand your situation.

  • Surveillance Only Addresses
  • Private keys
  • "Unlock private key"

signature: source and handling (incorrect) of the private key for a basic tx

I am looking to "get down to work" with the signature, however, when I adapt Runeks' comprehensive response at https://bitcoin.stackexchange.com/a/5241/89798 my code invariably fails to report that I have used the private wrong key. Am I searching for the private key incorrectly or am I handling it wrong in some way? Where in my process below am I making mistakes?

I will begin by noting that the Runeks code, as it stands, is executed correctly, that I am using v0.13.2 bitcoind and bitcoin-cli to get the legacy transactions used in your response, and I am using regtest instead of mainnet.

My process:

  1. I use a script to reset the regtest network and generate 101 blocks, so I have only one coin base that I can use:
CURDIR=`pwd`
cd /home/bitcoin14/.bitcoin
/usr/local/bin/bitcoin-cli -conf=/home/bitcoin14/.bitcoin/regtest.conf -regtest stop
sleep 1
rm -rf /home/bitcoin14/.bitcoin/regtest && /usr/local/bin/bitcoind -conf=/home/bitcoin14/.bitcoin/regtest.conf -regtest -daemon
sleep 2
/usr/local/bin/bitcoin-cli -conf=/home/bitcoin14/.bitcoin/regtest.conf -regtest generate 101 >/dev/null
cd $CURDIR
  1. I send the coins to a new address so I don't use that coin base transaction
$ /usr/local/bin/bitcoin-cli -conf=/home/bitcoin14/.bitcoin/regtest.conf getnewaddress
mk5U6xEDnTmfNrHcCXkdKrXziYH1YMygVw
$ /usr/local/bin/bitcoin-cli -conf=/home/bitcoin14/.bitcoin/regtest.conf sendtoaddress mk5U6xEDnTmfNrHcCXkdKrXziYH1YMygVw 49.99
675eac8d9aebe4f84aa2bb18a9f02304d11cb8937ca103b6ccf00fdf0de69727
  1. I get the hexadecimal and the vote of the previous transaction that contains the coins whose movement I want to sign
$ /usr/local/bin/bitcoin-cli -conf=/home/bitcoin14/.bitcoin/regtest.conf gettransaction 675eac8d9aebe4f84aa2bb18a9f02304d11cb8937ca103b6ccf00fdf0de69727 | egrep 'vout|hex'
      "vout": 1,
      "vout": 1
  "hex": "0100000001fe37d2309bb4b4f4de102ff08f08e049acaecd4c1ee195328404c02406e596c6000000004847304402206eef31e0114c8e31180da39c644fc518624e9c5f354f6c375bfffba8a3c0dc6d022021056d74ea16155036663b7aaab1e297715231747718e48ab94aefd05cded83d01feffffff0254330f00000000001976a91486d1e65bc7f89b24724f50fa5db3a7f102d9644d88acc0aff629010000001976a9143206b266d2998b1092fcd1737165c363c0e31b6c88ac65000000"
  1. I get a new address to which the coins will be redeemed
$ /usr/local/bin/bitcoin-cli -conf=/home/bitcoin14/.bitcoin/regtest.conf getnewaddress
n42ui4Tbd2mVPiRtAUr4RYW7KC7qUmXDTt
  1. I get the private key generated in "2" above
$ /usr/local/bin/bitcoin-cli -conf=/home/bitcoin14/.bitcoin/regtest.conf dumpprivkey mk5U6xEDnTmfNrHcCXkdKrXziYH1YMygVw
cPMjUqc3d4WhDG8Evn2G3RrVWMyetHtwiTZhpKW1T1QUrBhYGVnG
  1. I convert the private key into the hexadecimal equivalent according to https://bitcoin.stackexchange.com/a/52954/89798 (This on a different machine)
$ ./bitcoin-tool --input-type private-key-wif --input-format base58check --output-type private-key --output-format hex --network bitcoin --input cPMjUqc3d4WhDG8Evn2G3RrVWMyetHtwiTZhpKW1T1QUrBhYGVnG
3500dba7a3363e7127cd72273b0e475c5dbb1d378496b803edaf06f8eb3ee160
  1. I take the above and edit them in the Runeks code
HEX_TRANSACTION="0100000001fe37d2309bb4b4f4de102ff08f08e049acaecd4c1ee195328404c02406e596c6000000004847304402206eef31e0114c8e31180da39c644fc518624e9c5f354f6c375bfffba8a3c0dc6d022021056d74ea16155036663b7aaab1e297715231747718e48ab94aefd05cded83d01feffffff0254330f00000000001976a91486d1e65bc7f89b24724f50fa5db3a7f102d9644d88acc0aff629010000001976a9143206b266d2998b1092fcd1737165c363c0e31b6c88ac65000000"
...
OUTPUT_INDEX=1
...
SEND_TO_ADDRESS="n42ui4Tbd2mVPiRtAUr4RYW7KC7qUmXDTt"
...
PRIVATE_KEY=0x3500dba7a3363e7127cd72273b0e475c5dbb1d378496b803edaf06f8eb3ee160
  1. I execute the code (again a different machine from the previous one)
$ python2 sign3.py 
Traceback (most recent call last):
  File "sign3.py", line 144, in 
    (OUTPUT_INDEX, hash_160_to_bc_address(tx_info('txOut')(OUTPUT_INDEX)('scriptPubKey')(3:-2), bytes(0)))
RuntimeError: The supplied private key cannot be used to redeem output index 1
You need to supply the private key for address mk5U6xEDnTmfNrHcCXkdKrXziYH1YMygVw

The error is clear: I have not provided the correct private key. Where is my mistake? Thank you all!

Change from implementation to password to user password in private Travis-CI

Private Travis-CI is used in our organization to execute compilations and CRON jobs in a private GitHub repository. Currently, an implementation key is configured to access the private GitHub repository. It is now necessary to clone another private repository of the same organization in .travis.yml script. The easiest and recommended way is to change the user password, that is, use a GitHub user, who has access to all required repositories.

I have loaded the SSH key for the user, who has access to both repositories, using the Travis-CI CLI command as described in the documentation of both private repositories. However, I receive the error that the cloning repository is not found.

The script contains a line similar to:

- git clone --progress --verbose git@github.com:organisation/another-git-repo.git /tmp/local-folder

The error message is:

ERROR: repository not found.
fatal: could not be read from the remote repository.

So I guess I need to release the deployment key, so that the user key is used, but I can't find anything in the Travis-CI documentation. Since developers actively use compilations, I want to make sure that releasing the Deploy key is the way to go. Can anyone confirm that it is necessary to release the deployment key in order for the CLI user key loaded to work?

Nor can I find in the Travis-CI documentation: how can I see that the user key was loaded for use with repositories?

Bitcoin private key and address generator in golang

I am looking for a golang alternative to https://github.com/richardkiss/pycoin. So far I have found https://github.com/btcsuite/btcd but I have not discovered how to generate private keys and addresses using btcd. Python Pycoin has a very practical ku utility to generate private keys and integer addresses.

Is it possible to generate keys and private addresses from integers using https://github.com/btcsuite/btcd?

AUNT

private key: what prevents a smartphone wallet from downloading my keys

Wallet software uses variant technologies to protect and hide our private keys as mnemonic or simply using private keys.

What would prevent them from making a patch on your software that will install a keylogger, take a screenshot or simply go to memory and retrieve that private information?