Package org.keyczar.interfaces

Examples of org.keyczar.interfaces.VerifyingStream


    for (Iterator<Entry<KeyVersion, KeyczarKey>> iter =
      versionMap.entrySet().iterator(); iter.hasNext(); ) {
      KeyczarKey key = iter.next().getValue();
      ByteBuffer dataCopy = data.duplicate();
      ByteBuffer signatureCopy = signature.duplicate();
      VerifyingStream stream = VERIFY_CACHE.get(key);
      if (stream == null) {
        stream = (VerifyingStream) key.getStream();
      }
      stream.initVerify();
      stream.updateVerify(dataCopy);
      boolean result = stream.verify(signatureCopy);
      VERIFY_CACHE.put(key, stream);
      if (result) {
        return true;
      }
    }
View Full Code Here


    DecryptingStream cryptStream = CRYPT_CACHE.get(key);
    if (cryptStream == null) {
      cryptStream = (DecryptingStream) key.getStream();
    }

    VerifyingStream verifyStream = cryptStream.getVerifyingStream();
    if (inputCopy.remaining() < verifyStream.digestSize()) {
      throw new ShortCiphertextException(inputCopy.remaining());
    }

    // Slice off the signature into another buffer
    inputCopy.position(inputCopy.limit() - verifyStream.digestSize());
    ByteBuffer signature = inputCopy.slice();

    // Reset the position of the input to start of the ciphertext
    inputCopy.reset();
    inputCopy.limit(inputCopy.limit() - verifyStream.digestSize());

    // Initialize the crypt stream. This may read an IV if any.
    cryptStream.initDecrypt(inputCopy);

    // Verify the header and IV if any
    ByteBuffer headerAndIvToVerify = input.asReadOnlyBuffer();
    headerAndIvToVerify.limit(inputCopy.position());
    verifyStream.initVerify();
    verifyStream.updateVerify(headerAndIvToVerify);

    output.mark();
    // This will process large input in chunks, rather than all at once. This
    // avoids making two passes through memory.
    while (inputCopy.remaining() > DECRYPT_CHUNK_SIZE) {
      ByteBuffer ciphertextChunk = inputCopy.slice();
      ciphertextChunk.limit(DECRYPT_CHUNK_SIZE);
      cryptStream.updateDecrypt(ciphertextChunk, output);
      ciphertextChunk.rewind();
      verifyStream.updateVerify(ciphertextChunk);
      inputCopy.position(inputCopy.position() + DECRYPT_CHUNK_SIZE);
    }
    inputCopy.mark();
    verifyStream.updateVerify(inputCopy);
    if (!verifyStream.verify(signature)) {
      throw new InvalidSignatureException();
    }
    inputCopy.reset();
    cryptStream.doFinalDecrypt(inputCopy, output);
    output.limit(output.position());
View Full Code Here

    return false;
  }

  private boolean verify(ByteBuffer data, ByteBuffer signature, KeyczarKey key)
      throws KeyczarException {
    VerifyingStream stream = VERIFY_CACHE.get(key);
    if (stream == null) {
      stream = (VerifyingStream) key.getStream();
    }
    boolean foundValidSignature;
    try {
      stream.initVerify();
      stream.updateVerify(data.duplicate());
      foundValidSignature = stream.verify(signature.duplicate());
      VERIFY_CACHE.put(key, stream);
    } catch (KeyczarException e) {
      // Crypto library can throw errors for invalid keys
      // this allows the verifier to continue trying other keys
      foundValidSignature = false;
View Full Code Here

      return encryptingStream.getSigningStream();
    }

    @Override
    public VerifyingStream getVerifyingStream() {
      return new VerifyingStream() {
        @Override
        public int digestSize() {
          return 0;
        }
View Full Code Here

    public SigningStream getSigningStream() throws KeyczarException {
      return encryptingStream.getSigningStream();
    }

    public VerifyingStream getVerifyingStream() {
      return new VerifyingStream() {
        public int digestSize() {
          return 0;
        }

        public void initVerify() {
View Full Code Here

    if (key == null) {
      throw new KeyNotFoundException(hash);
    }

    VerifyingStream stream = VERIFY_CACHE.get(key);
    if (stream == null) {
      stream = (VerifyingStream) key.getStream();
    }
    stream.initVerify();
    if (hidden != null) {
      stream.updateVerify(hidden);
    }
    stream.updateVerify(data);
    // The signed data is terminated with the current Keyczar format
    stream.updateVerify(ByteBuffer.wrap(FORMAT_BYTES));

    boolean result = stream.verify(signature);
    VERIFY_CACHE.put(key, stream);
    return result;
  }
View Full Code Here

TOP

Related Classes of org.keyczar.interfaces.VerifyingStream

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.