/* * Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package example.cleaner; import java.lang.ref.Cleaner; import java.util.Arrays; import java.util.Optional; public class SensitiveData implements AutoCloseable { // A cleaner (preferably one shared within a library, // but for the sake of example, a new one is created here) private static final Cleaner cleaner = Cleaner.create(); // The sensitive data private char[] sensitiveData; // The result of registering with the cleaner private final Cleaner.Cleanable cleanable; /** * Construct an object to hold sensitive data. * @param sensitiveData a char array, non-null */ public SensitiveData(char[] sensitiveData) { final char[] chars = sensitiveData.clone(); final Runnable F // Pick one of the following cleaner functions = () -> Arrays.fill(chars, (char) 0); // A lambda // = new SensitiveCleanable(chars); // a nested record class for cleanup // = clearCharsRunnable(chars); // lambda from a static context this.sensitiveData = chars; this.cleanable = cleaner.register(this, F); } /** * Return an Optional of a copy of the char array. */ public Optional sensitiveData() { return Optional.ofNullable(sensitiveData == null ? null : sensitiveData.clone()); } /** * Close and cleanup the sensitive data storage. */ public void close() { sensitiveData = null; // Data not available after close cleanable.clean(); // The cleanable already has the char array reference } // Return a lambda to do the clearing, ensure it does not reference 'this'. private static Runnable clearCharsRunnable(char[] chars) { return () -> Arrays.fill(chars, (char)0); } /* * Record class to perform the cleanup of a char array. */ private record SensitiveCleanable(char[] sensitiveData) implements Runnable { public void run() { // cleanup action accessing SensitiveCleanable, executed at most once Arrays.fill(sensitiveData, (char)0); } } // Encapsulate a string for printing and clear the temporary buffer. public static void main(String[] args) { for (String s : args) { char[] chars = s.toCharArray(); try (SensitiveData sd = new SensitiveData(chars)) { Arrays.fill(chars, (char) 0); print(sd); } } } // Print the sensitive data and clear the temporary buffer. private static void print(SensitiveData sd) { char[] chars = sd.sensitiveData().get(); System.out.println(chars); Arrays.fill(chars, (char)0); } }