If you are happy to borrow from stdlib, the index/suffixarray package contains code that will generate a suffix array very quickly (n log n). The important functions are not exported, so they are copied in the example below.
Once you have the suffix array, it's not difficult to generate the bwt: