HMACSHA512

HMACSHA 512

在Java .NET Objective – C

Java

private final static String algorithm = "HMACSHA512";

 

    public static void main(String[] args) {

        String result = hmacSHA512("3e4f2550-0818-4665-9bfb-edbe9b15f586");

 

        System.out.println(result);

    }

 

    private static String hmacSHA512(String key) {

        String result = "";

 

        try {

            Calendar c = Calendar.getInstance(TimeZone.getTimeZone("UTC"));

            long unixTime = c.getTimeInMillis() / 1000;

            String data = String.valueOf(unixTime);

 

            final byte[] bytesData = data.getBytes("UTF-8");

            final byte[] bytesKey = key.getBytes();

            final SecretKey secretKey = new SecretKeySpec(bytesKey, algorithm);

            Mac mac = Mac.getInstance(algorithm);

            mac.init(secretKey);

            mac.update(bytesData);

            final byte[] macData = mac.doFinal();

 

            final long l = bytesToLong(macData);

            BigDecimal bDecimal = readUnsignedLong(l);

            BigDecimal pBigDecimal = new BigDecimal(Math.pow(10, 15));

            BigDecimal token = bDecimal.remainder(pBigDecimal);

 

            result = String.valueOf(token);

 

            return resultCheck(result);

        } catch (Exception e) {

            System.out.println(e.getMessage());

        }

 

        return result;

    }

 

    private static String resultCheck(String token) {

        while (token.length() < 15) {

            token = "0" + token;

        }

 

        return token;

    }

 

    public static long bytesToLong(byte[] b) {

        long s = 0;

        long s0 = b[0] & 0xff;

        long s1 = b[1] & 0xff;

        long s2 = b[2] & 0xff;

        long s3 = b[3] & 0xff;

        long s4 = b[4] & 0xff;

        long s5 = b[5] & 0xff;

        long s6 = b[6] & 0xff;

        long s7 = b[7] & 0xff;

        s1 <<= 8;

        s2 <<= 16;

        s3 <<= 24;

        s4 <<= 8 * 4;

        s5 <<= 8 * 5;

        s6 <<= 8 * 6;

        s7 <<= 8 * 7;

        s = s0 | s1 | s2 | s3 | s4 | s5 | s6 | s7;

        return s;

    }

 

    public static final BigDecimal readUnsignedLong(long value) throws IOException {

        if (value >= 0)

            return new BigDecimal(value);

 

        long lowValue = value & 0x7fffffffffffffffL;

        return BigDecimal.valueOf(lowValue).add(BigDecimal.valueOf(Long.MAX_VALUE)).add(BigDecimal.valueOf(1));

    }

 

.NET

static void Main(string[] args)

{

var dateTime = new DateTime(2018, 6, 1, 15, 22, 34, DateTimeKind.Utc);

Console.WriteLine(Generate("3e4f2550-0818-4665-9bfb-edbe9b15f586", dateTime));

 

Console.ReadKey();

}

 

public static string Generate(string secretKey, DateTime date)

{

date = date.Kind == DateTimeKind.Utc ? date : date.ToUniversalTime();

 

var unixTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);

var counter = (long)date.Subtract(unixTime).TotalSeconds;

 

byte[] keyBytes = Encoding.ASCII.GetBytes(secretKey);

 

ulong hash = 0;

 

using (HMACSHA512 hmac = new HMACSHA512(keyBytes))

{

byte[] counterBytes = Encoding.UTF8.GetBytes(counter.ToString());

byte[] hashBytes = hmac.ComputeHash(counterBytes);

 

hash = BitConverter.ToUInt64(hashBytes, 0);

}

 

ulong token = hash % (ulong)Math.Pow(10, 15);

 

return token.ToString("000000000000000");

}

 

Objective – C

- (NSString *)generate:(NSString *)secretKey {

NSString *counter = @"1527866554";

NSData *counterBytes = [counter dataUsingEncoding:NSUTF8StringEncoding];

NSData *keyBytes = [secretKey dataUsingEncoding:NSUTF8StringEncoding];

 

NSMutableData *macOut = [NSMutableData dataWithLength:CC_SHA512_DIGEST_LENGTH];

CCHmac( kCCHmacAlgSHA512,

keyBytes.bytes,

keyBytes.length,

counterBytes.bytes,

counterBytes.length,

macOut.mutableBytes);

 

unsigned long long hash = 0;

[macOut getBytes:&hash length:sizeof(unsigned long)];

unsigned long long token = hash % (unsigned long)pow(10, 15);

NSString *strToken = [NSString stringWithFormat:@"%015lld", token];

NSLog(@"secret key = %@, counter = %@, payment code = %@", secretKey, counter, strToken);

return strToken;

}

原文地址:https://www.cnblogs.com/BlueEye/p/8760821.html