code: byte[] DERToJOSE(byte[] derSignature) throws SignatureException {
          // DER Structure: http://crypto.stackexchange.com/a/1797
          boolean derEncoded = derSignature[0] == 0x30 && derSignature.length != ecNumberSize * 2;
          if (!derEncoded) {
              throw new SignatureException("Invalid DER signature format.");
          }
  
          final byte[] joseSignature = new byte[ecNumberSize * 2];
  
          //Skip 0x30
          int offset = 1;
          if (derSignature[1] == (byte) 0x81) {
              //Skip sign
              offset++;
          }
  
          //Convert to unsigned. Should match DER length - offset
          int encodedLength = derSignature[offset++] & 0xff;
          if (encodedLength != derSignature.length - offset) {
              throw new SignatureException("Invalid DER signature format.");
          }
  
          //Skip 0x02
          offset++;
  
          //Obtain R number length (Includes padding) and skip it
          int rLength = derSignature[offset++];
          if (rLength > ecNumberSize + 1) {
              throw new SignatureException("Invalid DER signature format.");
          }
          int rPadding = ecNumberSize - rLength;
          //Retrieve R number
          System.arraycopy(derSignature, offset + Math.max(-rPadding, 0), joseSignature, Math.max(rPadding, 0), rLength + Math.min(rPadding, 0));
  
          //Skip R number and 0x02
          offset += rLength + 1;
  
          //Obtain S number length. (Includes padding)
          int sLength = derSignature[offset++];
          if (sLength > ecNumberSize + 1) {
              throw new SignatureException("Invalid DER signature format.");
          }
          int sPadding = ecNumberSize - sLength;
          //Retrieve R number
          System.arraycopy(derSignature, offset + Math.max(-sPadding, 0), joseSignature, ecNumberSize + Math.max(sPadding, 0), sLength + Math.min(sPadding, 0));
  
          return joseSignature;
      }
docs: Visible for testing