Brazilians use 'boa noite' either as a greeting or when they go to bed. The current use for 'bom dia/boa tarde/boa noite' dependes on time clock: before noon 'bom dia' should be said; after noon and before 18:00, 'boa tarde'; after 18:00, boa noite. Also, as I said before, those greetings are used both for meeting someone or 'saying goodbye'.
In theory, and following those rules, Brazilians should say 'bom dia' after midnight but it doesn't happen exactly like that. It depends much more on your sleeping time. If you wake up at 4:30 after a normal sleep, to go to work, you certainly would say 'bom dia' to people you meet. But if you are just coming from a party and arrive at 4:30 at home, you certainly would say 'boa noite' before going to bed.