📄 sharedsecret.java
字号:
/** * Gets creator's certificate. * @return creator's certificate */ public Certificate getCreatorCert() { return _creatorCert; } /** * Gets the string representation of the encoded creator's certificate * @return a string */ public String getCreatorEncodedString() { return Hexadecimal.valueOf(_creatorCertSeq); } /** * Gets domain name. * @return domain name */ public String getDomainName() { return _domainName; } /** * Gets secret. * @return shared secret */ private String getSecret() { return Hexadecimal.valueOf(sequence()); } /** * Gets signature. * @return signature */ public byte[] getSignature() { return _signatureSeq; } /** * Gets signature string. * @return signature strnig */ public String getSignatureString() { return _signature; } /** * Initializes data. */ private final void init() { try { _sign = Signature.getInstance(SIGNATURE_ALGORITHM); } catch (NoSuchAlgorithmException excpt) { System.err.println(excpt.toString()); } } /** * Loads shared secret. * @param filename filename of the shared secret file to be loaded */ public synchronized static SharedSecret load(String filename) throws FileNotFoundException, IOException { FileReader freader = new FileReader(filename); BufferedReader breader = new BufferedReader(freader); Vector lines = new Vector(); String line = null; while (true) { line = breader.readLine(); if (line == null) { // end of line break; } lines.addElement(line); } breader.close(); return convertLinesToSharedSecret(lines.elements()); } /** * Saves to file. * @param filename filename of the shared secret file to be saved */ public void save(String filename) throws IOException { Enumeration lines = toLines(); if (lines == null) { System.err.println("No secret."); return; } FileWriter fwriter = new FileWriter(filename); BufferedWriter bwriter = new BufferedWriter(fwriter); while (lines.hasMoreElements()) { String line = (String)lines.nextElement(); bwriter.write(line); bwriter.newLine(); } bwriter.flush(); bwriter.close(); } /** * Saves shared secret. * @param filename filename of the shared secret file to be saved * @param secrets the shared secret to be saved */ public synchronized static void save(String filename, SharedSecret secret) throws IOException { if (secret == null) { throw new IOException("Secret is null."); } secret.save(filename); } /** * Returns current byte sequence as a shared secret (password) for authentication. * @return current byte sequence as a shared secret (password) for authentication. */ final public byte[] secret() { try { ByteSequence seq = new ByteSequence(sequence()); // seq.append(_dateSeq); seq.append(_domainNameSeq); seq.append(_creatorCert.getEncoded()); return seq.sequence(); } catch (Exception ex) { ex.printStackTrace(); return null; // ??????Is this OK?(HT) } } /** * Sets signature. * @param signature signature */ private void setCreator(Certificate creator) { try { _creatorCert = creator; _creatorCertSeq = creator.getEncoded(); } catch (java.security.cert.CertificateEncodingException ex) { System.out .println("Cannot get encoded byte sequence of the creator's certificate: " + creator.toString()); _creatorCert = null; _creatorCertSeq = null; } } /** * Sets domain name. * @param name domain name */ private void setDomainName(String name) { _domainName = name; ByteSequence seq = new ByteSequence(name); _domainNameSeq = seq.sequence(); } /** * Sets signature. * @param signature signature */ private void setSignature(byte[] signature) { _signature = Hexadecimal.valueOf(signature); _signatureSeq = signature; } /** * Sets signature. * @param signature signature string */ private void setSignature(String signature) { byte[] seq = null; try { seq = Hexadecimal.parseSeq(signature); } catch (NumberFormatException excpt) { return; } _signature = signature; _signatureSeq = seq; } /** * Signs the signature. */ final private void sign(PrivateKey key) { if (key == null) { // unknown user System.err.println("Sharedsecret.sign(): null private key"); return; } try { _mdigest.reset(); _mdigest.update(secret()); _sign.initSign(key); _sign.update(_mdigest.digest()); setSignature(_sign.sign()); } catch (InvalidKeyException excpt) { System.err.println(excpt.toString()); return; } catch (SignatureException excpt) { System.err.println(excpt.toString()); return; } } /** * Returns lines representation of the shared secret. * @return lines representation of the shared secret */ public Enumeration toLines() { Vector lines = null; final String secret = getSecret(); final String domain = getDomainName(); final String creator = getCreatorEncodedString(); final String signature = getSignatureString(); if (secret != null &&!secret.equals("")) { if (lines == null) { lines = new Vector(); } lines.addElement(FIELD_SECRET + FIELD_NAME_TERM + secret); } if (domain != null &&!domain.equals("")) { if (lines == null) { lines = new Vector(); } lines.addElement(FIELD_DOMAIN_NAME + FIELD_NAME_TERM + domain); } if (creator != null &&!creator.equals("")) { if (lines == null) { lines = new Vector(); } lines.addElement(FIELD_CREATOR + FIELD_NAME_TERM + creator); } if (signature != null &&!signature.equals("")) { if (lines == null) { lines = new Vector(); } lines.addElement(FIELD_SIGNATURE + FIELD_NAME_TERM + signature); } if (lines == null) { return null; } return lines.elements(); } /** * Returns a string representation of the shared secret. * @return a string representation of the shared secret * @see ByteSequence#toString * @override ByteSequence#toString */ public String toString() { Enumeration lines = toLines(); if (lines == null) { return null; } String str = null; while (lines.hasMoreElements()) { String line = (String)lines.nextElement(); if (str == null) { str = line; } else { str += _strNewLine + line; } } return str; } /** * Verifies the signature. * @return true if the signature is correct, otherwise false. */ final private boolean verify() { if (_signatureSeq == null) { return false; } try { _mdigest.reset(); _mdigest.update(secret()); _sign.initVerify(_creatorCert.getPublicKey()); // - System.out.println("secret : "+Hexadecimal.valueOf(secret())); _sign.update(_mdigest.digest()); // - System.out.println("signature : "+Hexadecimal.valueOf(_signatureSeq)); return _sign.verify(getSignature()); } catch (InvalidKeyException excpt) { System.err.println(excpt.toString()); return false; } catch (SignatureException excpt) { System.err.println(excpt.toString()); return false; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -