@Test public void testCalculateCookbook() throws Exception{ final byte[] n = TokenDecoder.base64DecodeToByte("n4EPtAOCc9AlkeQHPzHStgAbgs7bTZLwUBZdR8_KuKPEHLd4rHVTeT-O-XV2jRojdNhxJWTDvNd7nqQ0VEiZQHz_AJmSCpMaJMRBSFKrKb2wqVwGU_NsYOYL-QtiWN2lbzcEe6XC0dApr5ydQLrHqkHHig3RBordaZ6Aj-oBHqFEHYpPe7Tpe-OfVfHd1E6cS6M1FZcD1NNLYD5lFHpPI9bTwJlsde3uhGqC0ZCuEHg8lhzwOHrtIQbS0FVbb9k3-tVTU4fg_3L_vniUFAKwuCLqKnS2BYwdq_mzSnbLY7h_qixoR7jig3__kRhuaxwUkRz5iaiQkqgc5gHdrNP5zw"); final byte[] e =TokenDecoder.base64DecodeToByte("AQAB"); final byte[] d = TokenDecoder.base64DecodeToByte("bWUC9B-EFRIo8kpGfh0ZuyGPvMNKvYWNtB_ikiH9k20eT-O1q_I78eiZkpXxXQ0UTEs2LsNRS-8uJbvQ-A1irkwMSMkK1J3XTGgdrhCku9gRldY7sNA_AKZGh-Q661_42rINLRCe8W-nZ34ui_qOfkLnK9QWDDqpaIsA-bMwWWSDFu2MUBYwkHTMEzLYGqOe04noqeq1hExBTHBOBdkMXiuFhUq1BU6l-DqEiWxqg82sXt2h-LMnT3046AOYJoRioz75tSUQfGCshWTBnP5uDjd18kKhyv07lhfSJdrPdM5Plyl21hsFf4L_mHCuoFau7gdsPfHPxxjVOcOpBrQzwQ"); BigInteger N = new BigInteger(1, n); BigInteger E = new BigInteger(1, e); BigInteger D = new BigInteger(1, d); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); RSAPublicKeySpec pubKeySpec = new RSAPublicKeySpec(N, E); RSAPrivateKeySpec privKeySpec = new RSAPrivateKeySpec(N, D); rsaPublicKey = (RSAPublicKey) keyFactory.generatePublic(pubKeySpec); rsaPrivKey = (RSAPrivateKey) keyFactory.generatePrivate(privKeySpec); rsa256 = "{\"alg\":\"RS256\",\"kid\":\"bilbo.baggins@hobbiton.example\"}"; assertEquals("MRjdkly7_-oTPTS3AXP41iQIGKa80A0ZmTuV5MEaHoxnW2e5CZ5NlKtainoFmK"+ "ZopdHM1O2U4mwzJdQx996ivp83xuglII7PNDi84wnB-BDkoBwA78185hX-Es4J"+ "IwmDLJK3lfWRa-XtL0RnltuYv746iYTh_qHRD68BNt1uSNCrUCTJDt5aAE6x8w"+ "W1Kt9eRo4QPocSadnHXFxnt8Is9UzpERV0ePPQdLuW3IS_de3xyIrDaLGdjluP"+ "xUAhb6L2aXic1U12podGU0KLUQSE_oI-ZnmKJ3F4uOZDnd6QZWJushZ41Axf_f"+ "cIe8u9ipH84ogoree7vjbU5y18kDquDg", sRsaImpl.calculate(TokenDecoder.base64Encode(rsa256), "SXTigJlzIGEgZGFuZ2Vyb3VzIGJ1c2luZXNzLCBGcm9kbywgZ29pbmcgb3V0IH"+ "lvdXIgZG9vci4gWW91IHN0ZXAgb250byB0aGUgcm9hZCwgYW5kIGlmIHlvdSBk"+ "b24ndCBrZWVwIHlvdXIgZmVldCwgdGhlcmXigJlzIG5vIGtub3dpbmcgd2hlcm"+ "UgeW91IG1pZ2h0IGJlIHN3ZXB0IG9mZiB0by4", new PrivateKey(rsaPrivKey))); }
@Override public String calculate(String header, String payload, PrivateKey signingKey) { byte[] token = toToken(header, payload); try { Signature signature = Signature.getInstance(getAlgorithmInternal()); signature.initSign(signingKey.getPrivateKey()); signature.update(token); token = signature.sign(); return TokenDecoder.base64Encode(token); } catch (Exception e) { throw new RuntimeException(e); } }
SignatureMethodRSAImpl implements SignatureMethod<PrivateKey, PublicKey> { @Override public String calculate(String header, String payload, PrivateKey signingKey) { byte[] token = toToken(header, payload); try { Signature signature = Signature.getInstance(getAlgorithmInternal()); signature.initSign(signingKey.getPrivateKey()); signature.update(token); token = signature.sign(); return TokenDecoder.base64Encode(token); } catch (Exception e) { throw new RuntimeException(e); } } }
SignatureMethodRSAImpl implements SignatureMethod<PrivateKey, PublicKey> { @Override public String calculate(String header, String payload, PrivateKey signingKey) { byte[] token = toToken(header, payload); try { Signature signature = Signature.getInstance(getAlgorithmInternal()); signature.initSign(signingKey.getPrivateKey()); signature.update(token); token = signature.sign(); return TokenDecoder.base64Encode(token); } catch (Exception e) { throw new RuntimeException(e); } } SignatureMethodRSAImpl(String algorithm); }
SignatureMethodRSAImpl implements SignatureMethod<PrivateKey, PublicKey> { @Override public String calculate(String header, String payload, PrivateKey signingKey) { byte[] token = toToken(header, payload); try { Signature signature = Signature.getInstance(getAlgorithmInternal()); signature.initSign(signingKey.getPrivateKey()); signature.update(token); token = signature.sign(); return TokenDecoder.base64Encode(token); } catch (Exception e) { throw new RuntimeException(e); } } SignatureMethodRSAImpl(String algorithm); @Override String calculate(String header, String payload, PrivateKey signingKey); @Override boolean verify(String signature, String header, String payload, PublicKey verifyingKey); @Override String getAlgorithm(); }
SignatureMethodRSAImpl implements SignatureMethod<PrivateKey, PublicKey> { @Override public String calculate(String header, String payload, PrivateKey signingKey) { byte[] token = toToken(header, payload); try { Signature signature = Signature.getInstance(getAlgorithmInternal()); signature.initSign(signingKey.getPrivateKey()); signature.update(token); token = signature.sign(); return TokenDecoder.base64Encode(token); } catch (Exception e) { throw new RuntimeException(e); } } SignatureMethodRSAImpl(String algorithm); @Override String calculate(String header, String payload, PrivateKey signingKey); @Override boolean verify(String signature, String header, String payload, PublicKey verifyingKey); @Override String getAlgorithm();  }