What you are looking for is special-case of a more general, and quite well-studied object in computer science, namely, a Randomness Extractor. The Wikipedia article linked to should get you started in the correct direction, in case you did not already know it.