md5移植笔记(android)

这篇博客详细介绍了如何用C++实现MD5哈希算法,并将其封装为静态库md5core_static.a。通过修改BASE_EXPORT宏隐藏库中的符号,以避免在依赖库中暴露。过程包括:源码分析、CMakeLists配置、构建不同ABI的.a文件、以及在Android Studio中使用静态库的步骤。同时,提供了计算文件MD5值的示例代码。

md5移植目的:

1)生成md5core_static.a静态可执行文件

2)libtestLib.so依赖md5core_static, so库的release版本需要抹去md5的符号

3)计算一个文件的md5值

源码比较少,贴这里

md5.h

源码中修改了BASE_EXPORT这个宏了,如果不修改,libtestLib.so库中的md5相关符号会导出

#define BASE_EXPORT __attribute__((visibility("hidden")))
#define BASE_EXPORT_PRIVATE __attribute__((visibility("hidden")))
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef BASE_MD5_H_
#define BASE_MD5_H_



#if defined(COMPONENT_BUILD)
#if defined(WIN32)

#if defined(BASE_IMPLEMENTATION)
#define BASE_EXPORT __declspec(dllexport)
#define BASE_EXPORT_PRIVATE __declspec(dllexport)
#else
#define BASE_EXPORT __declspec(dllimport)
#define BASE_EXPORT_PRIVATE __declspec(dllimport)
#endif  // defined(BASE_IMPLEMENTATION)

#else  // defined(WIN32)
#if defined(BASE_IMPLEMENTATION)
#define BASE_EXPORT __attribute__((visibility("default")))
#define BASE_EXPORT_PRIVATE __attribute__((visibility("default")))
#else
#define BASE_EXPORT
#define BASE_EXPORT_PRIVATE
#endif  // defined(BASE_IMPLEMENTATION)
#endif

#else  // defined(COMPONENT_BUILD)
#define BASE_EXPORT __attribute__((visibility("hidden")))
#define BASE_EXPORT_PRIVATE __attribute__((visibility("hidden")))
#endif

namespace base {

// MD5 stands for Message Digest algorithm 5.
// MD5 is a robust hash function, designed for cyptography, but often used
// for file checksums.  The code is complex and slow, but has few
// collisions.
// See Also:
//   http://en.wikipedia.org/wiki/MD5

// These functions perform MD5 operations. The simplest call is MD5Sum() to
// generate the MD5 sum of the given data.
//
// You can also compute the MD5 sum of data incrementally by making multiple
// calls to MD5Update():
//   MD5Context ctx; // intermediate MD5 data: do not use
//   MD5Init(&ctx);
//   MD5Update(&ctx, data1, length1);
//   MD5Update(&ctx, data2, length2);
//   ...
//
//   MD5Digest digest; // the result of the computation
//   MD5Final(&digest, &ctx);
//
// You can call MD5DigestToBase16() to generate a string of the digest.

// The output of an MD5 operation.
    struct MD5Digest {
        unsigned char a[16];
    };

// Used for storing intermediate data during an MD5 computation. Callers
// should not access the data.
    typedef char MD5Context[88];

// Computes the MD5 sum of the given data buffer with the given length.
// The given 'digest' structure will be filled with the result data.
    BASE_EXPORT void MD5Sum(const void* data, size_t length, MD5Digest* digest);

// Initializes the given MD5 context structure for subsequent calls to
// MD5Update().
    BASE_EXPORT void MD5Init(MD5Context* context);

// For the given buffer of |data| as a StringPiece, updates the given MD5
// context with the sum of the data. You can call this any number of times
// during the computation, except that MD5Init() must have been called first.
    BASE_EXPORT void MD5Update(MD5Context* context, const std::string& data);

// Finalizes the MD5 operation and fills the buffer with the digest.
    BASE_EXPORT void MD5Final(MD5Digest* digest, MD5Context* context);

// MD5IntermediateFinal() generates a digest without finalizing the MD5
// operation.  Can be used to generate digests for the input seen thus far,
// without affecting the digest generated for the entire input.
    BASE_EXPORT void MD5IntermediateFinal(MD5Digest* digest,
                                          const MD5Context* context);

// Converts a digest into human-readable hexadecimal.
    BASE_EXPORT std::string MD5DigestToBase16(const MD5Digest& digest);

// Returns the MD5 (in hexadecimal) of a string.
    BASE_EXPORT std::string MD5String(const std::string& str);
}  // namespace base

#endif  // BASE_MD5_H_

md5.cpp

无修改

// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// The original file was copied from sqlite, and was in the public domain.

/*
 * This code implements the MD5 message-digest algorithm.
 * The algorithm is due to Ron Rivest.  This code was
 * written by Colin Plumb in 1993, no copyright is claimed.
 * This code is in the public domain; do with it what you wish.
 *
 * Equivalent code is available from RSA Data Security, Inc.
 * This code has been tested ag
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值