And holding a person is already a separate charge - kidnapping and unlawful imprisonment.
An actual sentence eventually has an end.
You have the right to not divulge information from your mind that would incriminate yourself. In the US this is called the 5th amendment.
The alternative is that people have a right to stop the courts from functioning. Our interests in not having courts that can be easily thwarted probably does outweigh our interests in any single conviction.
It's only forever if you refuse to comply for that long. It is coercive. The justice system does not, because it cannot, ask you to comply. It forces you to comply and it does not take no for an answer or it would not function.
Self incrimination protections are there for a good reason. If the person is on trial, then they haven't pleaded guilty - they say they haven't done what they are accused of. If they are in fact innocent, if the court decided to enforce them into provide this information and jailed them for contempt, it would literally be indefinite because they are innocent and cannot provide information on what they don't know!
I guess that's why a key or fingerprint is allowed - it is definitely something that can be used one way or the other. If they key doesn't work, it doesn't work. Same with a fingerprint.
But a passcode - it is not definitive. The defendant may say they don't know it as someone changed it on them. They then type in a wrong code: how are you going to disprove that?
A charge of contempt is still a charge.