Package org.keyczar.interfaces

Examples of org.keyczar.interfaces.SigningStream


    LOG.debug(Messages.getString("Signer.Signing", input.remaining()));
    KeyczarKey signingKey = getPrimaryKey();
    if (signingKey == null) {
      throw new NoPrimaryKeyException();
    }
    SigningStream stream = SIGN_QUEUE.poll();
    if (stream == null) {
      stream = (SigningStream) signingKey.getStream();
    }

    int spaceNeeded = digestSize();
    if (expirationTime > 0) {
      spaceNeeded += TIMESTAMP_SIZE;
    }
    if (output.capacity() < spaceNeeded) {
      throw new ShortBufferException(output.capacity(), spaceNeeded);
    }

    ByteBuffer header = ByteBuffer.allocate(HEADER_SIZE);
    signingKey.copyHeader(header);
    header.rewind();
    stream.initSign();

    // Sign the header and write it to the output buffer
    output.mark();
    output.put(header);

    if (expirationTime > 0) {
      // Write an expiration time following the header and sign it.
      ByteBuffer expiration = ByteBuffer.wrap(Util.fromLong(expirationTime));
      output.put(expiration);
      expiration.rewind();
      stream.updateSign(expiration);
    }

    if (hidden != null && hidden.remaining() > 0) {
      // Sign any hidden data
      stream.updateSign(hidden);
    }

    // Sign the input data
    stream.updateSign(input);
    // Sign the version byte
    stream.updateSign(ByteBuffer.wrap(FORMAT_BYTES));

    // Write the signature to the output
    stream.sign(output);
    output.limit(output.position());
    SIGN_QUEUE.add(stream);
  }
View Full Code Here


    LOG.debug(Messages.getString("Signer.Signing", input.remaining()));
    KeyczarKey signingKey = getPrimaryKey();
    if (signingKey == null) {
      throw new NoPrimaryKeyException();
    }
    SigningStream stream = SIGN_QUEUE.poll();
    if (stream == null) {
      stream = (SigningStream) signingKey.getStream();
    }

    int spaceNeeded = digestSize();
    if (output.capacity() < spaceNeeded) {
      throw new ShortBufferException(output.capacity(), spaceNeeded);
    }

    stream.initSign();
    // Sign the header and write it to the output buffer
    output.mark();
    // Sign the input data
    stream.updateSign(input);
    // Write the signature to the output
    stream.sign(output);
    output.limit(output.position());
    SIGN_QUEUE.add(stream);
  }
View Full Code Here

      }
    }

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

        throw new KeyczarException(e);
      }
    }

    public SigningStream getSigningStream() {
      return new SigningStream() {
        public int digestSize() {
          return 0;
        }

        public void initSign() {
View Full Code Here

      if (encryptingKey == null) {
        throw new NoPrimaryKeyException();
      }
      cryptStream = (EncryptingStream) encryptingKey.getStream();
    }
    SigningStream signStream = cryptStream.getSigningStream();

    int outputSize = HEADER_SIZE + cryptStream.maxOutputSize(inputLength) +
        signStream.digestSize();
    ENCRYPT_QUEUE.add(cryptStream);
    return outputSize;
  }
View Full Code Here

    EncryptingStream cryptStream = ENCRYPT_QUEUE.poll();
    if (cryptStream == null) {
      cryptStream = (EncryptingStream) encryptingKey.getStream();
    }
    // Initialize the signing stream
    SigningStream signStream = cryptStream.getSigningStream();
    signStream.initSign();

    // Write the key header
    output.mark();
    ByteBuffer outputToSign = output.asReadOnlyBuffer();
    encryptingKey.copyHeader(output);

    // Write the IV. May be an empty array of zero length
    cryptStream.initEncrypt(output);

    ByteBuffer inputCopy = input.asReadOnlyBuffer();
    while (inputCopy.remaining() > ENCRYPT_CHUNK_SIZE) {
      ByteBuffer inputChunk = inputCopy.slice();
      inputChunk.limit(ENCRYPT_CHUNK_SIZE);
      cryptStream.updateEncrypt(inputChunk, output);
      inputCopy.position(inputCopy.position() + ENCRYPT_CHUNK_SIZE);

      outputToSign.limit(output.position());
      signStream.updateSign(outputToSign);
      outputToSign.position(output.position());
    }

    // Sign any remaining plaintext
    cryptStream.doFinalEncrypt(inputCopy, output);
    output.limit(output.position() + signStream.digestSize());

    // Set the limit on the output to sign
    outputToSign.limit(output.position());
    signStream.updateSign(outputToSign);
    // Sign the final block of ciphertext output
    signStream.sign(output);
    ENCRYPT_QUEUE.add(cryptStream);
  }
View Full Code Here

    }
    KeyczarKey signingKey = getPrimaryKey();
    if (signingKey == null) {
      throw new NoPrimaryKeyException();
    }
    SigningStream stream = SIGN_QUEUE.poll();
    if (stream == null) {
      stream = (SigningStream) signingKey.getStream();
    }

    int spaceNeeded = digestSize();
    if (expirationTime > 0) {
      spaceNeeded += TIMESTAMP_SIZE;
    }
    if (output.capacity() < spaceNeeded) {
      throw new ShortBufferException(output.capacity(), spaceNeeded);
    }

    ByteBuffer header = ByteBuffer.allocate(HEADER_SIZE);
    signingKey.copyHeader(header);
    header.rewind();
    stream.initSign();

    // Sign the header and write it to the output buffer
    output.mark();
    output.put(header);

    if (expirationTime > 0) {
      // Write an expiration time following the header and sign it.
      ByteBuffer expiration = ByteBuffer.wrap(Util.fromLong(expirationTime));
      output.put(expiration);
      expiration.rewind();
      stream.updateSign(expiration);
    }

    if (hidden != null && hidden.remaining() > 0) {
      // Sign any hidden data
      stream.updateSign(hidden);
    }

    // Sign the input data
    stream.updateSign(input);
    // Sign the version byte
    stream.updateSign(ByteBuffer.wrap(FORMAT_BYTES));

    // Write the signature to the output
    stream.sign(output);
    output.limit(output.position());
    SIGN_QUEUE.add(stream);
  }
View Full Code Here

    KeyczarKey signingKey = getPrimaryKey();
    if (signingKey == null) {
      throw new NoPrimaryKeyException();
    }

    SigningStream stream = SIGN_QUEUE.poll();

    if (stream == null) {
      // If not, allocate a new stream object.
      stream = (SigningStream) signingKey.getStream();
    }

    stream.initSign();
    // Attached signature signs:
    // [blob | hidden.length | hidden | format] or [blob | 0 | format]
    byte[] hiddenPlusLength = Util.fromInt(0);
    if (hidden.length > 0) {
      hiddenPlusLength = Util.lenPrefix(hidden);
    }

    stream.updateSign(ByteBuffer.wrap(blob));
    stream.updateSign(ByteBuffer.wrap(hiddenPlusLength));
    stream.updateSign(ByteBuffer.wrap(FORMAT_BYTES));

    // now get signature output
    ByteBuffer output = ByteBuffer.allocate(stream.digestSize());
    output.mark();

    stream.sign(output);
    output.limit(output.position());

    // Attached signature format is:
    // [Format number | 4 bytes of key hash | blob size | blob | raw signature]
    byte[] signature =
View Full Code Here

TOP

Related Classes of org.keyczar.interfaces.SigningStream

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.