Class SecretBuffer

java.lang.Object
co.ankatech.ankasecure.sdk.security.SecretBuffer
All Implemented Interfaces:
AutoCloseable

public final class SecretBuffer extends Object implements AutoCloseable
Owning wrapper for a sensitive byte[]. Zeroizes on close() and nulls the internal field reference. Use only inside try-with-resources blocks.

The underlying buffer is exposed via bytes() for read-only consumption while the wrapper is open; access after close() throws IllegalStateException. The contract takes ownership of the supplied array — the caller must NOT retain a reference to it.

close() is idempotent: a second call is a no-op. The buffer is overwritten with 0x00 via Zeroization.zeroize(byte[]) before the field is nulled.

  • Constructor Summary

    Constructors
    Constructor
    Description
    SecretBuffer(byte[] owned)
    Takes ownership of the supplied array.
  • Method Summary

    Modifier and Type
    Method
    Description
    byte[]
     
    void
    Zeroizes the underlying byte[] (overwrites every element with 0x00), nulls the internal field, and marks this wrapper as closed.
    boolean
     
    int
     

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • SecretBuffer

      public SecretBuffer(byte[] owned)
      Takes ownership of the supplied array. The caller MUST NOT retain a reference to owned after construction; ownership has transferred.
      Throws:
      NullPointerException - if owned is null
  • Method Details

    • bytes

      public byte[] bytes()
      Returns:
      the underlying buffer (read-only consumption pattern)
      Throws:
      IllegalStateException - if close() has been called
    • length

      public int length()
      Returns:
      the length of the underlying buffer
      Throws:
      IllegalStateException - if close() has been called
    • isClosed

      public boolean isClosed()
      Returns:
      true if close() has been called; never throws
    • close

      public void close()
      Zeroizes the underlying byte[] (overwrites every element with 0x00), nulls the internal field, and marks this wrapper as closed. Idempotent.
      Specified by:
      close in interface AutoCloseable