Java Reference
In-Depth Information
integer division to divide the length value in half (line 231). The result is used
as the midpoint of the original string, obtaining two equal length substrings for
a password with an even number of characters. For a password with an odd
number of characters, the substring obtained from the last half is longer by one
character.
Because the password value will have its characters modified in the process
of encryption, and because String objects are immutable while StringBuffer
objects are mutable, lines 232 and 233 place the swapped halves of the original
password in a new StringBuffer object for further manipulation, leaving the orig-
inal password String unchanged. This is accomplished by using two forms of the
String method, substring(). The substring() method returns a portion, or
substring, of a String object. The form with a single argument, as in
pswd.substring(midpoint)
returns a new String with the same character sequence as the String pswd,
beginning at the midpoint index and continuing through the end of the string,
which, in this program, represents the last half of pswd. The form with two int
arguments specifies a substring with a beginning and ending location in the
String. For example, the code
pswd.substring(0, midpoint)
returns a new String with the same character sequence as pswd, beginning at offset
zero (the beginning of the string) and extending to the character at midpoint — 1;
this string represents the first half of pswd. Concatenating the two resulting strings,
the right half and then the left half of the password, provides the argument for the
StringBuffer constructor to create a string value for further manipulation.
The next step in the algorithm is to reverse the order of the characters in the
encryption string. The StringBuffer method, reverse(), in line 235 accomplishes
this with a single method call that replaces the character sequence contained in
the string buffer with the reverse of the sequence.
The next step is to loop through all of the characters (line 237) and encrypt
them by performing a bitwise AND operation (line 238), which uses the opera-
tor, &, as contrasted with the logical AND operator, &&. Individual bits (binary
digits) can have a value of zero (off ) or one (on). A bitwise AND examines the
bits of each character and, where two corresponding bits are both set on (have a
value of one), the corresponding bit in the result is set to one. In all other cases,
the resulting bit is set to off, or zero. The bitwise AND operator works only on
integer types, such as char; however, the result of a bitwise AND operation on
two char values will be placed in an int by the compiler, if not otherwise speci-
fied, to avoid a possible loss of precision. Line 238 casts the result back to a char
by placing the char keyword in parentheses before the resulting expression. The
result is used as an argument to the StringBuffer method, setCharAt(), to change
the value of each character in the encrypted password.
Line 240 obtains a hash code of the original password value from the String
method, hashCode(). A hash code is a transformation of a string of characters
into a shorter, fixed-length value (typically an integer) that represents the
original string. Hash codes are not necessarily unique, in that more than one
string can produce the same hash code; however, to produce the same hash code,
the strings generally have to be similar. The hashCode() method returns a hash
Search WWH ::




Custom Search