0

I'm using PMD to check java code, and I've run into the problem that states, "Do not hard code IPv4 or IPv6 addresses, even 127.0.0.1!" The IPv4 address I'm using is in fact just 127.0.0.1, and is only for testing purposes, but nonetheless I must convert the hard-coded version to some sort of encrypted version. I'm not sure what would be the easiest way to do this.

Any help would be greatly appreciated!

TNC
  • 337
  • 2
  • 5
  • 17
  • 3
    Encrypted != (not-hardcoded). Personally, I'd just ignore the warning if you don't know/understand/agree with the justification. – Matt Ball Sep 24 '12 at 23:46
  • 5
    Probably, you can replace it with `localhost` hostname and resolve the address at runtime? – Serge Sep 24 '12 at 23:52
  • 1
    Agreed with @Serge. http://pmd.sourceforge.net/snapshot/rules/basic.html#AvoidUsingHardCodedIP – Matt Ball Sep 24 '12 at 23:57

2 Answers2

3

You are not trying to encrypt anything.

What you wan to do is pass in a host name and do the proper host lookup to get the Internet address. Look at the standard JDK's InetAddress and the getAllByName(String host) and getByName(String host)

fishtoprecords
  • 2,394
  • 7
  • 27
  • 38
2

I've resolved this PMD´s warning in this way:


// Old code
`if (!"127.0.0.1".equals(serverIP)) { ... }`

// New code
`if (!InetAddress.getLoopbackAddress().getHostAddress().equals(serverIP)) { ... }`
Wagner
  • 21
  • 1