Android:HmacSHA1加密和文件转Base64String

博客简单记录了HmacSHA1和Base64在Android中的相关内容,虽未详细展开,但保留了相关信息。

因为比较简单,只做记录:

 /**
     * HmacSHA1 Encrypt.
     * @param encryptText : content to be encrypted.
     * @param encryptKey : secret key.
     * @return Encrypted bytes.
     **/
    public static byte[] HmacSHA1Encrypt(
            String encryptText,
            String encryptKey) throws Exception {
        byte[] data = encryptKey.getBytes(ENCODING);
        SecretKey secretKey = new SecretKeySpec(data, MAC_NAME);
        Mac mac = Mac.getInstance(MAC_NAME);
        mac.init(secretKey);
        byte[] text = encryptText.getBytes(ENCODING);
        byte[] digest = mac.doFinal(text);
        return digest;
    }

    //实现java8的join函数
    private static String join(String[] strs,String splitter) {
        StringBuffer sb = new StringBuffer();
        for(String s:strs){
            sb.append(s+splitter);
        }
        return sb.toString().substring(0, sb.toString().length()-1);
    }

    /**
     * get file's base64 string
     * @param filePath file's path
     * @return string
     */
    public static String getBase64Str(String filePath){
        byte[] readData = null;
        String rtStr = "";
        try
        {
            File file = new File(filePath);

            FileInputStream fis = new FileInputStream(filePath);
            if(fis != null && fis.getChannel() != null) {
                readData = new byte[(int)file.length()];
                int i = fis.read(readData);
                fis.close();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        catch (IOException e){
            e.printStackTrace();
        }
        if (readData != null) {
            rtStr = Base64.encodeToString(readData, Base64.NO_WRAP);
        }
        return rtStr;
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lidongxiu0714

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值