This is the way the old files_encryption did it. 
Look here:
https://github.com/owncloud/core/blob/stable45/apps/files_encryption/lib/crypt.php#L58
https://github.com/owncloud/core/blob/stable45/apps/files_encryption/lib/crypt.php#L138-#L142