-
-
Save erikt/10a37f818bb217dbcc14 to your computer and use it in GitHub Desktop.
A solution to Square's "Square Root" Java puzzler (http://corner.squareup.com/2013/03/puzzle-square-root.html)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import square.SquareRoot; | |
import java.math.BigInteger; | |
import java.security.Provider; | |
import java.security.SecureRandomSpi; | |
import java.security.Security; | |
/** | |
* A Not very secure random provider implementation. | |
* Made for Square's "Square Root" Java puzzler (2013/03/19). | |
* @author Erik Tjernlund (erik@tjernlund.net) | |
*/ | |
public class EriksNotSecureRandomSpi extends SecureRandomSpi { | |
@Override | |
protected void engineSetSeed(byte[] seed) { | |
} | |
@Override | |
protected void engineNextBytes(byte[] bytes) { | |
bytes[bytes.length - 1] = 1; | |
} | |
@Override | |
protected byte[] engineGenerateSeed(int numBytes) { | |
return new byte[0]; | |
} | |
} | |
/** | |
* Security provider that registers an insane secure random algorithm implementation. | |
* Made for Square's "Square Root" Java puzzler (2013/03/19). | |
* @author Erik Tjernlund (erik@tjernlund.net) | |
*/ | |
final class EriksSecurityProvider extends Provider { | |
public EriksSecurityProvider(String name, double version, String info) { | |
super(name, version, info); | |
put("SecureRandom.SHA1PRNG", "EriksNotSecureRandomSpi"); | |
} | |
/** | |
* Run to solve Square's "Square Root" Java puzzler (2013/03/19). | |
*/ | |
public static void main(String[] args) { | |
Provider provider = new EriksSecurityProvider("EriksSecurityProvider", 1.0, "Erik's not really very secure security provider"); | |
Security.insertProviderAt(provider, 1); | |
SquareRoot.answer(new BigInteger("1")); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment