Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*
 * 
 * Copyright (C) 2015 Orange Labs
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *    http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */

import java.security.PrivateKey;
import java.security.PublicKey;

import java.security.Signature;

import android.util.Base64;
import android.util.Log;

public class Main {
    static public boolean verifyJWS(String s, String algorithm, PublicKey pubKey, PrivateKey privKey) {
        boolean bverify = false;

        String parts[] = s.split("\\.");
        if (parts == null || parts.length != 3)
            return bverify;

        try {
            if ("RS256".compareTo(algorithm) == 0)
                algorithm = "SHA256withRSA";
            Signature signature = Signature.getInstance(algorithm, "SC");
            signature.initVerify(pubKey);
            signature.update((parts[0] + "." + parts[1]).getBytes());
            bverify = signature.verify(decodeB64(parts[2]));

            Log.d("verifyJWS", "payload: " + new String(decodeB64(parts[1])));
        } catch (Exception e) {
            e.printStackTrace();
        }

        return bverify;
    }

    static byte[] decodeB64(String s64) {
        return Base64.decode(s64, Base64.URL_SAFE);
    }
}