avoid code duplication, move encrypt checking to lib