1. Create a UTF-8 encoded Policy and encode the Policy in Base64.

var policy = "{\" expiration \ ": \" the 2120-01-01 T12:00:00. 000 z \ ", \ "the conditions \" : [[\ "content - length - range \", 0, 104857600]]}";
var encodePolicy = base64Encode(utf8.encode(policy));
print("encodePolicy : $encodePolicy");
Copy the code

For what Policy is, find the documentation on Aliyun

var policyText = {
    "expiration": "The 2020-01-01 T12:00:00) 000 z".// Set the expiration time of the Policy. If the expiration time is exceeded, files cannot be uploaded using the Policy
    "conditions": [["content-length-range".0.1048576000] // Set the size limit for uploading files. If the limit is exceeded, an error will be reported when uploading files to OSS]}Copy the code

2. Use the HMAC-SHA1 method, pass in accessKeySecret for encryption, and use Base64 encoding to obtain signature.

var key = utf8.encode(accessKeySecret);
var bytes = utf8.encode(encodePolicy);

var hmacSha1 = new Hmac(sha1, key);
Digest sha1Result = hmacSha1.convert(bytes);
print("sha1Result:$sha1Result");

String signature = base64Encode(sha1Result.bytes);
print("signature:$signature");
Copy the code

Dart uses HMAC-SHA1 encryption requiring the introduction of crypto

dependencies:
   crypto: ^3.0. 0
Copy the code
import 'package:crypto/crypto.dart';
Copy the code

See specific usage examples:

Pub. Flutter – IO. Cn/packages/cr…

www.woolha.com/tutorials/d…

3. Verify that the signature is correct

Find Java form upload examples in Ali Cloud: help.aliyun.com/document_de…

Write a Java class to print signature

    public static void main(String[] args) {
        // Create a UTF-8 encoded Policy and Base64 encoded the Policy
        final String policy = "{\" expiration \ ": \" the 2120-01-01 T12:00:00. 000 z \ ", \ "the conditions \" : [[\ "content - length - range \", 0, 104857600]]}";
        String encodePolicy = new String(Base64.encodeBase64(policy.getBytes()));
        System.out.println(encodePolicy);

        // Generate a signature
        String signaturecom = com.aliyun.oss.common.auth.ServiceSignature.create().computeSignature(accessKeySecret, encodePolicy);
        System.out.println(signaturecom);
    }
Copy the code

The results printed to dart are consistent with the Java results.